| Type |
Size |
Example |
|
|
|
| char, unsigned char, signed char |
1 byte |
char cLetter='P'; //stores a character |
| short, unsigned short |
2 bytes |
int iCount=1; //16 bit integer |
| int, unsigned int |
4 bytes |
int iCount=12; //32 bit integer |
| long, unsigned long |
4 bytes |
int lCount=101; |
| float |
4 bytes |
float fTotal=10.011; |
| double |
8 bytes |
double dRate=1000.5491 |
| long double |
8 bytes |
double ldArea=100000.98345 |
syntax :
datatype variable_name;
example :
#include <iostream.h>
int main(int argc, char* argv[])
{
char cLetter='P';
int iCount=10;
float fTotal;
fTotal=100.15;
cout<<"cLetter = "<<cLetter<<endl;
cout<<"iCount = "<<iCount<<endl;
cout<<"fTotal = "<<fTotal<<endl;
return 0;
}
User defined data types
Class
A class type is a user-defined composite type. It is composed of "fields" or "members" that can have different types. In a class all members are private by default.
syntax :
class [tag [: base-list ]]
{
member-list
} [declarators];
[ class ] tag declarators;
example :
class CPerson
{
int iAge;
char cName[15];
public:
void getDetails();
};
Structure :
A structure type is a user-defined composite type. It is composed of "fields" or "members" that can have different types. In C++, a structure is the same as a class except that its members are public by default.
syntax :
struct [tag] { member-list } [declarators];
[struct] tag declarators;
example :
struct PERSON
{
int iAge;
float fWeight;
char cName[25];
} family_member;
Union :
Unions are class types that can contain only one data element at a time.
syntax :
union [tag] { member-list } [declarators];
[union] tag declarators;
example :
union NumericType
{
int iValue;
long lValue;
double dValue;
};
Usage
#include <iostream.h>
#include <string.h>
class CPerson
{
int iAge;
char cName[15];
public:
void getDetails();
void showDetails();
};
void CPerson::getDetails()
{
iAge=27;
strcpy(cName,"David Beckam");
}
void CPerson::showDetails()
{
cout<<"Age : "<<iAge<<endl;
cout<<"Name : "<<cName<<endl;
}
int main()
{
CPerson Person; //declaring a variable (object) of user defined type CPerson
Person.getDetails();
Person.showDetails();
return 0;
}
Program Execution Control Statements
if - else Statement
The if statement evaluates the expression enclosed in parentheses. If the expression evaluates to a nonzero value (true), the statement dependent on the evaluation is executed; otherwise, it is skipped.
syntax :
if( condition1 == true )
{
if( condition2 == true )
{
//do something
}
else
{
//do something
}
}
else
{
//do something
}
example :
#include <iostream.h>
int main()
{
int iCondition=0;
cout<<"Ener a condition\n";
cin>>iCondition;
if( iCondition==1)
cout<<"You entered 1\n";
else if( iCondition==2)
cout<<"You entered 2\n";
else
cout<<"Another value\n";
}
Switch Case Statement :
This control statement allows us to make a decision from a number of choices.
syntax :
switch (expression)
{
case constant 1:
[Block]
case constant 2:
[Block]
case constant n:
[Block]
default :
[Block]
}
example :
#include <iostream.h>
int main(int argc, char* argv[])
{
char cLetter;
cout<<"Enter a letter\n";
cin>>cLetter;
switch (cLetter)
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
cout<<"The Letter is a Vowel\n";
break;
default :
cout<<"The letter is not a Vowel\n";
break;
}
return 0;
}
Loops
while Statement
The while statement lets you repeat a statement until a specified expression becomes false.
syntax :
while ( expression ) statement
example :
#include <iostream.h>
int main()
{
int iCount=0;
while (iCount< 10 )
{
cout<<iCount++;
}
}
do while Statement
The expression in a do-while statement is evaluated after the body of the loop is executed. Therefore, the body of the loop is always executed at least once.
syntax :
do statement while ( expression ) ;
example :
int main()
{
int iCount=0;
do
{
cout<<iCount++;
}while(iCount<10 );
}
for Statement :
The for statement lets you repeat a statement or compound statement, a specified number of times.
syntax :
for ( init-expression; cond-expression ; loop-expression ) statement
example :
int main()
{
for(int iCount=0;iCount<10;iCount++)
{
cout<<iCount;
}
}