You are on page 1of 88

By: Prabhat Kumar

ASHOKA COLLEGE OF COMPUTER EDUCATION


By: Prabhat Kumar

PROGRAM -1: WAP in C++ to find the factorial of a number using functions.

INPUT: Enter any number.

EXPLAINATION: Here in this program we declare two variables n and fact. To find the factorial of

a number we use data type i.e, unsigned int .

SOURCE CODE:

#include<iostream.h>

#include<conio.h>

int main() //beginning of main f()

unsigned int n,fact; //variable declaration

unsigned int factorial(unsigned int); //function declaration

cout<<"PROGRAM TO FIND A FACTORIAL OF ANY NUMBER:\n";

cout<<"\nEnter any no.="; //standard output

cin>>n; //standard input

fact=factorial(n); //function call

cout<<"\nThe value of factorial no.="<<fact; //standard output

return 0; //return type

unsigned int factorial(unsigned int x) //function defination

unsigned int i,f=1; //variable declaration

for(i=x;i>=1;i--) //for loop

f=f*i; //operation

return f; //return type

} / /end of program

ASHOKA COLLEGE OF COMPUTER EDUCATION


By: Prabhat Kumar

OUTPUT:

ASHOKA COLLEGE OF COMPUTER EDUCATION


By: Prabhat Kumar

PROGRAM-2: WAP in C++ to find the volume of cube using functions.

INPUT : Enter any number.

EXPLAINATION: Here in this program we declare two variables ‘a,b’ and one function i.e,cube . To find
the cube of a number we use formula i.e,cube=side*side*side .

SOURCE CODE:

#include<iostream.h>

#include<conio.h>

int main() //begining of main f()

int a,b; //declaration of variables

int cube(int); //declaration of function

cout<<"PROGRAM TO FIND VOLUME OF CUBE :\n";

cout<<"\nEnter any number="; //standard output

cin>>a; //standard input

b=cube(a); //function call

cout<<"\nAfter cubing the result="; //standard output

cout<<b;

return 0; //return type

int cube(int x) //function defination

int y; //declaration

y=x*x*x; //operation

return y;

} //end of program

ASHOKA COLLEGE OF COMPUTER EDUCATION


By: Prabhat Kumar

OUTPUT:

ASHOKA COLLEGE OF COMPUTER EDUCATION


By: Prabhat Kumar

PROGRAM-3: WAP in C++ to swap two numbers using call by value.

INPUT : Enter the value of a and b.

EXPLAINATION: Swap means to interchange the values In this program we declare two variables a and
b. To swap two numbers we take another temperory variable i.e, z in user defined function.

SOURCE CODE:

#include<iostream.h>

#include<conio.h>

int main() //beginning of main f()

int a,b; //variable declaration

void swap(int,int); //function declaration

cout<<"PROGRAM TO SWAP TWO NO'S USING CALL BY VALUE :\n\n";

cout<<"Enter the value of a="; //standard output

cin>>a; //standard input

cout<<"Enter the value of b="; //standard output

cin>>b; //standard input

cout<<"\n\nBefore swapping the values are\n\n\ta="<<a; //standard output

cout<<"\n\n\tb="<<b; //standard output

swap(a,b); //function call

return 0; //return type

void swap(int x,int y) //function defination

int z; //variable declaration

z=x;

x=y;

ASHOKA COLLEGE OF COMPUTER EDUCATION


By: Prabhat Kumar

y=z;

cout<<"\n\nAfter swapping the values are\n\n\ta="<<x; //standard output

cout<<"\n\n\tb="<<y;

//end of program

OUTPUT:

ASHOKA COLLEGE OF COMPUTER EDUCATION


By: Prabhat Kumar

PROGRAM-4: WAP in C++ to swap two numbers using call by reference.

INPUT : Enter the value of a and b.

EXPLAINATION: Refenence means address(&) .In this program we declare two variables a and b. To
swap two numbers by call by reference we take another temperory variable i.e, z in user defined f().

SOURCE CODE:

#include<iostream.h>

#include<conio.h>

int main() //begining of main f()

int a,b; //variable declaration

void swap(int&,int&); //function declaration

cout<<"PROGRAM TO SWAP TWO NO'S USING CALL BY REFERENCE:\n\n";

cout<<"Enter the value of a="; //standard output

cin>>a; //standard input

cout<<"Enter the value of b="; //standard output

cin>>b; //standard input

cout<<"\nBefore swapping the result=\n\n\ta="<<a;

cout<<"\n\n\tb="<<b;

swap(a,b); //function call

return 0; //return type

void swap(int&x,int&y) //function defination

int z; //variable declaration

z=x;

x=y;

ASHOKA COLLEGE OF COMPUTER EDUCATION


By: Prabhat Kumar

y=z;

cout<<"\nAfter swapping the result are:\n\n\ta="<<x;

cout<<"\n\n\tb="<<y;

} //end of program

OUTPUT:

ASHOKA COLLEGE OF COMPUTER EDUCATION


By: Prabhat Kumar

PROGRAM-5: WAP in C++ to find the simple interest using the concept of default arguments.

EXPLAINATION: In this program we declare variable ‘n’and function ‘si’and apply the formula of simple
interest i.e,S.I=(p*r*t)/100 in user defined f().

SOURCE CODE:

#include<iostream.h>

#include<conio.h>

int main() //beginning of main f()

float n; //variable declaration

float si(float, float, float t=5); //function declaration

cout<<"PROGRAM TO FIND SIMPLE INTEREST:";

n=si(2000,7); //f() call

cout<<"\n\nThe simple interest="; //standard output

cout<<n; //standard output

return 0; //return type

float si(float x,float y,float z) //formal parameter

float a; //variable declaration

a=x*y*z/100; //operation

return a;

} //end of program

ASHOKA COLLEGE OF COMPUTER EDUCATION


By: Prabhat Kumar

OUTPUT:

ASHOKA COLLEGE OF COMPUTER EDUCATION


By: Prabhat Kumar

PROGRAM-6: Wap to enter the name ,age and percentage.

INPUT : Enter any name, age and percentage.

EXPLAINATION:Structure is a collection of dissimiliar type of data elements. In this program the


