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() { unsigned int n,fact; unsigned int factorial(unsigned int); //variable declaration //function declaration //beginning of main f()

cout<<"PROGRAM TO FIND A FACTORIAL OF ANY NUMBER:\n"; cout<<"\nEnter any no.="; cin>>n; fact=factorial(n); //standard output //standard input //function call //standard output //return type

cout<<"\nThe value of factorial no.="<<fact; return 0; } unsigned int factorial(unsigned int x) { unsigned int i,f=1; for(i=x;i>=1;i--) f=f*i; return f; }

//function defination

//variable declaration //for loop //operation //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() { int a,b; int cube(int); //declaration of variables //declaration of function //begining of main f()

cout<<"PROGRAM TO FIND VOLUME OF CUBE :\n"; cout<<"\nEnter any number="; cin>>a; b=cube(a); //standard output

//standard input //function call //standard output

cout<<"\nAfter cubing the result="; cout<<b; return 0; } int cube(int x) { int y; y=x*x*x; return y; } //declaration //operation //return type

//function defination

//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() { int a,b; void swap(int,int); //variable declaration //function declaration //beginning of main f()

cout<<"PROGRAM TO SWAP TWO NO'S USING CALL BY VALUE :\n\n"; cout<<"Enter the value of a="; cin>>a; //standard output

//standard input //standard output

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

//standard input

