Skip to main content

CPP Programming

 1. Program for declaring function inside the class, declaring the objects and calling functions


//Simple Program for declaring functions inside the class #include<iostream.h> #include<conio.h> class TestSimple { int x,y,s; //Three member variables public: void getdata() // function to store values on member variables { cout<<"\nEnter two numbers "; cin>>x>>y; } void sum() { s=x+y; // adding the values ov x and y } void putdata() { cout<<"\nSum of "<<x<<" and "<<y<<" is "<<s; // displaying the values of variables } }; void main() { TestSimple t; //Object declaration t.getdata(); // Function Call t.sum(); t.putdata(); getdata(); }
2. Program for declaring function outside the class

//Simple Program for functions outside the class #include<iostream.h> #include<conio.h> class TestSimple { int x,y,s; public: void getdata(); //Function Prototype or Function Declaration void sum(); //Function Prototype or Function Declaration
void putdata(); //Function Prototype or Function Declaration }; void TestSimple::getdata() //Function Definition { cout<<"\nEnter two numbers "; cin>>x>>y; } void TestSimple::sum() //Function Definition { s=x+y; } void TestSimple::putdata() //Function Definition { cout<<"\nSum of "<<x<<" and "<<y<<" is "<<s; } void main() { TestSimple t; //Object Declaration t.getdata(); //Calling Function t.sum(); t.putdata(); getch(); }
3. Program to use arguments in Functions
//Program to use arguments in function
#include<iostream.h> 
#include<conio.h> 
class TestArg 

        int a,b; 
        public: 
        void getdata(int,int); // function with arguments 
        void putdata(); 
}; 
void TestArg::getdata(int x, int y) 

        a=x; b=y; 

void TestArg::putdata() 

         cout<<"\nValue of A is "<<a<<endl; 
         cout<<"\nValue of B is "<<b<<endl; 

void main() 

        TestArg t; 
        t.getdata(3,7); // Calling the function with arguments 
        t.putdata(); 
        getch(); 
}

4. Example for Function Overloading and Polymorphism 

//Example of function overloading and polymorphism
#include<iostream.h>
#include<conio.h>
class test
{
public:
void display()
{
cout<<"\nCPP Programing";
}
void display(int x)
{
cout<<"\nSingle argument Integer Value X="<<x;
}
void display(int a,int b)
{
cout<<"\nDouble arguments Integer Values A="<<a<<" B="<<b;
}
void display(float x)
{
cout<<"\nFloat Value X= "<<x;
}
};
void main()
{
test t;
t.display(); //calls display function without arguments
t.display(3); //calls display function with one integer argument
t.display(5,6); //calls display function with two int arguments
t.display(2.3f);//calls display function with float argument
getch();
}

5. Overloading Area function that calculates area of different shapes.

#include<conio.h> #include<iostream.h> class TArea { public: void area(float); void area(int); void area(int,int); float area(float,float); }; void TArea::area(float r) { float a; a=3.14*r*r; cout<<"\nArea of circle is "<<a; } void TArea::area(int s) { cout<<"\nArea of square is "<<s*s; } void TArea::area(int h, int w) { cout<<"\nArea of rectangle is"<<h*w; } float TArea::area(float b,float h) { return (0.5*b*h); } void main() { TArea t; float a; t.area(2.4f); t.area(5); t.area(5,6); a=t.area(3.4f,5.5f); cout<<"\nArea of Tiangle "<<a; getch(); }

6. Example for using default constructor

// program for default constructor #include<conio.h> #include<iostream.h> class test { private: int n; public: test() { n=5; } void square(); }; void test::square() { cout<<"Square "<<n*n; } void main() { test t; t.square(); getch(); }

7. Example for using constructor with arguments

// program for constructor with arguments #include<conio.h> #include<iostream.h> class test { private: int n; public: test(int a)// constructor { n=a; } void cube(); }; void test::cube() { cout<<"\nCube "<<n*n*n; } void main() { test t(4); //constructor call test t1(5); //constructor call t.cube(); //function call t1.cube(); //function call getch(); }

8. Example for passing Object as argument to a function, Constructors are not used

