You are on page 1of 24

Submitted To: Submitted By:

Mr. Virender Kr. Yadav Sumit Kumar


07/IT/41
INDEX
S.No. Practical Date Sign.

01 TO IMPLEMENT THE CONCEPT OF CLASS Mar 02nd,09

02 TO IMPLEMENT THE CONCEPT OF CLASS USING MEMBER Mar 02nd,09


FUNCTION DEFINED OUTSIDE CLASS AND PASSING OBJECTS AS
FUNCTION ARGUMENTS.

03 TO IMPLEMENT THE CONCEPT OF CLASS USING MEMBER Mar 09th,09


FUNCTION DEFINED OUTSIDE CLASS.

04 TO IMPLEMENT THE CONCEPT OF CLASS USING MEMBER Mar 09th,09


FUNCTION DEFINED OUSIDE CLASS AND INITIALIZATION OF
DATA MEMBERS.

05 TO IMPLEMENT THE USE OF FRIEND FUNCTION USING Mar 16th,09


MULTIPLE CLASSES

06 TO IMPLEMENT THE USE OF CONSTRUCTOR. Mar 16th,09

07 TO IMPLEMENT THE USE OF DESTRUCTOR. Mar 23th,09

08 TO IMPLEMENT THE CONCEPT OF FUNCTION OVERLOADING. Mar 23th,09

09 TO IMPLEMENT THE CONCEPT OF OPERATOR OVERLOADING. Mar 30th,09


(BINARY)

10 TO IMPLEMENT THE CONCEPT OF OPERATOR OVERLOADING. Mar 30th,09


(UNARY)

11 TO IMPLEMENT CONCEPT OF INHERITANCE. Apr 06th,09

12 TO IMPLEMENT CONCEPT OF VIRTUAL BASE CLASS Apr 06th,09

13 TO IMPLEMENT CONCEPT OF VIRTUAL FUNCTION. Apr 13th,09

14 TO IMPLEMENT CONCEPT OF GENERIC CLASSES USING Apr 13th,09


TEMPLATES.

15 TO IMPLEMENT CONCEPT OF EXCEPTION HANDLING. Apr 13th,09


Practical # 1

Objective: To implement the concept of class.


// Header Files
#include<iostream.h>
#include<conio.h>
class raise
{
int a,b; // Private by default
public:
void input(int m,int n)
{
a=m;
b=n;
}
void power(int a,int b)
{
int c=1,i;
for(i=0;i<b;i++)
{
c=c*a;
}
cout<<"The value of a^b is\n"<<c;
}
};
int main()
{
raise A;
int p,q;
clrscr();
cout<<"Enter the number a\n";
cin>>p;
cout<<"Enter the number b\n";
cin>>q;
A.input(p,q);
A.power(p,q);
getch();
return 0;
}

Output:
Practical # 2
Objective: To implement the concept of class using member
function defined outside class and passing objects as function
arguments.
// Header Files
#include<iostream.h>
#include<conio.h>
class point // Class Declaration
{
public:
int x,y;
void getdata(int x,int y);
};
void point::getdata(int a,int b)
{
x=a;
y=b;
}
int main() // Main Function
{
point p1,p2;
int p,q;
clrscr(); // Clears the screen
cout<<"Enter the value of x\n";
cin>>p;
cout<<"Enter the value of y\n";
cin>>q;
p1.getdata(p,q);
cout<<"Enter the value of x\n";
cin>>p;
cout<<"Enter the value of y\n";
cin>>q;
p2.getdata(p,q);
cout<<"The Aggregate values of x is\n"<<p1.x+p2.x;
cout<<"\nThe aggregate values of y is\n"<<p1.y+p2.y;
getch();
return 0;
}

Output:
Practical # 3

Objective: To implement the concept of class using member