cout<<"\n\nBefore swapping the values are\n\n\ta="<<a; //standard output cout<<"\n\n\tb="<<b; swap(a,b); return 0; } void swap(int x,int y) { int z; z=x; x=y; ASHOKA COLLEGE OF COMPUTER EDUCATION //variable declaration //function defination //standard output //function call //return type

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() { int a,b; //variable declaration //function declaration //begining of main f()

void swap(int&,int&);

cout<<"PROGRAM TO SWAP TWO NO'S USING CALL BY REFERENCE:\n\n"; cout<<"Enter the value of a="; cin>>a; //standard output

//standard input //standard output

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

//standard input

cout<<"\nBefore swapping the result=\n\n\ta="<<a; cout<<"\n\n\tb="<<b; swap(a,b); return 0; } void swap(int&x,int&y) { int z; z=x; x=y; ASHOKA COLLEGE OF COMPUTER EDUCATION //variable declaration //function defination //function call //return type

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() { float n; //variable declaration //beginning of main f()

float si(float, float, float t=5); //function declaration cout<<"PROGRAM TO FIND SIMPLE INTEREST:"; n=si(2000,7); //f() call //standard output

cout<<"\n\nThe simple interest="; cout<<n; return 0; } float si(float x,float y,float z) { float a; a=x*y*z/100; return a; }

//standard output //return type

//formal parameter

//variable declaration //operation

//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 { char name[20]; int age; float per; }s1,s2; int main() { 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="; cin>>s1.name; 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="; //standard output //standard input //beginning of main f() //name of the structure

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 { int a,b,c; public: void read() { cout<<"PROGRAM TO DEMONSTRATE THE CONCEPT OF DATA ABSTRACTION :\n"; cout<<"\nEnter the value of a="; //standard output cin>>a; //standard input //member function //data member //name of class

cout<<"\nEnter the value of b="; //standard output cin>>b; } void process() { c=a+b; } void show() { ASHOKA COLLEGE OF COMPUTER EDUCATION //member f() //operation //member f() //standard input

By: Prabhat Kumar cout<<"\nAfter adding the result="; cout<<c; } }; int main() { ABC o1; o1.read(); o1.process(); o1.show(); return 0; } //return type //end of program //creation of object //invoke member f() //begining of main f()

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 { int a,b,c; public: void read() { cout<<"PROGRAM TO DEMONSTRATE THE CONCEPT OF DATA ENCAPSULATION :\n"; cout<<"\nEnter the value of a="; //standard output cin>>a; //standard input //member function //data member //name of class

cout<<"\nEnter the value of b="; //standard output cin>>b; } void process() { c=a+b; } void show() { //member f() //operation //member f() //standard input

ASHOKA COLLEGE OF COMPUTER EDUCATION

By: Prabhat Kumar cout<<"\nAfter adding the result="; cout<<c; } }; int main() { ABC o1; o1.read(); o1.process(); o1.show(); return 0; } //return type //end of program //creation of object //invoke member f() //begining of main f()

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 { int a,b; public: void read() { cout<<"\nEnter the value of a="; //standard output cin>>a; //standard input //member function //data member //name of class

cout<<"\nEnter the value of b="; //standard output cin>>b; } void show() { If(a>b) { cout<<"a is greater"; } else //if loop //member f() //standard input

ASHOKA COLLEGE OF COMPUTER EDUCATION

By: Prabhat Kumar { cout<<"\nb is greater"; } } }; int main() { ABC o1,o2; //creation of object //begining of main f() //operation

cout<<"PROGRAM TO DEMONSTRATE THE CONCEPT OF CLASSES AND OBJECTS:\n"; o1.read(); o1.show(); o2.read(); o2.show(); return 0; } //return type //end of program //invoke member f()

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 { int n; public: void read() { cout<<"\nEnter any number="; //standard output cin>>n; } void show() { if(n%2==0) { cout<<"\nThe number is even\n"; } else { cout<<"\nThe number is odd\n"; } //if loop //member f() //standard input //data member //access level //member function //name of class

ASHOKA COLLEGE OF COMPUTER EDUCATION

By: Prabhat Kumar } }; int main() { ABC o1,o2; //creation of object //beginning of main f()

cout<<"PROGRAM TO DEMONSTRATE THE CONCEPT OF CREATING OBJECTS:\n"; o1.read(); o1.show(); o2.read(); o2.show(); return 0; } //return type //end of program //invoke member f()

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 { int a,b; public: void read() { cout<<"PROGRAM TO ACCESS THE DATA MEMBER AND MEMBER FUNCTION :\n"; cout<<"\nEnter the value of a="; //standard output cin>>a; } void process() { b=a*a; } void show() { cout<<"\nAfter squaring the result="; cout<<b; } ASHOKA COLLEGE OF COMPUTER EDUCATION //member f() //operation //member f() //standard input //member function //data member //name of class

By: Prabhat Kumar }; int main() { ABC o1; o1.read(); o1.process(); o1.show(); return 0; } //return type //end of program //creation of object //invoke member f() //begining of main f()

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 { int itemno; float price; public: void read(int i,float p) { itemno=i; price=p; } void show() { cout<<"item no="<<itemno<<"\t"; cout<<"\tprice="<<price<<"\n"; } }; int main() { //begining of main f() //standard output //standard output //member f() //member f() //data members //name of class

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++) { cout<<i<<":"<<endl; cout<<"itemno="; cin>>itno; cout<<"price="; cin>>pri; 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 //standard input //for loop

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 { int hours; int minutes; public: void read(int h,int m) { hours=h; minutes=m; } void show() { cout<<"\nHours="<<hours<<"\t\t"<<"Minutes="<<minutes; //standard output } void add(time, time); }; void time::add(time t1, time t2) { int h1,m1; ASHOKA COLLEGE OF COMPUTER EDUCATION //f() declaration //member function //member function //temporary variable/argument //variable declaration //name of the class

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() { time t1,t2,t3; //creation of object //begining of main f()

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); t2.read(3,50); t3.add(t1,t2); t1.show(); t2.show(); cout<<"\n\nAfter adding hours and minutes are\n"; t3.show(); return 0; } //return type //end of program //invoking show f() //invoking read f()

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 { int feet; int inches; public: void read(int i,int j) { feet=i; inches=j; } void show() { cout<<"\nFeet="<<feet<<"\tInches="<<inches; //standard output } void add(distance,distance); }; void distance::add(distance d1,distance d2) { int f1,inc1; ASHOKA COLLEGE OF COMPUTER EDUCATION //f() declaration //member function //member function //temporary variable/argument //variable declaration //name of the class

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() { distance d1,d2,d3; //creation of object //begining of main f()

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); d2.read(3,9); d3.add(d1,d2); d1.show(); d2.show(); cout<<"\n\nAfter adding feet and inches are\n"; d3.show(); //invoking show f() //invoking read f()

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 { int a,b,c; public: void read() { cout<<"PROGRAM TO DEMONSTRATE THE CONCEPT OF INLINE FUNCTION:\n\n"; cout<<"Enter a="; cin>>a; cout<<"Enter b="; cin>>b; } void show(); }; inline void abc::show() { cout<<"After applying inline f()the values are"; //inline f() //standard output //standard input //data member //access level //member f() //name of class

ASHOKA COLLEGE OF COMPUTER EDUCATION

By: Prabhat Kumar cout<<"a="<<a<<" and "<<"b="<<b; } int main() { abc o1; o1.read(); o1.show(); return 0; } //return type //end of program //creation of object //invoking read f() //begining of main()

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 { int a,b,c; public: void input(); void process(); void output(); }; void add::input() { 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 //member f() defination //member function declaration //variable declaration //name of class

cout<<"\nEnter the value of b="; //standard output cin>>b; } void add::process() { c=a+b; //operation //member f() defination //standard input

ASHOKA COLLEGE OF COMPUTER EDUCATION

By: Prabhat Kumar } void add::output() { cout<<"\nAfter adding="; cout<<c; } int main() { add a1; a1.input(); a1.process(); a1.output(); return 0; } //return type //end of program //creation of object //invoking M f() //beginning of main() //standard output //member f() defination

//standard output

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 { int a,b; public: void read() { cout<<"PROGRAM TO DEMONSTRATE THE CONCEPT OF FRIEND FUNCTION:\n"; cout<<"\nEnter the value of a="; //standard output cin>>a; //standard input //data member //access level //member funnction //name of class

cout<<"\nEnter the value of b="; //standard output cin>>b; } friend int func(abc); }; int func(abc o2) { return(o2.a+o2.b); } //return object //function defination //friend function //standard input

ASHOKA COLLEGE OF COMPUTER EDUCATION

By: Prabhat Kumar

int main() { int c; abc o1; o1.read(); c=func(o1);

//beginning of main()

//declaration //creation of object //invoke M F() //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 { int feet; int inches; public: void read(int f, int i) { feet=f; inches=i; } void show() { cout<<"Feet="<<feet<<"\t"<<"Inches="<<inches<<endl; //standard output } friend distance sum(distance,distance); }; distance sum(distance dd1,distance dd2) { //friend f() //member f() //member f() //data member //name of class

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; } int main() { distance d1,d2,d3; //creation of object //begining of main() //returning object

cout<<"PROGRAM TO DEMONSTRATE THE CONCEPT OF RETURNING OBJECT:\n"; cout<<"\nBefore Adding feet and inches are:\n\n"; d1.read(5,9); 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 //invoking memberf()

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 { int a,b; public: abc() { a=4; b=3; } abc(int i,int j) { a=i; b=j; } void show() { cout<<"\nThe value of a="<<a; //standard output //member function //parameterised constructor //data member //access level //default constructor //name of class

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

ASHOKA COLLEGE OF COMPUTER EDUCATION

By: Prabhat Kumar }; int main() { cout<<"PROGRAM TO DEMNSTRATE THE CONCEPT OF DEFAULT&PARAMETERISED CONSTRUCTOR:"; abc o1; abc o2(5,15); cout<<"\n\nThe values of Default constructor are:\n"; o1.show(); //invoking member f() //creation of object //beginning of main f()

cout<<"\nThe values of Parameterised Constructor are:\n"; o2.show(); return 0; } OUTPUT: //return type //end of program

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 { public: alpha() { count++; //increment //standard output //constructor //name of class

cout<<"\nObject created:"<<"\n"<<count<<"\n"; } ~alpha() { cout<<"\nObject destroyed:"<<"\n"<<count<<"\n"; count--; } }; int main() { //begining of main() //decrement //destructor

//standard output

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 { int itemno; float price; static int count; public: void read(int i,float f) { itemno=i; price=f; count++; } void show() { cout<<"itemno="<<itemno<<"\t"; cout<<"price="<<price<<"\n"; } static void display() { //static member function //standard output //member function //static data member //access level //member function //data member //name of class

ASHOKA COLLEGE OF COMPUTER EDUCATION

By: Prabhat Kumar cout<<count; } };

int x::count=0; int main() { x o1,o2,o3; //creation of object //beginning of main f()

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); 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 //invoking member function

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 { int i,j; public: abc() { i=5; j=4; } abc(int a, int b) { i=a; j=b; } abc(abc&s) { i=s.i; j=s.j; } //copy constructor //parameterised constructor //variable declaration //member f() //default constructor //name of class

ASHOKA COLLEGE OF COMPUTER EDUCATION

By: Prabhat Kumar void display() { cout<<"\ti="<<i<<"\tj="<<j<<endl; //standard output } }; int main() { cout<<"PROGRAM TO DEMONSTRATE THE CONCEPT OF COPY CONSTRUCTOR:\n"; abc o1; abc o2(5,15); abc o3(o2); //abc o3=o2; //creation of object //begining of main f() //member f()

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 { private: int a; protected: int b; public: void read() { cout<<"\nEnter the value of a="; //standard output cin>>a; } void get() { cout<<"\nEnter the value of b="; cin>>b; } void show() { ASHOKA COLLEGE OF COMPUTER EDUCATION //standard input //member function //access level //data member //name of class

By: Prabhat Kumar cout<<a; } void put() { cout<<b; } }; class derive:public base { 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; //derive class

ASHOKA COLLEGE OF COMPUTER EDUCATION

By: Prabhat Kumar } }; int main() { cout<<"PROGRAM OF SINGLE INHERITANCE:\n\n"; derive d1; d1.get(); d1.scan(); d1.add(); d1.display(); return 0; } //return type //creation of object //invoking member f() //beginning of main f()

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 { protected: int a; public: void read() { cout<<"\nEnter a="; cin>>a; } void show() { cout<<a; } }; class base2 { protected: //name of class2 //standard output //standard input //member function //access level //data member //name of class

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() { cout<<"PROGRAM OF MULTIPLE INHERITANCE\n"; derive d1; d1.read(); d1.get(); d1.add(); d1.print(); return 0; } //return type //end of program //creation of object //invoking member f() //beginning of main f()

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 { protected: int rollno; char name[20]; public: void read() { cout<<"Roll no="; cin>>rollno; cout<<"Name="; cin>>name; } void show() { cout<<"The roll no="<<rollno<<"\t"; cout<<"The name="<<name; } }; ASHOKA COLLEGE OF COMPUTER EDUCATION //standard output //standard input //member function //access level //data member //name of class

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() { cout<<"PROGRAM TO DEMONSTRATE THE CONCEPT OF MULTILEVEL INHERITANCE:\n"; student_total t1; t1.read(); t1.cal(); t1.show(); return 0; } //return type //end of program //creation of object //beginning of main f()

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 { private: char name[20]; int rollno; public: void read() { cout<<"\nEnter rollno="; cin>>rollno; //STANDARD OUTPUT //MEMBER FUNCTION //ACCESS LEVEL //DATA MEMBER //NAME OF CLASS

//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() { cout<<"PROGRAM TO DEMONSTRATE THE CONCEPT OF HIRERACHICAL INHERITANCE:"; stud_marks s1; s1.input(); s1.cal(); s1.display(); stud_fitness f1; f1.read(); f1.show(); } //RETURN TYPE //CREATION OF OBJECT //INVOKING MEMBER FUNCTION //BEGINNING OF MAIN F()

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 { private: char name[20]; int rollno; public: void read() { cout<<"Enter roll no="; cin>>rollno; cout<<"Enter name="; cin>>name; } void show() { cout<<"The rollno="<<rollno<<"\t"; cout<<"The name="<<name; } }; //standard output //standard input //member function //access level //data member //name of class

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(); cout<<"Marks1="<<endl; cin>>marks[0]; cout<<"Marks2="<<endl; cin>>marks[1]; cout<<"Marks3="<<endl; cin>>marks[2]; } void show() { stud_basic::show(); //cout<<marks[0]<<endl; //cout<<marks[1]<<endl; //cout<<marks[2]<<endl; } }; class sports { //call show of base //call read of base

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() { cout<<"PROGRAM TO DEMONSTRATE THE CONCEPT OF HYBRID INHERITANCE\n\n"; stud_res s1; s1.stud_acd_marks::read(); s1.sports::read(); s1.cal(); s1.show(); return 0; } //return type //end of program //creation of object //beginning of main f()

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 { int count; public: abc() { count=0; } void show() { cout<<"\n\n\tThe count="; cout<<count; } void operator++() { count=count+1; } }; //standard output //member function //declaration //access level //name of class

int main() ASHOKA COLLEGE OF COMPUTER EDUCATION

By: Prabhat Kumar { cout<<"PROGRAM TO DEMONSTRATE THE CONCEPT OF UNARY OPERTATOR OVERLOADING:"; abc o1; ++o1; o1.show(); return 0; } //return type //end of program //unary overloading //invoking member f()

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 { int hours; int minutes; public: void read() { cout<<"Enter hours="; cin>>hours; //standard output //standard input //access level //member function //data member //name of class

cout<<"Enter minutes="; cin>>minutes; } void show() { cout<<"After adding hours and minutes :\nhours="<<hours; cout<<"\tminutes="<<minutes; } time operator+(time tt2) ASHOKA COLLEGE OF COMPUTER EDUCATION //standard output //member function

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; t1.read(); t2.read(); t3=t1+t2; t3.show(); return 0; } //return type //end of program //operator overloading //creation of object //invoking member f()

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() { cout<<"PROGRAM TO DEMOSTRATE THE CONCEPT OF FUNCTION OVERLOADING:\n"; int add(int,int); int add(int); float add(float,float); int add(int,int,int); int a,b,c,d,e,f; float g,h,i; cout<<"Enter the value of a="; cin>>a; //standard output //variable declaration //function declaration //beginning of main

//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); 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; } int add(int x) { 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 //function definition //return type //function call

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 { public: void display() { cout<<"\nDisplay base"<<"\n\n"; } virtual void show() { cout<<"Show base"<<"\n\n"; } }; class derive:public base { public: void display() { cout<<"Display derive"<<"\n\n"; } void show() //access level //derive class //virtual function //standard output //access level //member function //name of class

ASHOKA COLLEGE OF COMPUTER EDUCATION

By: Prabhat Kumar { cout<<"Show derive"; } }; int main() { cout<<"PROGRAM TO DEMONSTRATE THE CONCEPT OF VIRTUAL FUNCTION:\n"; base b; derive d; base *bptr; bptr=&b; bptr->display(); bptr->show(); bptr=&d; bptr->display(); bptr->show(); return 0; } //return type //end of program //creation of object //beginning of main

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 { t1 a; t2 b; public: test(t1 x,t2 y) { a=x; b=y; } void show() { cout<<"\n\nThe values are="; cout<<a<<"\t"<<b<<endl; } }; //standard output //standard output //member f() //access level //name of class

ASHOKA COLLEGE OF COMPUTER EDUCATION

By: Prabhat Kumar int main() { 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 //begining of main f()

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() { char name[20]; int rollno; cout<<"PROGRAM OF FILES and STREAMS\n"; cout<<"\nEnter ur name="; cin>>name; //standard output //standard input //variable declaration //begining of main f()

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"; cout<<"and\t Roll no="<<rollno; //standard output //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() { char a[20]; //variable declaration //beginning of main function

cout<<"PROGRAM TO DEMONSTRATE THE CONCEPT OF GET AND PUT:\n"; cout<<"enter ur name="; cin.get(a,20); cout.write(a,20); return 0; } //cout<< The name= <<a; //return type //end of program //standard output

ASHOKA COLLEGE OF COMPUTER EDUCATION

By: Prabhat Kumar OUTPUT:

ASHOKA COLLEGE OF COMPUTER EDUCATION

You might also like