structure name is student .We declare three variable i.e, name, age and per .We have to enter
the name ,age and per of two students.

SOURCE CODE:

#include<iostream.h>

#include<conio.h>

struct student //name of the structure

char name[20];

int age;

float per;

}s1,s2;

int main() //beginning of main f()

cout<<"PROGRAM TO ENTER THE NAME, AGE and PERCENTAGE USING STRUCTURE:\n\n";

cout<<"Enter the Name age and per of first person:"<<endl; //standard output

cout<<"Enter name="; //standard output

cin>>s1.name; //standard input

cout<<"\nEnter the age=";

cin>>s1.age;

cout<<"\nEnter the per=";

cin>>s1.per;

cout<<"\nEnter the Name age and per of second person:"<<endl;

cout<<"Enter name=";

ASHOKA COLLEGE OF COMPUTER EDUCATION


By: Prabhat Kumar

cin>>s2.name;

cout<<"\nEnter the age=";

cin>>s2.age;

cout<<"\nEnter the per=";

cin>>s2.per;

cout<<"\nThe name,age and per of s1:";

cout<<"\nName="<<s1.name<<"\n Age="<<s1.age<<"\n Per="<<s1.per;

cout<<"\nThe name,age and per of s2:";

cout<<"\nName="<<s2.name<<"\n Age="<<s2.age<<"\n Per="<<s2.per;

return 0; //return type

} //end of program

OUTPUT:

ASHOKA COLLEGE OF COMPUTER EDUCATION


By: Prabhat Kumar

PROGRAM-7: Wap to demonstrate the concept of data abstraction.

INPUT : Enter the value of a and b.

EXPLAINATION: Data abstraction means to know about the essential features by by hiding the
background details. In this program the class name is ABC .We declare three variables a,b,c and
member functions read, process and show.We have to add two numbers. In main f() we create
one object i.e,‘o1’

SOURCE CODE:

#include<iostream.h>

#include<conio.h>

class ABC //name of class

int a,b,c; //data member

public:

void read() //member function

cout<<"PROGRAM TO DEMONSTRATE THE CONCEPT OF DATA ABSTRACTION :\n";

cout<<"\nEnter the value of a="; //standard output

cin>>a; //standard input

cout<<"\nEnter the value of b="; //standard output

cin>>b; //standard input

void process() //member f()

c=a+b; //operation

void show() //member f()

ASHOKA COLLEGE OF COMPUTER EDUCATION


By: Prabhat Kumar

cout<<"\nAfter adding the result=";

cout<<c;

};

int main() //begining of main f()

ABC o1; //creation of object

o1.read(); //invoke member f()

o1.process();

o1.show();

return 0; //return type

} //end of program

OUTPUT:

ASHOKA COLLEGE OF COMPUTER EDUCATION


By: Prabhat Kumar

PROGRAM-8: Wap to demonstrate the concept of data encapsulation.

INPUT : Enter the value of a and b.

EXPLAINATION: Data encapsulation means the wrapping up of data and functions in a single unit(called
class). In this program the class name is ABC .We declare three data members a,b,c and member
functions read, process and show.We have to add two numbers. In main f() we create one object i.e,‘o1’

SOURCE CODE:

#include<iostream.h>

#include<conio.h>

class ABC //name of class

int a,b,c; //data member

public:

void read() //member function

cout<<"PROGRAM TO DEMONSTRATE THE CONCEPT OF DATA ENCAPSULATION :\n";

cout<<"\nEnter the value of a="; //standard output

cin>>a; //standard input

cout<<"\nEnter the value of b="; //standard output

cin>>b; //standard input

void process() //member f()

c=a+b; //operation

void show() //member f()

ASHOKA COLLEGE OF COMPUTER EDUCATION


By: Prabhat Kumar

cout<<"\nAfter adding the result=";

cout<<c;

};

int main() //begining of main f()

ABC o1; //creation of object

o1.read(); //invoke member f()

o1.process();

o1.show();

return 0; //return type

} //end of program

OUTPUT:

ASHOKA COLLEGE OF COMPUTER EDUCATION


By: Prabhat Kumar

PROGRAM-9: Wap to demonstrate the concept of classes and objects.

INPUT : Enter any number.

EXPLAINATION: In this program the class name is ABC .We declare two data members a,b and member
functions read and process.We have to show greater between two numbers. In main f() we create one
object i.e,‘o1’

SOURCE CODE:

#include<iostream.h>

#include<conio.h>

class ABC //name of class

int a,b; //data member

public:

void read() //member function

cout<<"\nEnter the value of a="; //standard output

cin>>a; //standard input

cout<<"\nEnter the value of b="; //standard output

cin>>b; //standard input

void show() //member f()

If(a>b) //if loop

cout<<"a is greater";

else

ASHOKA COLLEGE OF COMPUTER EDUCATION


By: Prabhat Kumar

cout<<"\nb is greater"; //operation

};

int main() //begining of main f()

ABC o1,o2; //creation of object

cout<<"PROGRAM TO DEMONSTRATE THE CONCEPT OF CLASSES AND OBJECTS:\n";

o1.read(); //invoke member f()

o1.show();

o2.read();

o2.show();

return 0; //return type

} //end of program

ASHOKA COLLEGE OF COMPUTER EDUCATION


By: Prabhat Kumar

OUTPUT:

ASHOKA COLLEGE OF COMPUTER EDUCATION


By: Prabhat Kumar

PROGRAM-10: WAP in C++ to demonstrate the concept of creating objects.

INPUT : Enter any number.

EXPLAINATION: In this program the class name is ABC .We declare data member i.e, ‘n’ and member
functions read and process.We have to show whether the number is even or odd. In main f() we create
two objects i.e,‘o1 and o2’

SOURCE CODE:

#include<iostream.h>

#include<conio.h>

class ABC //name of class

int n; //data member

public: //access level

void read() //member function

cout<<"\nEnter any number="; //standard output

cin>>n; //standard input

void show() //member f()

if(n%2==0) //if loop

cout<<"\nThe number is even\n";

else

cout<<"\nThe number is odd\n"; }

ASHOKA COLLEGE OF COMPUTER EDUCATION


By: Prabhat Kumar

};

int main() //beginning of main f()

ABC o1,o2; //creation of object

cout<<"PROGRAM TO DEMONSTRATE THE CONCEPT OF CREATING OBJECTS:\n";

o1.read(); //invoke member f()

o1.show();

o2.read();

o2.show();

return 0; //return type

} //end of program

OUTPUT:

ASHOKA COLLEGE OF COMPUTER EDUCATION


By: Prabhat Kumar

PROGRAM-11: WAP in C++ to access the data member and member function.

INPUT : Enter any value of a.

EXPLAINATION:Here class name is ABC,data members are a&b, member functions are read, process and
show. We have to find the square of a number.

SOURCE CODE:

#include<iostream.h>

#include<conio.h>

class ABC //name of class

int a,b; //data member

public:

void read() //member function

cout<<"PROGRAM TO ACCESS THE DATA MEMBER AND MEMBER FUNCTION :\n";

cout<<"\nEnter the value of a="; //standard output

cin>>a; //standard input

void process() //member f()

b=a*a; //operation

void show() //member f()

cout<<"\nAfter squaring the result=";

cout<<b;

ASHOKA COLLEGE OF COMPUTER EDUCATION


By: Prabhat Kumar

};

int main() //begining of main f()

ABC o1; //creation of object

o1.read(); //invoke member f()

o1.process();

o1.show();

return 0; //return type

} //end of program

OUTPUT:

ASHOKA COLLEGE OF COMPUTER EDUCATION


By: Prabhat Kumar

PROGRAM-12: WAP in C++ to demonstrate the concept of array of object.

INPUT : Enter any value of five item no’s and price.

EXPLAINATION:Array is a collection of similar type of data elements.In this program the class name is
‘item’.We declare data member i.e,itemno &price and member function read&show .We have to show
item no.and price of five items. In main f() we take array of object o1[5].

SOURCE CODE:

#include<iostream.h>

#include<conio.h>

class item //name of class

int itemno; //data members

float price;

public:

void read(int i,float p) //member f()

itemno=i;

price=p;

void show() //member f()

cout<<"item no="<<itemno<<"\t"; //standard output

cout<<"\tprice="<<price<<"\n"; //standard output

};

int main() //begining of main f()

ASHOKA COLLEGE OF COMPUTER EDUCATION


By: Prabhat Kumar

int itno;

float pri;

item o1[5]; //array of object

cout<<"PROGRAM TO DEMONSTRATE THE CONCEPT OF ARRAY OF OBJECT:\n";

cout<<"The item no and price of Five items are\n";

for(int i=0;i<5;i++) //for loop

cout<<i<<":"<<endl;

cout<<"itemno=";

cin>>itno;

cout<<"price=";

cin>>pri; //standard input

o1[i].read(itno,pri);

cout<<"\nAll the item no and price of Five items are\n\n";

for(i=0;i<5;i++)

o1[i].show();

return 0; //return type

} //end of program

ASHOKA COLLEGE OF COMPUTER EDUCATION


By: Prabhat Kumar

OUTPUT:

ASHOKA COLLEGE OF COMPUTER EDUCATION


By: Prabhat Kumar

PROGRAM-13: WAP in C++ to add two diff. minutes and hours using object as f() argument.

INPUT : Enter any value of hours and minutes.

EXPLAINATION: Here the class name is time, data member are hours and minutes, member functions
are read &show. In this program we have to pass object as argument in place of values.

SOURCE CODE:

#include<iostream.h>

#include<conio.h>

class time //name of the class

int hours; //variable declaration

int minutes;

public: //member function

void read(int h,int m) //temporary variable/argument

hours=h;

minutes=m;

void show() //member function

cout<<"\nHours="<<hours<<"\t\t"<<"Minutes="<<minutes; //standard output

void add(time, time); //f() declaration

};

void time::add(time t1, time t2)

int h1,m1;

ASHOKA COLLEGE OF COMPUTER EDUCATION


By: Prabhat Kumar

hours=t1.hours+t2.hours;

minutes=t1.minutes+t2.minutes;

h1=minutes/60;

m1=minutes%60;

hours=hours+h1;

minutes=m1;

int main() //begining of main f()

time t1,t2,t3; //creation of object

cout<<"PROGRAM TO ADD TWO diff. MINUTES & HOURS USING OBJECT AS F() ARGUMENT:";

cout<<"\nBefore adding hours and minutes are\n";

t1.read(5,40); //invoking read f()

t2.read(3,50);

t3.add(t1,t2);

t1.show(); //invoking show f()

t2.show();

cout<<"\n\nAfter adding hours and minutes are\n";

t3.show();

return 0; //return type

} //end of program

ASHOKA COLLEGE OF COMPUTER EDUCATION


By: Prabhat Kumar

OUTPUT:

ASHOKA COLLEGE OF COMPUTER EDUCATION


By: Prabhat Kumar

PROGRAM-14: WAP in C++ to add two english distances using object as f() argument.

INPUT : Enter any value of hours and minutes.

EXPLAINATION: Here the class name is distance, data members are feet and inches, member functions
are read &show. In this program we have to pass object as argument in place of values.

SOURCE CODE:

#include<iostream.h>

#include<conio.h>

class distance //name of the class

int feet; //variable declaration

int inches;

public: //member function

void read(int i,int j) //temporary variable/argument

feet=i;

inches=j;

void show() //member function

cout<<"\nFeet="<<feet<<"\tInches="<<inches; //standard output

void add(distance,distance); //f() declaration

};

void distance::add(distance d1,distance d2)

int f1,inc1;

ASHOKA COLLEGE OF COMPUTER EDUCATION


By: Prabhat Kumar

feet=d1.feet+d2.feet;

inches=d1.inches+d2.inches;

f1=inches/12;

inc1=feet%12;

feet=feet+f1;

inches=inc1;

int main() //begining of main f()

distance d1,d2,d3; //creation of object

cout<<"PROGRAM TO ADD TWO DISTANCES USING OBJECT AS F() ARGUMENT:\n\n";

cout<<"Before adding feet and inches are\n";

d1.read(5,11); //invoking read f()

d2.read(3,9);

d3.add(d1,d2);

d1.show(); //invoking show f()

d2.show();

