Professional Documents
Culture Documents
PROGRAM -1: WAP in C++ to find the factorial of a number using functions.
EXPLAINATION: Here in this program we declare two variables n and fact. To find the factorial of
SOURCE CODE:
#include<iostream.h>
#include<conio.h>
f=f*i; //operation
} / /end of program
OUTPUT:
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>
cout<<b;
int y; //declaration
y=x*x*x; //operation
return y;
} //end of program
OUTPUT:
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>
z=x;
x=y;
y=z;
cout<<"\n\n\tb="<<y;
//end of program
OUTPUT:
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>
cout<<"\n\n\tb="<<b;
z=x;
x=y;
y=z;
cout<<"\n\n\tb="<<y;
} //end of program
OUTPUT:
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>
a=x*y*z/100; //operation
return a;
} //end of program
OUTPUT:
SOURCE CODE:
#include<iostream.h>
#include<conio.h>
char name[20];
int age;
float per;
}s1,s2;
cout<<"Enter the Name age and per of first person:"<<endl; //standard output
cin>>s1.age;
cin>>s1.per;
cout<<"Enter name=";
cin>>s2.name;
cin>>s2.age;
cin>>s2.per;
} //end of program
OUTPUT:
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>
public:
c=a+b; //operation
cout<<c;
};
o1.process();
o1.show();
} //end of program
OUTPUT:
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>
public:
c=a+b; //operation
cout<<c;
};
o1.process();
o1.show();
} //end of program
OUTPUT:
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>
public:
cout<<"a is greater";
else
};
o1.show();
o2.read();
o2.show();
} //end of program
OUTPUT:
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>
else
};
o1.show();
o2.read();
o2.show();
} //end of program
OUTPUT:
PROGRAM-11: WAP in C++ to access the data member and member function.
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>
public:
b=a*a; //operation
cout<<b;
};
o1.process();
o1.show();
} //end of program
OUTPUT:
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>
float price;
public:
itemno=i;
price=p;
};
int itno;
float pri;
cout<<i<<":"<<endl;
cout<<"itemno=";
cin>>itno;
cout<<"price=";
o1[i].read(itno,pri);
for(i=0;i<5;i++)
o1[i].show();
} //end of program
OUTPUT:
PROGRAM-13: WAP in C++ to add two diff. minutes and hours using object as f() argument.
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>
int minutes;
hours=h;
minutes=m;
};
int h1,m1;
hours=t1.hours+t2.hours;
minutes=t1.minutes+t2.minutes;
h1=minutes/60;
m1=minutes%60;
hours=hours+h1;
minutes=m1;
cout<<"PROGRAM TO ADD TWO diff. MINUTES & HOURS USING OBJECT AS F() ARGUMENT:";
t2.read(3,50);
t3.add(t1,t2);
t2.show();
t3.show();
} //end of program
OUTPUT:
PROGRAM-14: WAP in C++ to add two english distances using object as f() argument.
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>
int inches;
feet=i;
inches=j;
};
int f1,inc1;
feet=d1.feet+d2.feet;
inches=d1.inches+d2.inches;
f1=inches/12;
inc1=feet%12;
feet=feet+f1;
inches=inc1;
d2.read(3,9);
d3.add(d1,d2);
d2.show();
d3.show();
} //end of program
OUTPUT:
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>
cout<<"Enter b=";
cin>>b;
void show();
};
o1.show();
} //end of program
OUTPUT:
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>
public:
void process();
void output();
};
c=a+b; //operation
a1.process();
a1.output();
} //end of program
OUTPUT:
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>
};
int c; //declaration
cout<<c;
} //end of program
OUTPUT:
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>
int inches;
public:
feet=f;
inches=i;
};
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;
d2.read(3,9);
d3=sum(d1,d2);
d1.show();
d2.show();
d3.show();
} //end of program
OUTPUT:
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>
a=4;
b=3;
a=i;
b=j;
};
abc o2(5,15);
o2.show();
} //end of program
OUTPUT:
EXPLAINATION: Destructors are also special type of member functions. Here the class name is alpha,
SOURCE CODE:
#include<iostream.h>
#include<conio.h>
int count=0;
public:
alpha() //constructor
count++; //increment
~alpha() //destructor
count--; //decrement
};
alpha a2;
} //end of program
OUTPUT:
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>
float price;
itemno=i;
price=f;
count++;
cout<<"price="<<price<<"\n";
cout<<count;
};
int x::count=0;
o2.read(7,13.4);
o3.read(2,3.5);
o1.show();
o2.show();
o3.show();
cout<<"\n\nand COUNT=";
x::display();
} //end of program
OUTPUT:
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>
i=5;
j=4;
i=a;
j=b;
i=s.i;
j=s.j;
};
abc o2(5,15);
o1.display();
o2.display();
o3.display();
} //end of program
OUTPUT:
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>
protected:
int b;
public:
void get()
cin>>b;
void show()
cout<<a;
void put()
cout<<b;
};
private:
int d,e;
public:
void scan()
cin>>d;
void add()
e=d+b;
void display()
cout<<e;
};
d1.scan();
d1.add();
d1.display();
OUTPUT:
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>
public:
void show()
cout<<a;
};
protected:
int b;
public:
void get()
cout<<"\nEnter b=";
cin>>b;
void put()
cout<<b;
};
private:
int c;
public:
void add()
c=a+b;
void print()
cout<<"\n\nAfter adding=";
cout<<c;
};
d1.get();
d1.add();
d1.print();
} //end of program
OUTPUT:
SOURCE CODE:
#include<iostream.h>
#include<conio.h>
char name[20];
public:
cout<<"Name=";
cin>>name;
void show()
cout<<"The name="<<name;
};
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];
};
private:
int total;
public:
void read()
student_total::read();
void cal()
total=marks[0]+marks[1]+marks[2];
void show()
student_details::show();
cout<<"Total=";
cout<<total;
};
t1.read();
t1.cal();
t1.show();
} //end of program
OUTPUT:
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>
int rollno;
public:
cout<<"Enter ur name=";
cin>>name;
void show()
cout<<"Roll no="<<rollno<<endl<<"Name="<<name;
};
private:
float marks[30],marks_obt;
float per;
public:
void input()
stud_basic::read();
cout<<"Marks[0]=";
cin>>marks[0];
cout<<"Marks[1]=";
cin>>marks[1];
cout<<"Marks[2]=";
cin>>marks[2];
void display()
stud_basic::show();
cout<<"Marks1="<<marks[0]<<endl<<"Marks2="<<marks[1]<<endl<<"Marks3="<<marks[2];
cout<<"\nMarks_obt ="<<marks_obt<<endl;
cout<<"Percentage="<<per;
void cal()
marks_obt=marks[0]+marks[1]+marks[2];
per=float(marks_obt/300)*100;
};
private:
float height,weight;
public:
void read()
stud_basic::read();
cout<<"Height=";
cin>>height;
cout<<"Weight=";
cin>>weight;
void show()
stud_basic::show();
cout<<"Height="<<height<<"\t"<<"Weight="<<weight;
};
s1.cal();
s1.display();
stud_fitness f1;
f1.read();
f1.show();
} //RETURN TYPE
OUTPUT:
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>
int rollno;
public:
cout<<"Enter name=";
cin>>name;
void show()
cout<<"The rollno="<<rollno<<"\t";
cout<<"The name="<<name;
};
protected:
float marks[3];
public:
void read()
cout<<"Marks1="<<endl;
cin>>marks[0];
cout<<"Marks2="<<endl;
cin>>marks[1];
cout<<"Marks3="<<endl;
cin>>marks[2];
void show()
//cout<<marks[0]<<endl;
//cout<<marks[1]<<endl;
//cout<<marks[2]<<endl;
};
class sports
protected:
float score;
public:
void read()
cout<<"Score="<<endl;
cin>>score;
void show()
//cout<<score;
};
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()
stud_acd_marks::show();
sports::show();
cout<<endl<<"Marks_obt="<<marks_obt<<endl<<"Per="<<per<<endl;
};
s1.stud_acd_marks::read();
s1.sports::read();
s1.cal();
s1.show();
} //end of program
OUTPUT:
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>
abc()
count=0;
cout<<count;
void operator++()
count=count+1;
};
int main()
abc o1;
} //end of program
OUTPUT:
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>
int minutes;
cout<<"Enter minutes=";
cin>>minutes;
cout<<"\tminutes="<<minutes;
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()
t2.read();
t3.show();
} //end of program
OUTPUT:
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 add(int);
float add(float,float);
int add(int,int,int);
float g,h,i;
cin>>b;
cin>>c;
cin>>g;
cin>>h;
e=add(a,b);
f=add(a,b,c);
i=add(g,h);
int y;
y=x+x;
return y;
int z;
z=x+y;
return z;
int s;
s=p+q+r;
return s;
float o;
o=m+n;
return o;
OUTPUT:
SOURCE CODE:
#include<conio.h>
cout<<"Show base"<<"\n\n";
};
void display()
cout<<"Display derive"<<"\n\n";
void show()
cout<<"Show derive";
};
derive d;
base *bptr;
bptr=&b;
bptr->display();
bptr->show();
bptr=&d;
bptr->display();
bptr->show();
} //end of program
OUTPUT:
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>
t1 a;
t2 b;
test(t1 x,t2 y)
a=x;
b=y;
};
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();
} //end of program
OUTPUT:
SOURCE CODE:
#include<iostream.h>
#include<fstream.h>
int rollno;
cin>>rollno;
ofstream out("student");
out<<name<<"\n";
out<<rollno;
out.close();
ifstream in("student");
in>>name;
in>>rollno;
in.close();
} //end of program
OUTPUT:
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>
cin.get(a,20);
} //end of program
OUTPUT: