C++ Introduction

 

C++ is an object oriented programming language developed by Bjarne Stroustrup in 1980 .

It is an extension of C and earlier name was ' Extension of c ' later , it was named as C++ .

It is an superset of C programming .


 

Properties of C++ / Object oriented programming : 


1. Class : Class is a special block of code which is executed using object .Class has its data members and member functions . Class keyword is used to declare a class .

2. Object : Object is a instance of class . It is an sub part of class .



In the above example , there are given laptop with brands 

Laptop is a main block i.e. class and its brands are its different objects It means object is an instance of class .


Object has two properties state and behaviour 


3. Data Abstraction : Data abstraction points to useful and essential information or data . The visibility of useful data and hiding un-useful data is called as data abstraction .

For example , we sends messages on email , watsapp , etc . We can able to see only message not process behind it . 

4. Polymorphism : The meaning of poly is many and meaning of morphism is forms .It is an ability to take more than many forms . We can create many forms of one thing this process is called as polymorphism .


5. Message Passing : It means the communication between objects , data or classes .


6. Encapsulations : The providing security to data like wrapper class called as encapsulation . 

For example , medicine capsule is one type of wrapper , it consists of small small particles that means it provides security to those small particles inside it .

7. Inheritance : The child class or derived class derives/inherits properties from parent class or base class is called as inheritance .

For example , We takes some features from our parents like color body features etc , this property is called as inheritance .

8. Data binding : The holding of data together is called as data binding .



Basic syntax for program in C++ :


#include<iostream.h>                  //this is header file consists of standard input and output stream . Stream means flow of data in a program .

Class class_name                        //class keyword used create new class 

{

private :                                       // access specifier , used to show how data is handled 

Variables;

 public :                                      // access specifier 

methods ;

};                                                // ; semocolon used to create object for class

void main()                              //this is main function and main execution of program starts from here . Void means null value which does'n stores any value

{


--------statements---------

getch();                                    //this is linker between input and output screen .It is used to show output 
}

 

Example :

#include<iostream.h>

void main()
{
cout<<"Welcome to Tprograming !";
getch();
}

 

cout() :

               This is statement used to print any message or output .

Example : 

#include<iostream.h>

void main()

{

int t=10; 

cout<<"Value of t is : "<<t<<endln;

getch();

}

 

cin :

This is statement used to accept value from user .

Example :

#include<iostream.h>

void main()

{

int t;

cin>>t;

cout<<"Value of t is : "<<t<<endln;

 getch();

}


Control Flow in C++ :

    1. if-else 
    2. nested if else
    3. while loop
    4. for loop
    5. nested for loop
    6. go to statement
    7. do-while loop
    8. break statement

 

1. is-else statement :-


We can execute multiple condition and take multiple decisions using if-else statement . When one condition or statement becomes true then output gets printed . 

Syntax :-


if(condition)
{
  statement;
}
else
{
  statement;
}

Example :-

Q. Write c++ program for maximum and smaller of two numbers .



#include<iostream.h>

void main()
{
int p,q;
cout<<"enter value for p and q :"<<endl;
cin>>p>>q;
if(p>q)
{
cout<<"p is greater than q\n";
}
else
{
cout<<"q is greater than p\n";
}
getch();
}
 
Output :-

enter value for q and q : 20

30

q is greater than p

Q. Write a program for if-else compound statement :



#include<iostream.h>

void main()
{
float years, secs;
cout<<"Enter your age in years: ";

cin>>years;

if(years <0)

{

cout<<"I am sorry\n";

cout<<"Age cannot be negative";

}

Else

{

secs = years 365 * 24 * 60 *60;

cout<<"You have lived for "<<secs<<" seconds";

}

Ouput :-

Enter your age in years: 18 seconds

You have lived for 5.67648e+08 seconds 


Nested if-else :

Used to execute multiple conditions at a time

Syntax : 

if(condition1)

     statement1;

else

     if(condition2)

          statement2;

    else

           if(condition3)

                   statement3;


Example :

#include<iostream.h>


Void main()

{

int first, second;

cout<<"Enter first:"

cin>>first

cout<<"Enter second:"

cin>>second:

if(first>=second)
         {
                if(first% second=0)
                   {
                          if(first==second)
                                        cout<<"They are same\n";
                          else
                                        cout<<"They are evenly divisible\n";
                   }
               else
                          cout<<"They are not evenly divisible\n";
          }
          else
                cout<<"The second is larger\n";
        }
}