cout<<"\n\nAfter adding feet and inches are\n";

d3.show();

return 0; //return type

} //end of program

ASHOKA COLLEGE OF COMPUTER EDUCATION


By: Prabhat Kumar

OUTPUT:

ASHOKA COLLEGE OF COMPUTER EDUCATION


By: Prabhat Kumar

PROGRAM-15: WAP in C++ to demonstrate the concept of inline function.

INPUT : Enter any value of a & b.

EXPLAINATION:Inline functions are used to remove the overheads by pasting the calculation

in every function.Here in this program we take abc as class name ,two access

levels private & public ,two member f()’s as read & show .

SOURCE CODE:

#include<iostream.h>

#include<conio.h>

class abc //name of class

int a,b,c; //data member

public: //access level

void read() //member f()

cout<<"PROGRAM TO DEMONSTRATE THE CONCEPT OF INLINE FUNCTION:\n\n";

cout<<"Enter a="; //standard output

cin>>a; //standard input

cout<<"Enter b=";

cin>>b;

void show();

};

inline void abc::show() //inline f()

cout<<"After applying inline f()the values are";

ASHOKA COLLEGE OF COMPUTER EDUCATION


By: Prabhat Kumar

cout<<"a="<<a<<" and "<<"b="<<b;

int main() //begining of main()

abc o1; //creation of object

o1.read(); //invoking read f()

o1.show();

return 0; //return type

} //end of program

OUTPUT:

ASHOKA COLLEGE OF COMPUTER EDUCATION


By: Prabhat Kumar

PROGRAM-16: WAP in C++ to demonstrate the defination of member f()'s of a class.

INPUT : Enter any value of a & b.

EXPLAINATION:Here in this program we have to add two numbers. Taking add as class name ,two
access levels private & public ,two member f()’s as read & show .

SOURCE CODE:

#include<iostream.h>

#include<conio.h>

class add //name of class

int a,b,c; //variable declaration

public:

void input(); //member function declaration

void process();

void output();

};

void add::input() //member f() defination

cout<<"PROGRAM TO DEMONSTRATE THE DEFINATION OF MEMBER F()'S OF A CLASS:\n";

cout<<"\nEnter the value of a="; //standard output

cin>>a; //standard input

cout<<"\nEnter the value of b="; //standard output

cin>>b; //standard input

void add::process() //member f() defination

c=a+b; //operation

ASHOKA COLLEGE OF COMPUTER EDUCATION


By: Prabhat Kumar

void add::output() //member f() defination

cout<<"\nAfter adding="; //standard output

cout<<c; //standard output

int main() //beginning of main()

add a1; //creation of object

a1.input(); //invoking M f()

a1.process();

a1.output();

return 0; //return type

} //end of program

OUTPUT:

ASHOKA COLLEGE OF COMPUTER EDUCATION


By: Prabhat Kumar

PROGRAM-17: WAP in C++ to demonstrate the concept of friend function.

INPUT : Enter any value of a & b.

EXPLAINATION: Friend function are used to access the data member.In this program the class name is
‘abc’.We declare data member i.e, a & b and member function read .We have to add two numbers. In
main f() we create one objects i.e,o1.

SOURCE CODE:

#include<iostream.h>

#include<conio.h>

class abc //name of class

int a,b; //data member

public: //access level

void read() //member funnction

cout<<"PROGRAM TO DEMONSTRATE THE CONCEPT OF FRIEND FUNCTION:\n";

cout<<"\nEnter the value of a="; //standard output

cin>>a; //standard input

cout<<"\nEnter the value of b="; //standard output

cin>>b; //standard input

friend int func(abc); //friend function

};

int func(abc o2) //function defination

return(o2.a+o2.b); //return object

ASHOKA COLLEGE OF COMPUTER EDUCATION


By: Prabhat Kumar

int main() //beginning of main()

int c; //declaration

abc o1; //creation of object

o1.read(); //invoke M F()

c=func(o1); //function call

cout<<"\nAfter adding the result=";

cout<<c;

return 0; //return type

} //end of program

OUTPUT:

ASHOKA COLLEGE OF COMPUTER EDUCATION


By: Prabhat Kumar

PROGRAM-18: WAP in C++ to demonstrate the concept of returning object.

INPUT : Enter any two feet and inches.

EXPLAINATION: Here the class name is distance, data members are feet and inches, member functions
are read &show. In this program we have to pass object as argument in place of values and returning
object.

SOURCE CODE:

#include<iostream.h>

#include<conio.h>

class distance //name of class

int feet; //data member

int inches;

public:

void read(int f, int i) //member f()

feet=f;

inches=i;

void show() //member f()

cout<<"Feet="<<feet<<"\t"<<"Inches="<<inches<<endl; //standard output

friend distance sum(distance,distance); //friend f()

};

distance sum(distance dd1,distance dd2)

ASHOKA COLLEGE OF COMPUTER EDUCATION


By: Prabhat Kumar

distance dd3;

dd3.feet=dd1.feet+dd2.feet;

dd3.inches=dd1.inches+dd2.inches;

if(dd3.inches>=12)

dd3.feet++;

dd3.inches=dd3.inches-12;

return dd3; //returning object

int main() //begining of main()

distance d1,d2,d3; //creation of object

cout<<"PROGRAM TO DEMONSTRATE THE CONCEPT OF RETURNING OBJECT:\n";

cout<<"\nBefore Adding feet and inches are:\n\n";

d1.read(5,9); //invoking memberf()

d2.read(3,9);

d3=sum(d1,d2);

d1.show();

d2.show();

cout<<"\nAfter Adding feet and inches are:\n\n";

d3.show();

return 0; //return type

} //end of program

ASHOKA COLLEGE OF COMPUTER EDUCATION


By: Prabhat Kumar

OUTPUT:

ASHOKA COLLEGE OF COMPUTER EDUCATION


By: Prabhat Kumar

PROGRAM-19: WAP in C++ to demonstrate the concept of default and parameterised constructor.

EXPLAINATION:Constructor are special type of member functions. Constructors are always public. Here
the class name is abc,one default constructor,one parameterised constructor we have just to show the
values.

SOURCE CODE:

#include<iostream.h>

#include<conio.h>

class abc //name of class

int a,b; //data member

public: //access level

abc() //default constructor

a=4;

b=3;

abc(int i,int j) //parameterised constructor

a=i;

b=j;

void show() //member function

cout<<"\nThe value of a="<<a; //standard output

cout<<"\nThe value of b="<<b<<"\n\n"; //standard output

ASHOKA COLLEGE OF COMPUTER EDUCATION


By: Prabhat Kumar

};

int main() //beginning of main f()

cout<<"PROGRAM TO DEMNSTRATE THE CONCEPT OF DEFAULT&PARAMETERISED CONSTRUCTOR:";

abc o1; //creation of object

abc o2(5,15);

cout<<"\n\nThe values of Default constructor are:\n";

o1.show(); //invoking member f()

cout<<"\nThe values of Parameterised Constructor are:\n";

o2.show();

return 0; //return type

} //end of program

OUTPUT:

ASHOKA COLLEGE OF COMPUTER EDUCATION


By: Prabhat Kumar

PROGRAM-20: WAP in C++ to demonstrate the concept of destructors.

EXPLAINATION: Destructors are also special type of member functions. Here the class name is alpha,

Constructor nameis alpha() and destructor name is ~alpha().

SOURCE CODE:

#include<iostream.h>

#include<conio.h>

int count=0;

class alpha //name of class

public:

alpha() //constructor

count++; //increment

cout<<"\nObject created:"<<"\n"<<count<<"\n"; //standard output

~alpha() //destructor

cout<<"\nObject destroyed:"<<"\n"<<count<<"\n"; //standard output

count--; //decrement

};

int main() //begining of main()

cout<<"PROGRAM TO DEMONSTRATE THE CONCEPT OF DESTRUCTOR:\n\n";

alpha a1; //creation of object

ASHOKA COLLEGE OF COMPUTER EDUCATION


By: Prabhat Kumar

alpha a2;

//destructor invoke automatically

return 0; //return type

} //end of program

OUTPUT:

ASHOKA COLLEGE OF COMPUTER EDUCATION


By: Prabhat Kumar

PROGRAM-21: WAP to count the no. of object in a program by using static data member and static
member f().

EXPLAINATION: Static member functions are those functions which contain only static data members
and static member functions . Here in this program we have to count the no. of objects in a program.

SOURCE CODE:

#include<iostream.h>

#include<conio.h>

class x //name of class

int itemno; //data member

float price;

static int count; //static data member

public: //access level

void read(int i,float f) //member function

itemno=i;

price=f;

count++;

void show() //member function

cout<<"itemno="<<itemno<<"\t"; //standard output

cout<<"price="<<price<<"\n";

static void display() //static member function

ASHOKA COLLEGE OF COMPUTER EDUCATION


By: Prabhat Kumar

cout<<count;

};

int x::count=0;

int main() //beginning of main f()

x o1,o2,o3; //creation of object

cout<<"PROGRAM TO COUNT NO. OF OBJECT IN A PROGRAM BY USING D.M.& S.M.F():\n";

cout<<"\nThe item no and price are\n\n";

o1.read(5,12.5); //invoking member function

o2.read(7,13.4);

o3.read(2,3.5);

o1.show();

o2.show();

o3.show();

cout<<"\n\nand COUNT=";

x::display();

return 0; //return type

} //end of program

ASHOKA COLLEGE OF COMPUTER EDUCATION


By: Prabhat Kumar

OUTPUT:

ASHOKA COLLEGE OF COMPUTER EDUCATION


By: Prabhat Kumar

PROGRAM-22: WAP in c++ to demonstrate the concept of copy constructor.

EXPLAINATION: Copy constructor copies values from main function to member function.Here the class
name is abc.In this program we use three constructors one is default constructor,second is
parameterised constructor and third is copy constructor.

SOURCE CODE:

#include<iostream.h>

#include<conio.h>

class abc //name of class

int i,j; //variable declaration

public: //member f()

abc() //default constructor

i=5;

j=4;

abc(int a, int b) //parameterised constructor

i=a;

j=b;

abc(abc&s) //copy constructor

i=s.i;

j=s.j;

ASHOKA COLLEGE OF COMPUTER EDUCATION


By: Prabhat Kumar

void display() //member f()

cout<<"\ti="<<i<<"\tj="<<j<<endl; //standard output

};

int main() //begining of main f()

cout<<"PROGRAM TO DEMONSTRATE THE CONCEPT OF COPY CONSTRUCTOR:\n";

abc o1; //creation of object

abc o2(5,15);

abc o3(o2); //abc o3=o2;

cout<<"\nThe values of Default Constructor are:\n\n";

o1.display();

cout<<"\nThe values of Parameterised Constructor are:\n\n";

o2.display();

cout<<"\nThe values of Copy Constructor are:\n\n";

o3.display();

return 0; //return type

} //end of program

ASHOKA COLLEGE OF COMPUTER EDUCATION


By: Prabhat Kumar

OUTPUT:

ASHOKA COLLEGE OF COMPUTER EDUCATION


By: Prabhat Kumar

PROGRAM-23: WAP in c++ to demonstrate the concept of single inheritance.

INPUT : Enter any two numbers.

EXPLAINATION: When a derived class inherits the features of base class,it is single inheritance. Here we
take two classes one is base and other is derived class.We have to add two numbers.

SOURCE CODE:

#include<iostream.h>

#include<conio.h>

class base //name of class

private: //access level

int a; //data member

protected:

int b;

public:

void read() //member function

cout<<"\nEnter the value of a="; //standard output

cin>>a; //standard input

void get()

cout<<"\nEnter the value of b=";

cin>>b;

void show()

ASHOKA COLLEGE OF COMPUTER EDUCATION


By: Prabhat Kumar

cout<<a;

void put()

cout<<b;

};

class derive:public base //derive class

private:

int d,e;

public:

void scan()

cout<<"\nEnter the value of d=";

cin>>d;

void add()

e=d+b;

void display()

cout<<"\nAfter adding the result=";

cout<<e;

ASHOKA COLLEGE OF COMPUTER EDUCATION


By: Prabhat Kumar

};

int main() //beginning of main f()