function defined outside class.
#include<iostream.h>
#include<conio.h>
#include<process.h>
class calc
{
int m,n;
public:
void getdata(int a,int b)
{
m=a;
n=b;
}
void getchoice(char ch)
{
do
{
int a,b;
clrscr();
cout<<"\n******** Calculator ********";
cout<<"\n+";
cout<<"\n-";
cout<<"\n*";
cout<<"\n/";
cout<<"\n%";
cout<<"\nPress 0 To Exit";
cout<<"\n-------------------------------------";
cout<<"\nEnter your choice: " ;
cin>>ch;
switch(ch)
{
case '+' :
cout<<"Plz Enter the 1st number: ";
cin>>a;
cout<<"Plz Enter the 2nd number: ";
cin>>b;
cout<<"The Sum is: "<<a+b;
getch();
break;
case '-' :
cout<<"Plz Enter the 1st number: ";
cin>>a;
cout<<"Plz Enter the 2nd number: ";
cin>>b;
cout<<"The Difference is: "<<a-b;
getch();
break;
case '*' :
cout<<"Plz Enter the 1st number: ";
cin>>a;
cout<<"Plz Enter the 2nd number: ";
cin>>b;
cout<<"The Product is: "<<a*b;
getch();
break;
case '/' :
cout<<"Plz Enter the 1st number: ";
cin>>a;
cout<<"Plz Enter the 2nd number: ";
cin>>b;
cout<<"The Quotient is: "<<a/b;
getch();
break;
case '%' :
cout<<"Plz Enter the 1st number: ";
cin>>a;
cout<<"Plz Enter the 2nd number: ";
cin>>b;
cout<<"The Remainder is: "<<a%b;
getch();
break;
case '0' :
cout<<"Press any key to exit... ";
getch();
exit(0);
break;
default :
cout<<"Wrong Choice\nPress Any Key to
Continue...\n";
getch();
}
}while(ch!=0);
}
};
int main()
{
calc C;
clrscr();
char ch;
int p,q;
C.getchoice(ch);
cout<<"Enter the Numbers\n";
cin>>p>>q;
C.getdata(p,q);
getch();
return 0;
}

Output:
Practical # 4
Objective: To implement the concept of class using member
function defined ouside class and initialization of data members.
// Header Files
#include<iostream.h>
#include<conio.h>
class phone
{
public:
int code,exch;
char no[10];
};
int main()
{
phone p1;
clrscr();
cout<<"Plz Enter Your Area Code : ";
cin>>p1.code;
cout<<"Plz Enter your exchange code : ";
cin>>p1.exch;
cout<<"Plz Enter your number : ";
cin>>p1.no;
cout<<"Your number
is :"<<"("<<p1.code<<")"<<"0"<<p1.exch<<"-"<<p1.no;
getch();
return 0;
}

Output:
Practical # 5
Objective: To implement the use of friend function using multiple
classes.
#include<iostream.h>
#include<conio.h>
class db;
class dm
{
float meter,centi;
public:
void set1(void)
{
cout<<"Enter the distance in meter\n";
cin>>meter;
cout<<"Enter the distance in centimeter\n";
cin>>centi;
}
friend void addvalue(dm,db);
};
class db
{
float inch,feet;
public:
void set2(void)
{
cout<<"Enter distance in feet\n";
cin>>feet;
cout<<"Enter distance in inches\n";
cin>>inch;
}
friend void addvalue(dm,db);
};
void addvalue(dm m, db b)
{
cout<<"Result in
meters\n"<<m.meter+m.centi/100+b.feet/3.28+b.inch/39.37;
}
int main()
{
dm ab;
db xy;
clrscr();
ab.set1();
xy.set2();
addvalue(ab,xy);
getch();
return 0;
}
Output:
Practical # 6

Objective: To implement the use of constructor.


#include<iostream.h>
#include<conio.h>
class abc
{
public:
int a,b;
abc()
{
clrscr();
cout<<"\nThis is a constructor\n";
cout<<"Enter 2 integers\n";
cin>>a>>b;
cout<<"The values are\n"<<a<<endl<<b;
}
void sum()
{
cout<<"\n\nThe sum of the integers is\n";
cout<<a+b;
}
};
int main()
{
{
abc A;
A.sum();
}
getch();
return 0;
}