Output :

            Enter first: 10


            Enter second: 2

            They are evenly divisible


 

           Enter first: 2


           Enter second: 10

           The second is larger


           

           Enter first: 10


           Enter second: 3

           They are not evenly divisible


         

           Enter first: 10


           Enter second: 10

           They are same



while loop :
         It is used for iteration . This loop executes until condition becomes true . 

Syntax :

while(condition)


{

statement;

increment/decrement;

}

Example :

Q. Write c++ program to print values from 1 to 10 using while loop


#include<iostream.h>

void main()

{

int t=1;

while(t<=10)

{

cout<<t<<" \t";

t++;

}

getch();

}

Output :

 1 2 3 4 5 6 7 8 9 10


 

for loop :


              It is also used for iteration or to use multiple values at a time and all the conditions , increments are written as one place .

Syntax :

for(variable=initialization;condition;increment/decrement)


{

statement;

}

Example :

Q.Write a c++ program to print 1 to 10 numbers using for loop.

#include<iostream.h>


void main()

{

int t;

for(t=1;t<=10;t++)

{

cout<<t<<" ";

}

getch();

}

Output :

 1 2 3 4 5 6 7 8 9 10 



Nested for loop :

        It is used to execute for loop multiple times 

Syntax :

for(variable1=initialization;condition1;increment/decrement)

{
    statement1;

    for(variable2=initialization;condition2;increment/decrement)
      {
            statement2;
      }
}

Example :

Q. Write a c++ program for printing two dimensional array or matrix using nested for loop . 



#define ROW 2
#define COL 2
#include<iostream.h>
void main()
{
int t,p,mat1[ROW][COL];
cout<<"enter values for mat1";
for(t=0;t<ROW;t++)
for(p=0;p<COL;p++)
cin>>"%d",&mat1[t][p]<<endl;
//printing matrix
cout<<"Matrix is %d ",mat1[t][p]<<endl;
getch();
}

switch/break case :

      It is used to execute different sections at a time with multiple conditions .

Syntax :

switch(condition)

{

case 1 :

     statement;

     break;

case 2 :

    statement ;

    break ;

defualt :

statements;

Example :

Q. Write c++ program for switch case in c++

#include<iostream.h>

void main() 

{

int s,t,p;

cout<<"enter any arithmetic operator (+,-,*,/) : ";

cin>>s;

cout<<"Enter value for t and p : ";

cin>>t>>p;

switch(s)

{

   case '+':

        cout<<t<<"+"<<p<<"="<<t+p;

        break;

  case '-':

        cout<<t<<"-"<<p<<"="<<t-p;

        break;

case '*':

        cout<<t<<"*"<<p<<"="<<t*p;

        break;

case '/':

        cout<<t<<"/"<<p<<"="<<t/p;

        break;

default:

         cout<<"no arithmetic operator found";

         break;

}  

getch();

}

Output :

enter any arithmetic operator : +

Enter value for t and p : 10

20

10+20=30


goto statement :

The go to statement is used to alter the normal sequence of program execution by transferring control to some other part of the program . We can jump to last condition 

Example :

Q. Write a c++ program for goto statement . 

#include<iostream.h>

void main()

{

int marks;

cout<<"Enter marks:"

cin>>marks;

if(marks <60)

go to end;

else

{

cout<<"Excellent Performance";

exit(1)

}

end:

cout<<"Poor performance",

}

Output :

Enter marks : 50

Poor performance