cout<<"PROGRAM OF SINGLE INHERITANCE:\n\n";

derive d1; //creation of object

d1.get(); //invoking member f()

d1.scan();

d1.add();

d1.display();

return 0; //return type

OUTPUT:

ASHOKA COLLEGE OF COMPUTER EDUCATION


By: Prabhat Kumar

PROGRAM-24: WAP in c++ to demonstrate the concept of multiple inheritance.

INPUT : Enter the value of a & b.

EXPLAINATION: When a derived class inherits the features of more than one base class then it is
multiple inheritance. Here we take two base classes and one derived class. In this program we have to
add two numbers.

SOURCE CODE:

#include<iostream.h>

#include<conio.h>

class base1 //name of class

protected: //access level

int a; //data member

public:

void read() //member function

cout<<"\nEnter a="; //standard output

cin>>a; //standard input

void show()

cout<<a;

};

class base2 //name of class2

protected:

ASHOKA COLLEGE OF COMPUTER EDUCATION


By: Prabhat Kumar

int b;

public:

void get()

cout<<"\nEnter b=";

cin>>b;

void put()

cout<<b;

};

class derive:public base1,public base2

private:

int c;

public:

void add()

c=a+b;

void print()

cout<<"\n\nAfter adding=";

cout<<c;

ASHOKA COLLEGE OF COMPUTER EDUCATION


By: Prabhat Kumar

};

int main() //beginning of main f()

cout<<"PROGRAM OF MULTIPLE INHERITANCE\n";

derive d1; //creation of object

d1.read(); //invoking member f()

d1.get();

d1.add();

d1.print();

return 0; //return type

} //end of program

OUTPUT:

ASHOKA COLLEGE OF COMPUTER EDUCATION


By: Prabhat Kumar

PROGRAM-25: WAP in C++ to demonstrate the concept of multilevel inheritance.

INPUT : Enter the value of a & b.

EXPLAINATION:The transitive nature of inheritance is reflected by this form of inheritance. When a


subclass inherits from a class that itself inherits from another class, it is known as multilevel inheritance.

SOURCE CODE:

#include<iostream.h>

#include<conio.h>

class student //name of class

protected: //access level

int rollno; //data member

char name[20];

public:

void read() //member function

cout<<"Roll no="; //standard output

cin>>rollno; //standard input

cout<<"Name=";

cin>>name;

void show()

cout<<"The roll no="<<rollno<<"\t";

cout<<"The name="<<name;

};

ASHOKA COLLEGE OF COMPUTER EDUCATION


By: Prabhat Kumar

class student_details:public student

protected:

int marks[3];

public:

void read()

student::read();

cout<<"Marks[0]=";

cin>>marks[0];

cout<<"Marks[1]=";

cin>>marks[1];

cout<<"Marks[2]=";

cin>>marks[2];

void show()

student::show();

cout<<marks[0]<<marks[1]<<marks[2];

};

class student_total:public student_details

private:

int total;

ASHOKA COLLEGE OF COMPUTER EDUCATION


By: Prabhat Kumar

public:

void read()

student_total::read();

void cal()

total=marks[0]+marks[1]+marks[2];

void show()

student_details::show();

cout<<"Total=";

cout<<total;

};

int main() //beginning of main f()

cout<<"PROGRAM TO DEMONSTRATE THE CONCEPT OF MULTILEVEL INHERITANCE:\n";

student_total t1; //creation of object

t1.read();

t1.cal();

t1.show();

return 0; //return type

} //end of program

ASHOKA COLLEGE OF COMPUTER EDUCATION


By: Prabhat Kumar

OUTPUT:

ASHOKA COLLEGE OF COMPUTER EDUCATION


By: Prabhat Kumar

PROGRAM-26: WAP in C++ to demonstrate the concept of hierarchical inheritance.

INPUT : Enter any roll no., name and three subject’s marks.

EXPLAINATION: When many subclasses inherit from a single base class, it is known as hirarchical
inheritance.

SOURCE CODE:

#include<iostream.h>

#include<conio.h>

class stud_basic //NAME OF CLASS

private: //ACCESS LEVEL

char name[20]; //DATA MEMBER

int rollno;

public:

void read() //MEMBER FUNCTION

cout<<"\nEnter rollno="; //STANDARD OUTPUT

cin>>rollno; //STANDARD INPUT

cout<<"Enter ur name=";

cin>>name;

void show()

cout<<"Roll no="<<rollno<<endl<<"Name="<<name;

};

class stud_marks:public stud_basic

ASHOKA COLLEGE OF COMPUTER EDUCATION


By: Prabhat Kumar

private:

float marks[30],marks_obt;

float per;

public:

void input()

stud_basic::read();

cout<<"Enter three different marks:"<<endl;

cout<<"Marks[0]=";

cin>>marks[0];

cout<<"Marks[1]=";

cin>>marks[1];

cout<<"Marks[2]=";

cin>>marks[2];

void display()

stud_basic::show();

cout<<"\nDifferent marks are"<<endl;

cout<<"Marks1="<<marks[0]<<endl<<"Marks2="<<marks[1]<<endl<<"Marks3="<<marks[2];

cout<<"\nMarks_obt ="<<marks_obt<<endl;

cout<<"Percentage="<<per;

void cal()

ASHOKA COLLEGE OF COMPUTER EDUCATION


By: Prabhat Kumar

marks_obt=marks[0]+marks[1]+marks[2];

per=float(marks_obt/300)*100;

};

class stud_fitness:public stud_basic

private:

float height,weight;

public:

void read()

stud_basic::read();

cout<<"Enter height and weight:"<<endl;

cout<<"Height=";

cin>>height;

cout<<"Weight=";

cin>>weight;

void show()

stud_basic::show();

cout<<"\nHeight and weight are:"<<endl;

cout<<"Height="<<height<<"\t"<<"Weight="<<weight;

ASHOKA COLLEGE OF COMPUTER EDUCATION


By: Prabhat Kumar

};

void main() //BEGINNING OF MAIN F()

cout<<"PROGRAM TO DEMONSTRATE THE CONCEPT OF HIRERACHICAL INHERITANCE:";

stud_marks s1; //CREATION OF OBJECT