//passing object as argument to function without using constructor #include<conio.h> #include<iostream.h> class test { private: int n; public: void getdata(int x); void sum(test ob); //object passed as argument-test ob; void display(); }; void test::getdata(int x) { n=x; } void test::sum(test ob) { n=n+ob.n; //n belongs to the calling object (t2) //ob.n belongs to the object passed as argument (t1) } void test::display() { cout<<"\n Value of n "<<n; } void main() { test t1,t2; t1.getdata(4); t1.display(); t2.getdata(5); t2.display(); t2.sum(t1); //t2 object calls sum t2.display(); getch(); }

9. Example for passing Object as argument to a function, Constructors are used

//passing object as argument - using constructors
#include<iostream.h>
#include<conio.h>
class test
{
private:
int x ;
public:
test(int n) //constructor to initalize objects
{
x=n;
}
void mul(test o) //object as argument (o=t1)
{
x=x*o.x;
}
void display()
{
cout<<"\n X ="<<x;
}
};
void main()
{
test t1(5);
test t2(6);
t1.display();
t2.display();
t2.mul(t1);   //t1 passed as argument
t2.display();
getch();
}

10. Example of Copy Constructor and overloading constructor

/* Example of copy constructor and constructor overloading because this
program has multiple constructors*/
#include<iostream.h>
#include<conio.h>
class test
{
int x;
public:
test();     //default constructor
test(int n);    //constructor with arguments
test(test &t); //copy constructor
void display(); //function to display values
};
test::test()
{
x=0;
}
test::test(int n)
{
x=n;
}
test::test(test &t)
{
x=t.x;
}
void test:: display()
{
cout<<"\n Value of x "<<x;
}
void main()
{
test t; //calling default constructor
test t1(4); //calling constructor with arg
t=t1; //call to copy constructor-equal to test t(t1)
t1.display();
t.display();
//test t2(t1); this statement also call copy constructor
//t2.display();
getch();
}


11. Program to define a Destructor and Dynamic Memory Allocation in constructor

#include<iostream.h>
#include<conio.h>
#include<string.h>
class TestStr
{
char *s;
int size;
public:
TestStr(char *);
~TestStr();
void dispString();
}
TestStr::TestStr(char *st) //constructor is defined
{
cout<<"\nConstructor is callled\n";
size=strlen(st);
s=new char(size+1);  //dynamic memory allocation
strcpy(s,st);
}
TestStr::~TestStr() //destructor is defined here
{
cout<<"\nDestructor is called\n";
delete s;
}
void TestStr::dispString()
{
cout<<"\nString : "<<s;
}
void main()
{
TestStr s1("CPP");
s1.dispString();
{
TestStr s2("Java");
s2.dispString();
}
getch();
}

12. Program to describe the use of Static variable

#include<iostream.h>
#include<conio.h>
class TestS
{
static int count;
public:
void increment();
void display();
};
int TestS::count;
void TestS::increment()
{
count++;
}
void TestS::display()
{
cout<<"\nCount : "<<count;
}
void main()
{
TestS t1,t2,t3;
clrscr();
t1.increment();
t1.display();
t2.increment();
t2.display();
t3.increment();
t3.display();
       getch();
}

13. Program to describe the use of Static function

#include<iostream.h>
#include<conio.h>
class TestS
{
static int count;
public:
void increment();
static void display();
};
int TestS::count;
void TestS::increment()
{
count++;
}
void TestS::display()
{
cout<<"\nCount : "<<count;
}
void main()
{
TestS t1,t2,t3;
clrscr();
t1.increment();
t1.display();
t2.increment();
t2.display();
t3.increment();
t3.display();
       getch();
}

14. Example for Friend Function

#include<iostream.h>
#include<conio.h>
class TestF
{
int x;
public:
void getdata(int a)
{
x=a;
}
friend void square(TestF s);
};
void square(TestF s) //Friend Function
{
cout<<"\tSquare is - "<<s.x*s.x;
}
void main()
{
* TestF f;
f.getdata(5);
square(f);  //calling friend function, no need of arguments
getch();
}

15. Program to overload Unary (-) Operator


//Overloading unary operator
#include<conio.h>
#include<iostream.h>
class TestOp
{
int x,y;
public:
void getdata(int,int);
void operator-();
void putdata();
};
void TestOp::getdata(int a,int b)
{
x=a;
y=b;
}
void TestOp::operator-() //Unary operator Overloading
{
x=-x;            //Negating the values
y=-y;
}
void TestOp::putdata()
{
cout<<"\nValue of X "<<x;
cout<<"\nValue of Y "<<y;
}
void main()
{
TestOp t;
t.getdata(-5,6);
t.putdata();
-t;
t.putdata();
getch();       
}

16. Program to Overload Binary (+) Operator-Constructors are used

//Overloading Binary operator-constructor is used
#include<conio.h>
#include<iostream.h>
class TestOp
{
int x;
public:
TestOp();
TestOp(int);
TestOp operator+(TestOp);
void putdata();
};
TestOp::TestOp()
{
}
TestOp::TestOp(int a)
{
x=a;
}
TestOp TestOp::operator+(TestOp t)
{
TestOp r;
r.x=x+t.x;
return r;
}
void TestOp::putdata()
{
cout<<"\nValue of X "<<x;
}
void main()
{
TestOp t1(4);
TestOp t2(5);
TestOp t3;
clrscr();
t3=t1+t2;            //calling operator overloading function
t3=t1.operator+(t2); //t1 calls operator overloading function and
t1.putdata();        //t2 is treated as an argument to the function
t2.putdata();        //t3=t1-t2 is similar to t3=t1.operator-(t2)
t3.putdata();
getch();
}

17. Program to overload Binary (-) Operator-

//Overloading Binary operator-constructor is not used #include<conio.h> #include<iostream.h> class TestOp { int x; public: void getdata (int); TestOp operator-(TestOp); void putdata(); }; void TestOp::getdata(int a) { x=a; } TestOp TestOp::operator-(TestOp t) //overloading Binary Operator { TestOp r; r.x=x-t.x; return r; } void TestOp::putdata() { cout<<"\nValue of X "<<x; } void main() { TestOp t1; TestOp t2; TestOp t3; clrscr(); t1.getdata(8); t2.getdata(7); t3=t1-t2; //calling operator overloading function //t3=t1.operator-(t2); //t1 calls operator overloading function and t1.putdata(); //t2 is treated as an argument to the function t2.putdata(); //t3=t1-t2 is similar to t3=t1.operator-(t2) t3.putdata(); getch(); }

18. Program to overload Unary Operator using Friend function

//Overloading Unary operator using Friend Function
#include<iostream.h>
#include<conio.h>
class testF
{
int x,y;
public:
void getdata(int a,int b);
void friend operator++(testF &t); //declaring friend function for operator 
void putdata();                   //overloading
};
void testF::getdata(int a,int b)
{
x=a;
y=b;
}
void operator++(testF &t)//overloading ++ unary operator
{
t.x++;
t.y++;
}
void testF::putdata()
{
cout<<"\nX = "<<x;
cout<<"\nY = "<<y;
}
void main()
{
testF f;
clrscr();
f.getdata(2,6);
f.putdata();
++f; //Call to overloded operator
f.putdata();
getch();
}

19. Program to overload Binary operator using Friend function

//Overloading Binary operator using Friend Function
#include<iostream.h>
#include<conio.h>
class testF
{
int x,y;
public:
void getdata(int a,int b);
testF friend operator*(testF &t1,testF &t2); //declaring friend function for 
void putdata();                              //operator overloading
};
void testF::getdata(int a,int b)
{
x=a;
y=b;
}
testF operator*(testF &t1,testF &t2)//overloading * binary operator
{
testF r;
r.x=t1.x*t2.x;
r.y=t1.y*t2.y;
return r;
}
void testF::putdata()
{
cout<<"\nX = "<<x;
cout<<"\nY = "<<y;
}
void main()
{
testF f1,f2,f3;
clrscr();
f1.getdata(3,7);
f2.getdata(2,6);
f1.putdata();
f2.putdata();
f3=f1*f2; //Call to overloded operator
f3.putdata();
getch();
}

20. Program for Single inheritance

//Single Inheritance #include<iostream.h> #include<conio.h> class Arithmatic { public: int square(int n) { return n*n; } int mul(int x,int y) { return x*y; } }; class test:public Arithmetic //inheriting class Arithmetic to class test { int x,y; public: test() { x=4; y=5; } void display() { cout<<"\nAnswer is"<<square(x)+square(y)+mul(2,mul(x,y)); //call to base (Arithmetic) class functions } }; void main() { test t; t.display(); cout<<"\nSquare is "<<t.square(5); //calling base class function getch(); //using derived class object }

21. Example of Multiple Inheritance

//Multiple Inheritance #include<conio.h> #include<iostream.h> class BaseA { public: int square(int x) { return x*x; } }; class BaseB { public: int mul(int x,int y) { return x*y; } }; class test:public BaseA,private BaseB //public inheritance for BaseA and Private for BaseB { //also try both as public int x,y; public: test(int a,int b) { x=a; y=b; } void display() { cout<<"\nResult is "<<square(x)+square(y)+mul(2,mul(x,y)); } }; void main() { test t(5,8); clrscr(); t.display(); //cout<<"\nMultiplication "<<t.mul(5,6);//can't be accessed because of private inheritane cout<<"\nSquare "<<t.square(5); getch(); }

22. Example of Multilevel Inheritance

//Multilevel Inheritance #include<conio.h> #include<iostream.h> class BaseA { public: int square(int x) { return x*x; } }; class BaseB:public BaseA { public: int mul(int x,int y) { return x*y; } }; class test:public BaseB //Multilevel inheritance { int x, y; public: test(int a,int b) { x=a; y=b; } void display() { cout<<"\nResult is "<<square(x)+square(y)+mul(2,mul(x,y)); } }; void main() { test t(5,8); clrscr(); t.display(); cout<<"\nMultiplication "<<t.mul(5,6);//Base class function is called using derived class object cout<<"\nSquare "<<t.square(5); getch(); }
23. Example of Hierarchical Inheritance

//Hierarchical Inheritance #include<iostream.h> #include<conio.h> class Base { public: int square(int); int mul(int,int); }; int Base::square(int a) { return a*a; } int Base::mul(int a,int b) { return a*b; } class TestA:public Base { public: void show(int,int); }; void TestA::show(int a,int b) { cout<<"\nResult 1 "<<square(a)+square(b)+mul(2,mul(a,b)); } class TestB:public Base { public: void display(int a,int b); }; void TestB::display(int a,int b) { cout<<"\nResult 2 "<<square(a)+square(b)+mul(2,mul(a,b)); } void main() { TestA A; A.show(3,5); TestB B; B.display(2,5); getch(); } 

24. Program to declare a constant member function.

#include<iostream.h>
#include<conio.h>
class TestC
{
int a;
public:
TestC();
void show() const;
};
TestC::TestC()
{
a=5;
}
void TestC::show() const
{
      //a=10; //This statement will show error because function cannot
cout<<"\nA = "<<a;    //alter the data.
}
void main()
{
TestC t;
t.show();
getch();
}

23. Program to give the example of a friend function common for multiple classes.

#include<conio.h>
#include<iostream.h>
class TestB;
class TestA
{
int x;
public:
void getdata(int a)
{
x=a;
}
friend void compare(TestA,TestB);
};
class TestB
{
int y;
public:
void getdata(int a)
{
y=a;
}
friend void compare(TestA,TestB);
};
void compare(TestA a,TestB b)
{
if(a.x>b.y)
{
cout<<"\nGreater "<<a.x;
}
else
{
cout<<"\nGreater "<<b.y;
}
}
void main()
{
TestA A;
TestB B;
A.getdata(5);
B.getdata(6);
compare(A,B);
getch();
}



24. Program to demonstrate the use pointer of object.

#include<conio.h> #include<iostream.h> class test { int x; public: void getdata(int n) { s=n; } void putdata() { cout<<"\nS = "<<s; } }; void main() { test t,*p; clrscr(); t.getdata(4); p=&t; p->putdata(); p->getdata(8); p->putdata(); getch(); }

24. program to demonstrate the use of pointer to object.

#include<conio.h>
#include<iostream.h>
class test
{
int x;
public:
void getdata(int n)
{
s=n;
}
void putdata()
{
cout<<"\nS = "<<s;
}
};
void main()
{
          test t,*p;
          clrscr();
          t.getdata(4);
          p=&t;
          p->putdata();
          p->getdata(8);
          p->putdata();
          getch();
}

25. A simple example of this pointer.

#include<conio.h>
#include<iostream.h>
class test
{
int x;
public:
void getdata(int x)
{
this->x=x;
}
void putdata()
{
cout<<"\nS = "<<x;
}
};
void main()
{
test t;
clrscr();
t.getdata(4);
t.putdata();
getch();
}

25. An example of pure virtual function