Output:
Practical # 7

Objective: To implement the use of destructor.


#include<iostream.h>
#include<conio.h>
class abc
{
public:
int a,b;
void sum()
{
cout<<"\n\nThe sum of the integers is\n";
cout<<a+b;
}
~abc()
{
cout<<"\n\nThis is a destructor.\nThe Memory has been
deallocated\n";
cout<<"Goodbye...\n";
}
};
int main()
{
{
abc A;
A.sum();
}
getch();
return 0;
}

Output:
Practical # 8

Objective: To implement the concept of function overloading.


#include<iostream.h>
#include<conio.h>
class fo
{
float rad,len,bth;
public:
void area(float a)
{
float area;
rad=a;
area=3.14*a;
cout<<"The area of the circle is -> "<<area<<endl;
}
void area(float b,float c)
{
float area;
len=b;
bth=c;
area=len*bth;
cout<<"The area of the rectangle is -> "<<area<<endl;
}
};
int main()
{
fo F;
float a,b,c;
clrscr();
cout<<"Enter the Radius of the Circle\n";
cin>>a;
F.area(a);
cout<<"\n**************************************\n";
cout<<"\nEnter the Length & Breadth of the Rectangle\n";
cin>>b>>c;
F.area(b,c);
getch();
return 0;
}

Output:
Practical # 9

Objective: To implement the concept of operator overloading.


(binary)
#include<iostream.h>
#include<conio.h>
class complex
{
int x,y;
public:
void getvalue()
{
cout<<"Enter the value of x : ";
cin>>x;
cout<<"Enter the value of y : ";
cin>>y;
}
void display()
{
cout<<"\nThe value of x is : "<<x;
cout<<"\nThe value of y is : "<<y<<endl;
}
complex operator+(complex);
};
complex complex::operator +(complex c)
{
complex temp;
temp.x=x+c.x;
temp.y=y+c.y;
return(temp);
}
int main()
{
complex c1,c2,c3;
clrscr();
c1.getvalue();
c2.getvalue();
cout<<"\nFor 1st Object"<<endl;
c1.display();
cout<<"\nFor 2nd Object"<<endl;
c2.display();
cout<<"\nFor 3rd Object(after addition)"<<endl;
c3=c2+c1;
c3.display();
getch();
return 0;
}
Output:
Practical # 10

Objective: To implement the concept of operator overloading.


(unary)
#include<iostream.h>
#include<conio.h>
class space
{
int x,y,z;
public:
void getdata(int a,int b,int c)
{
x=a;
y=b;
z=c;
}
void display(void)
{
cout<<x<<" ";
cout<<y<<" ";
cout<<z<<"\n";
}
void operator-()
{
x=-x;
y=-y;
z=-z;
}
};
int main()
{
space S;
int a,b,c;
clrscr();
cout<<"Enter 3 +ve/-ve integers\n";
cin>>a>>b>>c;
S.getdata(a,b,c);
cout<<"\nThe Current values of integers are\n";
cout<<"S : ";
S.display();
-S;
cout<<"The modified value of integers are\n";
cout<<"S : ";
S.display();
getch();
return 0;
}

Output:
Practical # 11

Objective: To implement concept of inheritance.


#include<iostream.h>
#include<conio.h>
class abc
{
int a,b; // Private by default
public:
void add(int a,int b)
{
int sum;
sum=a+b;
cout<<sum;
}
};
class pqr:public abc
{
int p,q;
public:
void sub(int p,int q)
{
int sub;
sub=p-q;
cout<<sub;
}
};
int main()
{
abc A;
pqr P;
clrscr();
int m,n;
cout<<"Enter the numbers for algebra\n";
cin>>m>>n;
cout<<"The sum from Base class is ";
A.add(m,n);
cout<<"\nThe sum from Derived class is ";
P.add(m,n);
cout<<"\nThe difference is ";
P.sub(m,n);
getch();
return 0;
}