s1.input(); //INVOKING MEMBER FUNCTION

s1.cal();

s1.display();

stud_fitness f1;

f1.read();

f1.show();

} //RETURN TYPE

OUTPUT:

ASHOKA COLLEGE OF COMPUTER EDUCATION


By: Prabhat Kumar

PROGRAM-27: WAP in C++ to demonstrate the concept of hybrid inheritance.

EXPLAINATION: When a derived class inherits the features from multiple base classes which again
inherits the featurs of a single base class then it is known as hybrid inheritance.Here we take four classes
,in this program we have to find the percentage of all the marks obtained.

SOURCE CODE:

#include<iostream.h>

#include<conio.h>

class stud_basic //name of class

private: //access level

char name[20]; //data member

int rollno;

public:

void read() //member function

cout<<"Enter roll no="; //standard output

cin>>rollno; //standard input

cout<<"Enter name=";

cin>>name;

void show()

cout<<"The rollno="<<rollno<<"\t";

cout<<"The name="<<name;

};

ASHOKA COLLEGE OF COMPUTER EDUCATION


By: Prabhat Kumar

class stud_acd_marks:public stud_basic

protected:

float marks[3];

public:

void read()

stud_basic::read(); //call read of base

cout<<"Marks1="<<endl;

cin>>marks[0];

cout<<"Marks2="<<endl;

cin>>marks[1];

cout<<"Marks3="<<endl;

cin>>marks[2];

void show()

stud_basic::show(); //call show of base

//cout<<marks[0]<<endl;

//cout<<marks[1]<<endl;

//cout<<marks[2]<<endl;

};

class sports

ASHOKA COLLEGE OF COMPUTER EDUCATION


By: Prabhat Kumar

protected:

float score;

public:

void read()

cout<<"Score="<<endl;

cin>>score;

void show()

//cout<<score;

};

class stud_res:public stud_acd_marks,public sports

private:

float marks_obt;

float per;

public:

void cal()

marks_obt=marks[0]+marks[1]+marks[2]+score;

per=(marks_obt/400)*100;

void show()

ASHOKA COLLEGE OF COMPUTER EDUCATION


By: Prabhat Kumar

stud_acd_marks::show();

sports::show();

cout<<endl<<"Marks_obt="<<marks_obt<<endl<<"Per="<<per<<endl;

};

int main() //beginning of main f()

cout<<"PROGRAM TO DEMONSTRATE THE CONCEPT OF HYBRID INHERITANCE\n\n";

stud_res s1; //creation of object

s1.stud_acd_marks::read();

s1.sports::read();

s1.cal();

s1.show();

return 0; //return type

} //end of program

ASHOKA COLLEGE OF COMPUTER EDUCATION


By: Prabhat Kumar

OUTPUT:

ASHOKA COLLEGE OF COMPUTER EDUCATION


By: Prabhat Kumar

PROGRAM-28: WAP in c++ to demonstrate the concept of unary operator overloading.

EXPLAINATION: Let us consider the unary plus operator. A plus operator when used as a unary,takes
just one operand. The given program shows how the unary plus operator is overloaded.

SOURCE CODE:

#include<iostream.h>

#include<conio.h>

class abc //name of class

int count; //declaration

public: //access level

abc()

count=0;

void show() //member function

cout<<"\n\n\tThe count="; //standard output

cout<<count;

void operator++()

count=count+1;

};

int main()

ASHOKA COLLEGE OF COMPUTER EDUCATION


By: Prabhat Kumar

cout<<"PROGRAM TO DEMONSTRATE THE CONCEPT OF UNARY OPERTATOR OVERLOADING:";

abc o1;

++o1; //unary overloading

o1.show(); //invoking member f()

return 0; //return type

} //end of program

OUTPUT:

ASHOKA COLLEGE OF COMPUTER EDUCATION


By: Prabhat Kumar

PROGRAM-29: WAP in C++ to demonstrate the concept of binary operator overloading.

INPUT : Enter any values of hours and minutes.

EXPLAINATION: Operator overloading means to do additional job to an operator a special function with
that operator called as operator overloading. The same mechanism of unary operator can be used to
overload a binary operator.The f() notation c=sum(a,b);can be replaced by a natural looking expression
c=a+b;

SOURCE CODE:

#include<iostream.h>

#include<conio.h>

class time //name of class

int hours; //data member

int minutes;

public: //access level

void read() //member function

cout<<"Enter hours="; //standard output

cin>>hours; //standard input

cout<<"Enter minutes=";

cin>>minutes;

void show() //member function

cout<<"After adding hours and minutes :\nhours="<<hours; //standard output

cout<<"\tminutes="<<minutes;

time operator+(time tt2)

ASHOKA COLLEGE OF COMPUTER EDUCATION


By: Prabhat Kumar

time tt3;

tt3.hours=hours+tt2.hours;

tt3.minutes=minutes+tt2.minutes;

if(tt3.minutes>=60)

tt3.hours++;

tt3.minutes=tt3.minutes-60;

return tt3;

};

int main()

cout<<"PROGRAM TO DEMONSTRATE THE CONCEPT OF BINARY OPERATOR OVERLOADING:\n\n";

time t1,t2,t3; //creation of object

t1.read(); //invoking member f()

t2.read();

t3=t1+t2; //operator overloading

t3.show();

return 0; //return type

} //end of program

ASHOKA COLLEGE OF COMPUTER EDUCATION


By: Prabhat Kumar

OUTPUT:

ASHOKA COLLEGE OF COMPUTER EDUCATION


By: Prabhat Kumar

Program-30: WAP in C++ to demonstrate the concept of function overloading.

INPUT : Enter any value of a,b,c,g,h.

Explaination: Overloading refers to the use of same thing for different purposes. C++ also permits
overloading of functions. This means that we can use the same function name to create functions that
perform a variety of different tasks, using the concept of function overloading. For e.g: In this program
an overloaded add() function handles different types of data as shown below:

Source Code:

#include<iostream.h>

#include<conio.h>

int main() //beginning of main

cout<<"PROGRAM TO DEMOSTRATE THE CONCEPT OF FUNCTION OVERLOADING:\n";

int add(int,int); //function declaration

int add(int);

float add(float,float);

