C++ Tutorial

C++ Tutorial 

C++ tutorial give us basic and advanced knowledge  of C++. Our C++ tutorial is very simple , and very short program example with definition,  any body can learn  beginners or professionals.

C++ is an object-oriented programming language. But not true object programming language, because it has data type like int, float char etc.

48 reserve keyboard inside c++, these special keyboard , we don’t use as variable.

Which language save every thing in object, that language is truly object truly object-oriented programming, Smalltak was first trule object language

What is C++

C++ is a general purpose, case-sensitive, free-form programming language that supports object-oriented, procedural and generic programming. This is base of every language , this come after c language, This is middle level language , it provide both feature high level and low-level

One main thing come in c++, only private concept was come. In c language , which data belong to structure , all data was public, we could make variable(object) of the structure from any where , we was access data from any where , that was the main problem in c language.

 

Object-Oriented Programming (OOPs)

C++ is a object-oriented programming, the four major This language  has important four feature  of object-oriented programming (OOPs). :

1.     Polymorphism

2.     Inheritance

3.     Abstraction

4.     Encapsulation

  These four feature make of c++ object-oriented programming.

#include<iostream.h>

#include<conio.h>

void main()

{

clrscr();

cout<<”welcome in c++”;

getch();

}

 

 

Object

Object is real world entity that has state and behavior is known as an object. For example: CPU, Mouse, Monitor, keyboard,  etc. It is a physical entity.

Class

Collection of objects, functions, data types . class is like a template, class is map, object is like building, we cannt make building without map. So this  is called class. It is a logical entity.

Inheritance

When an class get all the properties and behavior from parent class i.e. known as inheritance. It gives code reusability. It is used to achieve runtime polymorphism.

Polymorphism

One name many task  known as polymorphism. For example:

6+7=13

A+B=AB

In C++, we use Function overloading and Function overriding to achieve polymorphism.

Abstraction

Hiding internal details and showing external working is known as abstraction.

Example :- we donnt know how to work clutch,break inside the car

.

Encapsulation

Combine data together into a one unit is known as encapsulation. For example: capsule, etc.

Simple Program in C++:-

#include<iostream.h>

#include<conio.h>

class abc

{

        private:

        int a,b,c;

        public:

        void show()

        {

               cout<<"enter two values ";//we are using cout<< instead of printf

               cin>>a>>b;// we are using cin>>instead of scanf

               c=a+b;

               cout<<"sum of two vaiable"<<c;

        

        }

};

 void main()

{

        abc obj;//obj is object

        obj.show();//call show function with help of obj object

        getch();

       

}


Program to calculate area of rectangle using objects ans classes

#include<iostream>

#include<conio.h>

class rectangle

{

private:

 int a,b;

public:

 void setdata (int x,int  y)

{

a=x;

b=y;

}

void area()

{

int ar=a*b;

cout<<”\n  Area of rectangle =”<<ar;

}

};    //end of class specification

void main()

{

clrscr();

rectangle r1,r2;  //object definition

r1.setdata(5,10);    //object r1 calls setdata()

cout<<”Rectangle1”;

r1.area();    //object r1 calls area()

r2.setdata(10,20);      //object r2 calls setdata()

cout<<”\nRectangle 2”;

r2.area();     //object r2 calls area()

getch();


}


Program to calculate the area of rectangle by defining member function outside the class ?

#include<iostream>

using namespace std;

class rectangle

{

private:

int a,b;

public:

void setdata (int,int); // member function declaration

void area(); //member function declaration

};

void rectangle :: setdata(int x, int y)//member function definition

{

a=x;

b=y;

}

void rectangle :: area()     //member function definition

{

int  ar=a*b;

cout <<”\n Area of rectangle =” <<ar;

}

int main()

{

 clrscr ()

rectangle  r1,r2;

r1.setdata(5,10);

cout <<”Rectangle r1......”;

r1 . area();

r2 .setdata(10,20);

cout <<\nRectangle r2.....”;

r2 .area ();

           

return 0;

}

C++ classes as user-defined data type.

One of the important  feature of a class is its property to create user defined data type and objects which are instance of class represents variable of this user defined data type.

#include<iostream.h>

#include<conio.h>

#include<iomanip.h>

class circle

{

            private:

            double r;

            public:

            void input();

            void area();


};

void circle::input()

{

            cout<<"Enter radius of circle=";

            cin>>r;

}

void circle::area()

{

            double ar=3.1415*r*r;

             cout<<"Area of circle ="<<setprecision(2)<<ar;


}

void circle::circumference()

{

            double cir=3.1415*r*;

            cout<<"circumference of circle ="<<setprecision(2)<<cir;

}

void main()

{

            clrscr();

            circle c1;

            c1.input();

            c1.area();

            c1.circumference();

            getch();

}

RETURNING OBJECT FROM A FUNCTION


A function can return an object in a similar manner as that of returning a variable of basic data types from functions.When a functions returns and 

object, the return type of the function is the name of the class to which the object belongs and the normal return statement in the function is used

to return the object. This can be illustrated in the following program.


PROGRAM TO ADD TWO TIME ABJECTS BY RETURNING AN OBJECT FROM A FUNCTON:


#include<iostream.h>

#include<conio.h>

class time

{


 private:

 int hours,minutes;

 public:

 void read_time();

 time add_time(time);

 void show_time();

};


void time::read_time()

{

  cout<<"enter time in hours and minutes:";

  cin>>hours>>minutes;

}


time time::add_time(time tt2)

{

  time temp;

  int min=minutes + tt2.minutes;

  int hrs=min/60;

  temp.minutes=min%60;

  temp.hours=hrs + hours+tt2.hours;

  return temp;

}


void time::show_time()

{

  cout<<"time obtained on adding=";

  cout<<hours<<":"<<minutes;

}


int main()

{

  clrscr();

  time t1,t2,t3;

  t1.read_time();

  t2.read_time();

  t3=t1.add_time(t2);    //t3=t1+t2

  t3.show_time();

  getch();

  return 0;

}



OUTPUT:


enter time in hours and minutes: 5 30

enter time in hours and minutes: 5 20

time obrained on adding= 11:20


  Program to demonstrate passing of an  objects by Refernce?

#include<iostream.h>

#include<conio.h>

class acc_info

{

private:

int acctno;float balance;

public:

void read();

void show();

void transfer_money(acc_info &,double);

};

void acc_info::read()

{

cout<<"enter the account number=";

cin>>acctno;

cout<<"enter the balance=";

cin>>balance;

}

void acc_info::show()

{

cout<<"\n account number is ="<<acctno;

cout<<"\n balance is ="<<balance;

}

void acc_info::transfer_money(acc_info &acct,double amt)

{

balance=balance-amt;

acct.balance=acct.balance+amt;

}

int main()

{

clrscr();

acc_info acct1,acct2;

cout<<"enter account information of acct1..\n";

acct1.read();

cout<<"enter account information of acct2..\n";

acct2.read();

cout<<"\n displaying account information before transfer..\n";

acct1.show();

acct2.show();

double money_trans;

cout<<"\n enter amount to transfer from acct1 to acct2=";

cin>>money_trans;

acct1.transfer_money(acct2,money_trans);

cout<<"displaying account information after transfer of money...";

acct1.show();

acct2.show();

getch();

return 0;

}

 

 

OUTPUT IS=

enter account information of acct1..

enter the account number=11

balance is =1000

enter account information of acct2..

enter the account number=22

balance is =2000

account number=11  Interest =0.05      Balance=1012.5

account number=22  Interest =0.05      Balance=2025






0 comments:

Post a Comment