Output:
Practical # 12

Objective: To implement concept of virtual base class.


#include<iostream.h>
#include<conio.h>
class A
{
public:
int a,b;
void display()
{
cout<<"Enter 2 integers for display\n";
cin>>a>>b;
cout<<"The values are\n";
cout<<a<<endl<<b<<endl;
}
};
class B:virtual public A
{
public:
int p,q,r;
void show1()
{
cout<<"Enter 3 values for show1\n";
cin>>p>>q>>r;
cout<<"The values are\n";
cout<<p<<endl<<q<<endl<<r<<endl;
}
};
class C:virtual public A
{
public:
int x;
void show2()
{
cout<<"Enter a value for show2\n";
cin>>x;
cout<<"The values are\n";
cout<<x<<endl;
}
};
class D:public B,public C
{
public:
int i,j,k,l;
void sum()
{
cout<<"Enter 4 values for sum\n";
cin>>i>>j>>k>>l;
cout<<"The values are\n";
cout<<i+j+k+l<<endl;
}
};
int main()
{
D item;
clrscr();
cout<<"\n\n******** A Program to demonstrate Virtual Base Class
********\n\n";
item.display();
item.show1();
item.show2();
item.sum();
getch();
return 0;
}

#include<iostream.h>
#include<conio.h>
class base
{
public:
void display()
{
cout<<"Display Base\n";
}
virtual void show()
{
cout<<"Show Base\n";
}
};
class derived:public base
{
public:
void display()
{
cout<<"Display Derived\n";
}
void show()
{
cout<<"Show Derived\n";
}
};
int main()
{
base B;
derived D;
base *ptr;
clrscr();
cout<<"\n\n******** Virtual Function ********\n";
cout<<"\nPointer points to Base\n";
ptr=&B;
ptr->display(); // Calls Base Version
ptr->show(); // Calls Base Version
cout<<"\nPointer points to Derived\n";
ptr=&D;
ptr->display(); // Calls Base Version
ptr->show(); // Calls Derived Version
getch();
return 0;
}
Output:
Practical # 13

Objective: To implement concept of virtual function.


#include<iostream.h>
#include<conio.h>
class base
{
public:
void display()
{
cout<<"Display Base\n";
}
virtual void show()
{
cout<<"Show Base\n";
}
};
class derived:public base
{
public:
void display()
{
cout<<"Display Derived\n";
}
void show()
{
cout<<"Show Derived\n";
}
};
int main()
{
base B;
derived D;
base *ptr;
clrscr();
cout<<"\n\n******** Virtual Function ********\n";
cout<<"\nPointer points to Base\n";
ptr=&B;
ptr->display(); // Calls Base Version
ptr->show(); // Calls Base Version
cout<<"\nPointer points to Derived\n";
ptr=&D;
ptr->display(); // Calls Base Version
ptr->show(); // Calls Derived Version
getch();
return 0;
}
Output:
Practical # 14

Objective: To implement the concept of generic classes using


templates.
#include<iostream.h>
#include<conio.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<<a<<" and "<<b<<endl;
}
};
int main()
{
clrscr();
test<float,int> test1(1.23,123);
test<int,char> test2(100,'W');
test1.show();
test2.show();
getch();
return 0;
}

Output:
Practical # 15
Objective: To implement the concept of Exception Handling.
#include<iostream.h>
#include<conio.h>
void divide(int x,int y,int z)
{
cout<<"\nWe are inside the function\n";
if((x-y)!=0) // It is OK
{
int R=z/(x-y);
cout<<"Result = "<<R<<endl;
}
else // There is a problem
{
throw(x-y); // Throw point
}
}
int main()
{
try
{
cout<<"We are in the try block\n";
divide(10,20,30); // Invoke divide
divide(10,10,20); // Invoke divide
}
catch(int i) // Catches the exception
{
cout<<"Caught the exception\n";
}
getch();
return 0;
}

You might also like