Introduction to C++ Language
- C++ is a low-level language allowing data manipulation that computers use (bits, bytes, addresses).
- General-purpose programming language conducive for scientific and management applications.
- Examples of applications written in C/C++: Microsoft Word and Excel.
- Introduced modular programming using several modules (modules represented as
.c files).
Development Cycle Model
- The development of C++ programs follows a structured cycle which includes:
- Editing/Correction of Source Code
- Compilation Process
- Testing
Structure of a C++ Program
- Basic structure of a C++ program demonstrated in main.c:
#include <stdio.h>
#include <stdlib.h>
int main() {
printf("Hello World!\n");
system("PAUSE");
return 0;
}
- Components:
- Include Directives: Tell the preprocessor to include standard library headers (e.g.,
stdio.h for I/O functions). - Main Function: Execution starts and ends in
int main(). The opening brace { marks the beginning of instructions. - Return Statement:
return 0; signifies successful execution.
- Comments enhance documentation and maintenance for developers.
- Two forms:
- Single-line:
// comment - Multi-line:
/* comment */
Variable Declaration
- A variable must be declared before use.
- Syntax:
Type variable_name [= value]; - Common Types:
- int: Integer type
- float: Floating-point type
- bool: Boolean type
- char: Character type
- char variable[n]: String (character array)
Assignment Statement
- Assigning values to a variable:
Variable = expression;
- Common Arithmetic Operators:
+ : addition- : subtraction* : multiplication/ : division% : modulo
- Input can be handled as follows:
- Using
scanf() from stdio.h:
cpp
scanf("formats", &variable);
// where formats can be %d, %f, %c, %s
- Using
cin from iostream.h:
cpp
cin >> variable;
- Output can be handled similarly:
- Using
printf():
cpp
printf("formats", variable);
- Using
cout:
cpp
cout << variable;
Conditional Structures
- Simple Conditional Statement:
if (expression) {
// Instructions
}
- Alternative Conditional Statement:
if (expression) {
// Instructions1
} else {
// Instructions2
}
Looping Constructs
- Counting Loop (for loop):
for(expr1; expr2; expr3) {
// Instructions
}
- expr1: Initialized once at the beginning.
- expr2: Checked before each iteration (if true, continues).
- expr3: Modified after each iteration.
- Conditional Loop (while loop):
while (expression) {
// Instructions
}
- Post-test Loop (do-while loop):
do {
// Instructions
} while (expression);
Additional Programming Tasks
- Example exercises to implement basic C programs:
- Write a program that calculates circle properties given a radius.
- Implement a temperature conversion program from Fahrenheit to Celsius.
- Create a program that evaluates if a number is even or odd.
- Develop a billing program considering various conditions for discounts.
- Emphasis on systematic approach to problem-solving using programming logic and structures.