HISTORY
- C is derived from B programming language
- C is developed from AT&T Bell Lab in the 1970s
- C was first used to develop and maintain Unix operating systems
- C++ is evolved from C in early 1980s
- C++ is an object-oriented programming language
- Today C++ is used widely in developing operating systems, networking, database and application software
A SAMPLE OF C++ PROGRAM
The C++ program shown in Example 1 is stored in a file named hello.cpp.
#include<iostream>
using namespace std;
//This program displays a "Hell Word" message
int main( )
{
cout<<"Hello Word"<<endl;
return 0;
}
Analyze the program
- Files that contain C++ programs are named using the .cpp file extension (for example hello.cpp).
- Files that contains C++ programs must have a main() function to indicate where the program execution begins. The main function as well as other functions in a C++ program begins with an opening curly brace and ends with a closing curly brace( { and } ).
- The main( ) function usually is written to return an integer value. The int before the main( ) function tells C++ that the main( ) function returns an integer.
- C++ is case-sensitive.
- The statement #include<iostream> is a preprocessor directive that is included in C++ program to allow the program to perform the input and output.
- The statement return 0 will tell C++ to exit the main( ) function. This statement simply means "The program ends here".
- The arrow << tell you the direction of data moving. cout is an object. If you think cout as a name of the screen (the output device), then the arrow tells the computer to send the string in quotes to the screen, this causes the text contained in the quotes to be written to the screen.
- endl tells the computer to start a new line after writing out the text.
- Line comments starts with two forward slashes (//) and continue to the end of the current line. Line comments can appear on the line by themselves or at the end of a line following executable code. The compiler does not compile the comment statements
- Block comments start with a foward slash and an asterisk (/*) and end with an asterisk and a forward slash (*/). Block comments can appear on line by themselves, on a line before the executable code, or after executable code. Block comments also can extend across as many lines as needed.
DATA TYPES
Figure 1.1 lists the four basic C++ data types.
Figure 1.1:Basic C++ Data types
Data type |
Size (bytes) |
char |
1 |
int |
2 or 4 |
float |
4 |
double |
8 |
The other data types you can use in C++ are long, short, or unsigned, as illustrated in Figure 1.2. You use the qualifier unsigned with data types char or int. You can store only positive values in an unsigned char or unsigned int. You use the qualifier long with data types integer and double. You use the qualifier short with data type int.
Figure 1.2 lists other C++ data types.
Figure 1.2 C++ Qualifiers
Data Type |
Size(bytes) |
unsigned char |
1 |
unsigned int |
2 |
unsigned long int |
4 |
short int |
2 |
long int |
4 |
long double |
10 |
VARIABLES
- An identifier is a named memory location.
- A variable is an identifer that C++ programs use to store values needed by your program.
- Variable names in C++ can consists of letters, numeric digits, the the underscore character, but they can not begin with a digit. When naming variables, remember that you can not you a C++ keyword. A keyword is a word that the compiler reserves with a special meaning. The length of variable names are limited to 255 characters.
DECLARING A VARIABLE
- You must declare a variable before you can use them
- Syntax: Data type variable name(s);
Example: int x,y ;
- You can initialize a C++ variable when you declare it.
Example: double x=4.5;
INPUT/OUTPUT
- Use the C++ extraction operator (>>) to extract data from the input stream, such as cin. cin is a standard input stream, which is usually the keyboad.
- Syntax: cin >> input variable;
Example: int x, y;
cin >>x>>y; //Accept two integer numbers entered from the keyboard. The numbers are
seperated by at least one space.
- Use the C++ extraction operator (<<) to insert data into an output stream. You can use the cout output stream. cout is a standard output stream, which is a screen.
Example: cout <<"hello world"<<endl; //Display hello world on the screen
- Use manipulators with the insertion operator to format the data to be displayed. Before you use the manipulators in a program, include a header file statement #include<iomanip.h> right after #include<isotream.h>.Figure 1.3 lists some manipulators and their description.
Figure 1.3: C++ Manipulators
Manipulator |
Description |
Example |
endl |
Cause a newline to be inserted into the output streams (move a cursor key to a new line) |
cout<<"hello word"<<endl; |
setw( ) |
Allows you to specify a field width to be occupied by a value. |
int num=8;
cout<<setw(8)<<num; |
setprecision( ) |
Sets a number of decmial places of a float or double number. |
double y=1.23456;
cout<<setiosflags(ios::fixed)<<setprecision(2)<<y<<endl;
the output will be 1.23 |
- You can output strings and numbers together.
Example: double x =1800.0;
cout<<"You ingested "<<setioflags(ios::fixed)<<setprecision(2)<<x<< " Calories"<<endl;
output You ingested 1800.00 calories
ARITHMETIC OPERATORS
Figure 1.4 lists some common C++ operators and their functions.
Figure 1.4 C++ Operators
Operator Name |
Operator Symbol |
Example |
Comment |
Addition |
+ |
num1 + num2 |
|
Subtraction |
- |
num1 - num2 |
|
Multiplicaton |
* |
num1*num2 |
|
Division |
/ |
15/3
15.0/2.0
|
Integer division; result is 7; fraction is truncated
Floating-point division; result is 7.500000 |
Modulus |
% |
5%2 |
Performs the division and finds the remainder; the result is 1 |
Unary minus |
- |
-(num1) |
if value of num1 is 10, then -(num1) is -10 |
Assignment |
= |
num1=10; |
assigns 10 to variable num1 |
|
+= |
num += 10; |
Equivalent to num = num +10; |
|
-= |
num -= 10; |
Equivalent to num = num -10; |
|
*= |
num *= 10; |
Equivalent to num = num *10; |
|
/= |
num /= 10; |
Equivalent to num = num /10; |
|
%= |
num %= 10; |
Equivalent to num = num %10; |
Increment |
++ |
num++ or ++num |
Increment num to 1 |
Decrement |
-- |
num-- or --num |
Decrement num to 1 |
Precedence prefers to the order in which the compiler will execute the operations. Operators have different precedences associated with them. Operators also have associativity, which refers to the order in which the compiler will execute operations that have the same precedence. For example, the expression num1 + num2 - num3 includes two operators that have the same precedence--the addition and the subtraction operator. The two operator have left to right associativity, so the computer will begin with the leftmost operator and then move to the right. In this case, the subtraction operator will follow the addition operator. Figure 1.5 shows the precedence and associativity of the operators discussed in this lecture.
Figure 1.5 Order of Precedence
Operator Symbol(s) |
Order of Precedence |
Associativity |
( ) |
First |
Left to Right |
- |
Second |
Right to Left |
* / % |
Third |
Left to Right |
+ - |
Fourth |
Left to Right |
= += -= *= /= %= |
Fifth |
Right to Left |