2. Basic C++|VC++ Syntax and Semantics
This section introduces you to the basic syntax and semantics of C++ programming language.


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 ";
     cin>>iCondition;
     if( iCondition==1)
        cout<<"You entered 1 ";
     else if( iCondition==2)
        cout<<"You entered 2 ";
     else
       cout<<"Another value ";
}

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 ";
     cin>>cLetter;
     switch (cLetter)
     {
        case 'a':
        case 'e':
        case 'i':
        case 'o':
        case 'u':
           cout<<"The Letter is a Vowel ";
           break;
        default :
           cout<<"The letter is not a Vowel ";
           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;
        }
}



   

Site optimized for IE6 and above. Copyright © 2010. Site Developed Using KTS WebCloud