#include<conio.h> #include<iostream.h> class Base { public: void virtual display()=0; }; class Derived:public Base { public: void display() { cout<<"\nDerived Class Display Function"; } }; void main() { Base *B; //Base b;//can not be used Derived d; B=&d; B->display(); getch(); }

25. Another Example of this pointer.

#include<iostream.h>
#include<conio.h>
class test
{
int x;
public:
void getdata(int n)
{
x=n;
}
test compare(test t)
{
if(t.x > x)
return t;
else
return *this;
}
void display()
{
cout<<"\nX = "<<x;
}
};
void main()
{
test t,t1,t2;
t.getdata(8);
t1.getdata(6);
t2=t.compare(t1);
t2.display();
     getch();
}

26. Base class Pointer example 


#include<conio.h> #include<iostream.h> class Base { public: void show() { cout<<"\nBase Class Function"; } }; class Derived: public Base { public: void show() { cout<<"\nDerived Class Function"; } }; void main() { Base *B; Base b; Derived *D; Derived d; clrscr(); cout<<"\nBase Class Pointer Base Calss Object"; B=&b; B->show(); cout<<"\nBase Class Pointer Derived Calss Object"; B=&b; B->show(); cout<<"\nDerived Class Pointer Base Calss Object"; D=&d; D->show(); cout<<"\nCalling Derived class function using Base Class Pointer"; ((Derived*)B)->show(); cout<<"\nCalling Base class function using Derived Class Pointer"; ((Base *)D)->show(); getch(); }

27. An example of Virtual Function.

#include<conio.h>
#include<iostream.h>
class Base
{
public:
void show()
{
cout<<"\nBase Class Show Funcion";
}
void virtual display()
{
cout<<"\nBase Class Display Function";
}
};
class Derived:public Base
{
public:
void show()
{
cout<<"\nDerived Class Show Function";
}
void display()
{
cout<<"\nDerived Class Display Function";
}
};
void main()
{
Base *B;
Base  b;
Derived d;
B=&b;
B->show();
B->display();
B=&d;
B->show();
B->display();
getch();
}

https://filehippo.com/download_dev-c/ -- Download Dev IDE for CPP

28. A Simple Example of Exception Handling

#include <iostream> 
using namespace std; 
  
int main() 

   int age = 15; 
  
   cout << "Before try \n"; 
   try

      cout << "Inside try \n"; 
      if (age < 18) 
      { 
         throw age; 
         cout << "After throw - the code never executed) \n"; 
      } 
   } 
   catch (int age )

      cout << "Exception Caught \n"; 
      cout <<"You are a Minor\n";
   } 
  
   cout << "After catch  the code will be executed) \n"; 
   return 0; 


29. Multiple catch blocks.

#include <iostream> 
using namespace std; 
  
int main() 

   int age = 25;   
   try 
   { 
      if (age > 18) 
      { 
         throw age; 
      } 
      else
      {
      throw 'm';
     }
   } 
   catch (int age ) 
   { 
       cout <<"You are "<<age<<" old and You are eligible";
   } 
   catch (char c)
   {
  cout<<"You are a Minor and You are not eligible" ;
   }
    return 0; 


30. Re-throwing Exception

#include<iostream>
using namespace std;
class Texcep
{
public:
void divide(double x,double y)
{
try
{
if(y==0.0)
{
throw y;
}
else
{
cout <<"\nDivision = "<<x/y<<"\n";
}
}
catch(double)
{
cout<<"\nDivisior cannot be zero";
throw;   //Throwing same exception again
}

}
};
int main()
{
Texcep t;
try
{
  t.divide(5,0.0) ;
  }
  catch(double) //catching the re-thrown exception
  {
  cout<<"\nRethrow - Exception Caught Again";
     }
return 0;
}

31. File Handling using constructors

#include<iostream.h>
#include<fstream.h>
#include<conio.h>
class File
{
char wdata[40],rdata[40];
public:
void write()
{
ofstream outf("data.txt");
cout<<"Enter a String withouut space : ";
cin>>wdata;
outf<<wdata<<"\n";
cout<<"Data has been Stored";
}
void read()

ifstream inf("data.txt");
inf>>rdata;
cout<<"\n\n"<<rdata;
cout<<"\nSuccessfully Read";
}
};
void main()
{
File f;
f.write();
getch();
f.read();
getch();
}

Comments

Post a Comment