Professional Documents
Culture Documents
Name:
Register No:
CONTENTS
S.N
O
DATE
EXPERIMENT NAME
1.
CONSTRUCTOR AND
DESTRUCTOR
2.
INHERITANCE
3.
FUNCTION OVERLOADING
4.
BINARY OPERATOR
OVERLOADING
5.
VIRTUAL FUNCTION
6.
POLYMORPHISM
7.
FRIEND FUNCTION
8.
TEMPLATE
9.
EXCEPTION HANDLING
10.
DYNAMIC ALLOCATION
PAG
E
NO
MAR
KS
SIGNATUR
E
EX. NO: 1
DATE :
PROGRAM:#include<iostream.h>
#include<conio.h>
class stu
{
char name[20], add[20];
int roll, zip;
public:
stu();
~stu();
void read();
void disp();
};
stu::stu()
{
cout<<"\n This is student details \n";
}
void stu::read()
{
cout<<"\n Enter the Student name:";
cin>>name;
cout<<"\n Enter the student roll no:";
cin>>roll;
cout<<"\n Enter the student address:";
cin>>add;
cout<<"\n Enter the zip code:";
cin>>zip;
}
void stu::disp()
{
cout<<"\n Student name:"<<name;
cout<<"\n Roll no is:"<<roll;
cout<<"\n Address is:"<<add;
cout<<"\n Zipcode is :"<<zip;
}
stu::~stu()
{
cout<<"\n\n Student details are closed";
}
void main()
{
clrscr();
stu s;
s.read();
s.disp();
}
RESULT:Thus a c++ program using constructor and destructor was successfully executed.
EX. NO: 2
DATE :
INHERITANCE
AIM:
A program to print area and perimeter of a rectangle using inheritance.
ALGORITHM:
STEP 1: Start the program.
STEP 2: Invoke the class.
STEP 3: Enter the length and breadth in base class.
STEP 4: Using base class and function calculate area and perimeter of a rectangle.
STEP 5: Print the area and rectangle of rectangle.
STEP 6: Stop the program.
PROGRAM:#include<iostream.h>
#include<conio.h>
class rectangle
{
protected:
float length, breadth;
public:
rectangle():length(0.0) , breadth(0.0)
{
cout<<"\n\n Enter length and breadth";
cin>>length>>breadth;
}
};
class area:rectangle
{
public:
float calc()
{
return length*breadth;
}
};
class perimeter:rectangle
{
public:
float calc()
{
return 2*(length+breadth);
}
};
void main()
{
clrscr();
cout<<"\n\n Enter data for rectangle to find area:";
area a;
cout<<"\n Area ="<<a.calc()<<" sq.units";
cout<<"\n\n Enter data for rectangle to find perimeter:";
perimeter p;
cout<<"\n\n Perimeter ="<<p.calc()<<" m";
getch();
}
EX. NO: 3
DATE :
FUNCTION OVERLOADING
AIM:
A program to print sum of two numbers of different datatype using function overloading.
ALGORITHM:
STEP 1: Start the program.
STEP 2: Create two functions with different arguments datatype but functions are having same name.
STEP 3: Take values to perform the operations of the functions and print them (values).
STEP 4: End the program.
PROGRAM:#include<iostream.h>
#include<conio.h>
long add(long, long);
float add(float, float);
void main()
{
long a, b, x;
float c, d, y;
clrscr();
cout<<"\n\n Enter 2 integers:";
cin>>a>>b;
x=add(a, b);
cout<<"\n\n Sum of integers:"<<x<<endl;
cout<<"\n\n Enter 2 float nos:";
cin>>c>>d;
y=add(c, d);
cout<<"\n\n Sum of floats:"<<y;
getch();
}
long add(long x, long y)
{
long sum;
sum=x+y;
return sum;
}
float add(float x, float y)
{
float sum;
sum=x+y;
return sum;
}
RESULT:-
PROGRAM:#include<iostream.h>
#include<conio.h>
class complex
{
int a, b;
public:
void getvalue()
{
cout<<"Enter value of comple nos a, b:";
cin>>a>>b;
}
complex operator+(complex ob)
{
complex t;
t.a=a+ob.a;
t.b=b+ob.b;
return t;
}
complex operator-(complex ob)
{
complex t;
t.a=a-ob.a;
t.b=b-ob.b;
return t;
}
void display()
{
cout<<a<<"+"<<b<<"i"<<"\n";
}
};
void main()
{
clrscr();
complex obj1, obj2, res1, res2;
obj1.getvalue();
obj2.getvalue();
res1=obj1+obj2;
res2=obj1-obj2;
cout<<"\n\n Input values:";
obj1.display();
obj2.display();
cout<<"\n\n Result:-\n";
res1.display();
res2.display();
getch();
}
RESULT:
Thus a c++ program using binary operator was successfully executed.
EX. NO: 5
DATE :
VIRTUAL FUNCTIONS
AIM:
To write a C++ program for virtual functions.
ALGORITHM:
STEP 1: Start the program.
STEP 2: Create base and derived class.
STEP 3: Create function in both base and derived class with the same name.
STEP 4: Give virtual keyword before the function in base class.
STEP 5: Create pointer object and normal object of class A and class B
STEP 6: Assign address of OB and PA and call the function that is virtual and result will come
STEP 7: End the program.
PROGRAM:#include<iostream.h>
#include<conio.h>
class A
{
int a;
public:
A()
{
a=1;
}
virtual void show()
{
cout<<"\n\n"<<a;
}
};
class B:public A
{
int b;
public:
B()
{
b=2;
}
void show()
{
cout<<"\t\n\n"<<b;
}
};
class C:public A
{
int c;
public:
C()
{
c=3;
}
void show()
{
cout<<"\t\n\n"<<c;
}
};
void main()
{
clrscr();
A *pa;
B ob;
C oc;
pa=&ob;
pa->show();
pa=&oc;
pa->show();
getch();
}
OUTPUT:2
RESULT:
Thus a c++ program using virtual function was successfully executed.
EX. NO: 6
DATE :
POLYMORPHISM
AIM:
To write a C++ program for polymorphism.
ALGORITHM:
STEP 1: Start the program.
STEP 2: Create base and derived class.
STEP 3: Create base class pointer objects and assign the address of the objects of derived class to the
pointers.
STEP 4: Call the function area( ) using pointers.
STEP 5: Result will come as required.
STEP 6: End the program.
PROGRAM:#include<iostream.h>
#include<conio.h>
class Polygon
{
protected:
int width, height;
public:
void set_values(int a, int b)
{ width=a; height=b; }
};
class Rectangle:public Polygon
{
public:
int area()
{
return width*height;
}
};
class Triangle: public Polygon
{
public:
int area()
{
return width*height/2;
}
};
void main ()
{
clrscr();
Rectangle rect;
Triangle trgl;
Polygon * ppoly1 = ▭
Polygon * ppoly2 = &trgl;
ppoly1->set_values(4, 5);
ppoly2->set_values(4, 5);
cout<<rect.area()<<"\n";
cout<<trgl.area()<<"\n";
getch();
}
OUTPUT:20
10
RESULT:
Thus a c++ program using polymorphism was successfully executed.
EX. NO: 7
DATE :
FRIEND FUNCTIONS
AIM:
To write a C++ program for friend functions.
ALGORITHM:
STEP 1: Start the program.
STEP 2: Create a function in class to get data and a friend function to access the private data of the class.
STEP 3: Create object to call the function to get data and call the friend function to display the value of the
private variable.
STEP 4: End the program.
PROGRAM:#include<stdio.h>
#include<conio.h>
class sample
{
int x;
public:
void getdata();
friend void display(class sample);
};
void sample::getdata()
{
cout<<"Enter x";
cin>>x;
}
void display(class sample abc)
{
cout<<"Entered number: "<<abc.x;
}
void main()
{
clrscr();
sample obj;
obj.getdata();
cout<<"\n Accessing the private data by non-member functions";
display(obj);
getch();
}
OUTPUT:Enter x: 30
Accessing the private data by non-member function
Entered number: 30
RESULT:
Thus a c++ program using friend function concept to access private members was successfully
executed.
EX. NO: 8
DATE :
TEMPLATE
AIM:
To write a C++ program for function template for swapping numbers.
ALGORITHM:
STEP 1: Start the program.
STEP 2: Create a function template for swapping numbers
STEP 3: Take values for swapping and call the function which will call function template.
STEP 4: After swapping, the values will be printed
STEP 5: End the program.
PROGRAM:#include<iostream.h>
#include<conio.h>
template<class T>T swap(T &fst, T &snd)
{
T tmp;
tmp=fst;
fst=snd;
snd=tmp;
return 0;
}
int swap(int &a, int &b);
float swap(float &a, float &b);
void main()
{
int ix, iy;
float fx, fy;
clrscr();
cout<<"\n Enter two integer numbers";
cin>>ix>>iy;
cout<<"\n Enter two float numbers";
cin>>fx>>fy;
swap(ix, iy);
cout<<"\n After swapping integers are:-";
cout<<"\n\nix="<<ix<<" iy="<<iy;
swap(fx, fy);
cout<<"\n\n After swapping float numbers are:-";
cout<<"\n\nfx="<<fx<<" fy="<<fy;
getch();
}
RESULT:
Thus a c++ program using template concept to swap two numbers was successfully executed.
EX. NO: 9
DATE :
EXCEPTION HANDLING
AIM:
To write a C++ program for exception handling.
ALGORITHM:
STEP 1: Start the program.
STEP 2: Inside the main( ), get values for two variables a and b.
STEP 3: Now, subtract the two variables and store the value to another variable x.
STEP 4: Inside the try block, give ifelse statement that if x is not equal to zero then give result for (a/x),
otherwise throw x(value).
STEP 5: Catch followed by try catches the exception and print the x value if x is equal to zero
STEP 6: Stop the program
PROGRAM:#include<iostream.h>
#include<conio.h>
void main()
{
int a, b;
cout<<"\n Enter a and b";
cin>>a>>b;
int x;
x=a-b;
try
{
if(x!=0)
{
cout<<"Result:"<<(a/x);
}
else
{
throw x;
}
}
catch(int i)
{
cout<<"Caught divide by 0 exception x="<<x;
}
cout<<"END";
return 0;
}
RESULT:
Thus a c++ program using exception handling concept to find divide by zero exception was
successfully executed.
EX. NO: 10
DATE :
DYNAMIC ALLOCATION
AIM:
To write a C++ program for dynamic allocation.
ALGORITHM:
STEP 1: Start the program.
STEP 2: Create the pointer of integer type using new operator.
STEP 3: Enter numbers into it.
STEP 4: After entering numbers, take the pointer to original position.
STEP 5: Now, print the numbers that are entered with their respective addresses.
STEP 5: End the program.
PROGRAM:#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int *p=new int[4] , k;
for(k=0;k<4;k++)
{
cout<<"Enter the number";
cin>>*p;
p++;
}
p=p-4;
cout<<"\n Entered nos with their address are \n";
for(k=0;k<4;k++)
{
cout<<"\n \t"<<*p<<"\t"<<(unsigned)p;
p++;
}
delete p;
for(k=0;k<4;k++)
{
cout<<"\n\n \t"<<*p<<"\t"<<(unsigned)p;
p++;
}
getch();
}
RESULT:Thus a c++ program using dynamic allocation concept was successfully executed.