int add(int,int,int);

int a,b,c,d,e,f; //variable declaration

float g,h,i;

cout<<"Enter the value of a="; //standard output

cin>>a; //standard input

cout<<"Enter the value of b=";

cin>>b;

cout<<"Enter the value of c=";

cin>>c;

cout<<"Enter the value of g=";

cin>>g;

cout<<"Enter the value of h=";

ASHOKA COLLEGE OF COMPUTER EDUCATION


By: Prabhat Kumar

cin>>h;

d=add(a); //function call

e=add(a,b);

f=add(a,b,c);

i=add(g,h);

cout<<"After adding the results are:::::::\n"<<endl;

cout<<"Function with one argument"<<endl<<"Input"<<endl<<"a="<<a<<endl<<"Result="<<d<<endl;

cout<<"Function with two argument


result="<<endl<<"Input"<<endl<<"a="<<a<<",b="<<b<<endl<<"Result="<<e<<endl;

cout<<"Function with three argument


result="<<endl<<"Input"<<endl<<"a="<<a<<",b="<<b<<",c="<<c<<endl<<"Result="<<f<<endl;

cout<<"Function with two floating argument


result="<<endl<<"Input"<<endl<<"g="<<g<<",h="<<h<<endl<<"Result="<<i;

return 0; //return type

int add(int x) //function definition

int y;

y=x+x;

return y;

int add(int x,int y)

int z;

z=x+y;

return z;

ASHOKA COLLEGE OF COMPUTER EDUCATION


By: Prabhat Kumar

int add(int p,int q,int r)

int s;

s=p+q+r;

return s;

float add(float m,float n)

float o;

o=m+n;

return o;

OUTPUT:

ASHOKA COLLEGE OF COMPUTER EDUCATION


By: Prabhat Kumar

PROGRAM-31: WAP in C++ to demonstrate the concept of virtual f()'s.

EXPLAINATION: How we do achieve polymorphism? It is achieved using what is known as virtual


functions. When we use the same f() name in both the base&derived classes, the f() in base class is
declared as virtual using the keyword virtual preceding its normal declaration.

SOURCE CODE:

#include<conio.h>

class base //name of class

public: //access level

void display() //member function

cout<<"\nDisplay base"<<"\n\n"; //standard output

virtual void show() //virtual function

cout<<"Show base"<<"\n\n";

};

class derive:public base //derive class

public: //access level

void display()

cout<<"Display derive"<<"\n\n";

void show()

ASHOKA COLLEGE OF COMPUTER EDUCATION


By: Prabhat Kumar

cout<<"Show derive";

};

int main() //beginning of main

cout<<"PROGRAM TO DEMONSTRATE THE CONCEPT OF VIRTUAL FUNCTION:\n";

base b; //creation of object

derive d;

base *bptr;

bptr=&b;

bptr->display();

bptr->show();

bptr=&d;

bptr->display();

bptr->show();

return 0; //return type

} //end of program

ASHOKA COLLEGE OF COMPUTER EDUCATION


By: Prabhat Kumar

OUTPUT:

ASHOKA COLLEGE OF COMPUTER EDUCATION


By: Prabhat Kumar

PROGRAM-32: WAP in C++ to demonstrate the concept of templates.

EXPLAINATION: Templates is a new concept which enable us to define generic programming. Generic
programming is an approach where generic types are used as parameters in algorithms so that they
work for a variety of suitable data types and data structure.

SOURCE CODE:

#include<iostream.h>

template<class t1,class t2>

class test //name of class

t1 a;

t2 b;

public: //access level

test(t1 x,t2 y)

a=x;

b=y;

void show() //member f()

cout<<"\n\nThe values are="; //standard output

cout<<a<<"\t"<<b<<endl; //standard output

};

ASHOKA COLLEGE OF COMPUTER EDUCATION


By: Prabhat Kumar

int main() //begining of main f()

cout<<"PROGRAM TO DEMONSTRATE THE CONCEPT OF TEMPLATES:\n";

test<float,int>t1(1.95,7);

test<float,char>t2(3.14,'a');

test<int,float>t3(7,9.5);

t1.show();

t2.show();

t3.show();

return 0; //return type

} //end of program

OUTPUT:

ASHOKA COLLEGE OF COMPUTER EDUCATION


By: Prabhat Kumar

PROGRAM-33: WAP in c++ to demonstrate the concept of files and streams.

INPUT : Enter ur name and roll no.

EXPLAINATION: Files are used to store the output of program.

SOURCE CODE:

#include<iostream.h>

#include<fstream.h>

int main() //begining of main f()

char name[20]; //variable declaration

int rollno;

cout<<"PROGRAM OF FILES and STREAMS\n";

cout<<"\nEnter ur name="; //standard output

cin>>name; //standard input

cout<<"\nEnter ur roll no=";

cin>>rollno;

ofstream out("student");

out<<name<<"\n";

out<<rollno;

out.close();

ifstream in("student");

in>>name;

in>>rollno;

in.close();

cout<<"\nName="<<name<<"\t"; //standard output

cout<<"and\t Roll no="<<rollno; //standard output

ASHOKA COLLEGE OF COMPUTER EDUCATION


By: Prabhat Kumar

return 0; //return type

} //end of program

OUTPUT:

ASHOKA COLLEGE OF COMPUTER EDUCATION


By: Prabhat Kumar

PROGRAM-34: WAP in C++ to demonstrate the concept of get and put.

INPUT : Enter ur name.

EXPLAINATION: The functions get() and put() are member functions of the file stream class fstream.get()
is used to read a single character from the file.put() is used to write a single character to the output file.

SOURCE CODE:

#include<iostream.h>

#include<conio.h>

int main() //beginning of main function

char a[20]; //variable declaration

cout<<"PROGRAM TO DEMONSTRATE THE CONCEPT OF GET AND PUT:\n";

cout<<"enter ur name="; //standard output

cin.get(a,20);

cout.write(a,20); //cout<<”The name=”<<a;

return 0; //return type

} //end of program

ASHOKA COLLEGE OF COMPUTER EDUCATION


By: Prabhat Kumar

OUTPUT:

ASHOKA COLLEGE OF COMPUTER EDUCATION

You might also like