[go: up one dir, main page]

0% found this document useful (0 votes)
11 views14 pages

CPP Lab Print

lab material
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views14 pages

CPP Lab Print

lab material
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Exercise -10 a)

AIM: Write a Program in C++ to illustrate the order of execution of


constructors and destructors in inheritance?
Program:
#include<iostream>
using namespace std;
class Base
{
public:
Base( )
{
cout << "Inside Base constructor" << endl;
}
~Base ( )
{
cout << "Inside Base destructor" << endl;
}
};
class Derived : public Base
{
public:
Derived ( )
{
cout << "Inside Derived constructor" << endl;
}
~Derived ( )
{
cout << "Inside Derived destructor" << endl;
}
};
int main( )
{
Derived x;
return 0;
}
Exercise -10 b)
AIM: Write a Program to show how constructors are invoked in derived
class?
Program:
#include <iostream>
using namespace std;
class base1
{

private:
int x;
public:
base1(int i)
{
x = i;
cout << "\n base1 initialized \n";
}
void show_x()
{
cout << "\n x = "<<x;
}

};
class base2
{

private:
float y;
public:
base2(float j)
{
y = j;
cout << "\n base2 initialized \n";
}
void show_y()
{
cout << "\n y = "<<y;
}
};
class derived : public base1, public base2
{
private:
int n,m;
public:
derived(int a, float b, int c, int d):base1(a), base2(b)
{
m = c;
n = d;
cout << "\n derived initialized \n";
}
void show_mn()
{
cout << "\n m = "<<m;
cout << "\n n = "<<n;
}
};

int main()
{
derived d(5, 7.65, 30, 100);
cout << "\n";
d.show_x();
d.show_y();
d.show_mn();
}
Exercise -11 a)
AIM: Write a program to illustrate runtime polymorphism?
Program:
#include <iostream>
using namespace std;
class base
{

private:
int x;
public:
void readx()
{
cout<<"enter x value";
cin>>x;
}
virtual void show()
{ cout<<"x="<<x<<endl;
}
};
class derived:public base
{
int y;
public:void ready()
{cout<<"enter y value";
cin>>y;
}
void show()
{
cout<<"y="<<y<<endl;}

};
int main ()
{
base b,*p;
derived d;
b.readx();
d.ready();

p=&b;
p->show();
p=&d;
p->show();
return 0;
}
Exercise -11 b)
AIM: Write a program illustrates pure virtual function and calculate the
area of different shapes by using abstract class.

Program:
#include <iostream>
using namespace std;
class shape
{
virtual void area(int)=0;
virtual void area(int,int)=0;
};

class derived: public shape


{
public:

void area(int a)
{
cout<<"area of square is"<<(a*a)<<endl;
}
void area(int w,int h)
{
cout<<"area of rectangle is"<<(w*h)<<endl;
}
};
int main ()
{ derived *p;
int l,w,h,b;
derived d;
p=&d;
cout<<"enter length of square"<<endl;
cin>>l;
p->area(l);
cout<<"enter width,height of rectangle"<<endl;
cin>>w>>h;
p->area(w,h);
return 0;
}
Exercise -11 c)
AIM: Write a case study on virtual functions?

Description:
A virtual function is a member function that is declared within a base class and
redefined by a derived class. To create virtual function, precede the function’s
declaration in the base class with the keyword virtual. When a class containing
virtual function is inherited, the derived class redefines the virtual function to suit
its own needs.
Base class pointer can point to derived class object. In this case, using base class
pointer if we call some function which is in both classes, then base class function is
invoked. But if we want to invoke derived class function using base class pointer, it
can be achieved by defining the function as virtual in base class, this is how virtual
functions support runtime polymorphism.
Polymorphism is also achieved in C++ using virtual functions. If a function with
same name exists in base as well as parent class, then the pointer to the base class
would call the functions associated only with the base class. However, if the
function is made virtual and the base pointer is initialized with the address of the
derived class, then the function in the child class would be called.

To cause late binding to occur for a particular function, C++ requires that you use
the virtual keyword when declaring the function in the base class. Late binding
occurs only with virtual functions, and only when you’re using an address of the
base class where those virtual functions exist, although they may also be defined in
an earlier base class.To create a member function as virtual, you simply precede the
declaration of the function with the keyword virtual. Only the declaration needs
the virtual keyword, not the definition. If a function is declared as virtual in the base
class, it is virtual in all the derived classes. The redefinition of a virtual function in a
derived class is usually called overriding.Notice that you are only required to
declare a function virtual in the base class. All derived-class functions that match
the signature of the base-class declaration will be called using the virtual
mechanism. You can use the virtual keyword in the derived-class declarations (it
does no harm to do so), but it is redundant and can be confusing.
Exercise -12 a)
AIM: Write a C++ Program to illustrate template class?

Program:
#include <iostream>
using namespace std;
template<class t1>
class sample
{
t1 a;
public:
void getdata()
{
cout<<"Enter a : ";
cin>>a;
}
void display()
{
cout<<"Displaying values"<<endl;
cout<<"a="<<a<<endl;
}
};

int main()
{
sample<int> s1;
sample<char>s2;
sample<float>s3;
cout <<"Integer data"<<endl;
s1.getdata();
s1.display();
cout <<"Character data"<<endl;
s2.getdata();
s2.display();
cout <<"Float data"<<endl;
s3.getdata();
s3.display();
return 0;
}
Exercise -12 b)
AIM: Write a Program to illustrate class templates with multiple
parameters?

Program:
#include <iostream>
using namespace std;
template<class t1,class t2>
class sample
{
t1 a;
t2 b;
public:
void getdata()
{
cout<<"Enter a and b: ";
cin>>a>>b;
}
void display()
{
cout<<"Displaying values"<<endl;
cout<<"a="<<a<<endl;
cout<<"b="<<b<<endl;
}
};
int main()
{
sample<int,int> s1;
sample<int,char>s2;
sample<int,float>s3;
cout <<"Two Integer data"<<endl;
s1.getdata();
s1.display();
cout <<"Integer and Character data"<<endl;
s2.getdata();
s2.display();
cout <<"Integer and Float data"<<endl;
s3.getdata();
s3.display();
return 0;
}
Exercise -12 c)
AIM: Write a Program to illustrate member function template?

Program:
#include <iostream>
using namespace std;
template<class t1>
class sample
{
t1 a;
public:
void getdata(t1 x);
void display()
{
cout<<"Displaying values"<<endl;
cout<<"a="<<a<<endl;
}
};
template<class t1>
void sample<t1>::getdata(t1 x)
{
a=x;
}
int main()
{
sample<int>s1;
sample<char>s2;
sample<float>s3;
cout <<"Integer data"<<endl;
s1.getdata(10);
s1.display();
cout <<"Character data"<<endl;
s2.getdata('D');
s2.display();
cout <<"Float data"<<endl;
s3.getdata(2.2);
s3.display();
return 0;
}
Exercise -13 a)
AIM: Write a Program for Exception Handling Divide by zero

Program:
#include<iostream>
using namespace std;
int main()
{
int b;
float a,d;
cout<<"Enter the value of a:";
cin>>a;
cout<<"Enter the value of b:";
cin>>b;
try
{
if((b)!=0)
{
d=float(a/b);
cout<<"Result is:"<<d;
}
else
{
throw(b);
}
}
catch(int i)
{
cout<<"Answer is infinite because b is:"<<i;
}
}
Exercise -13 b)
AIM: Write a Program to re-throw an Exception
Program:

#include<iostream>
using namespace std;
void divide(double x,double y)
{
cout<<"inside function"<<endl;
try
{
if(y==0.0)
throw y;
else
cout<<"division = "<<x/y<<endl;
}
catch(double d)
{ cout<<"caught double inside function"<<endl;
throw;
}
cout<<"end of function"<<endl;
}

int main()
{

cout<<"inside main"<<endl;
try
{
divide(12.6,2.0);
divide(12.6,0.0);
}
catch(double d)
{
cout<<"caught double inside main"<<endl;
}
cout<<"end of main"<<endl;
return 0;
}
Exercise -5 b)
AIM: Write a Program illustrate function template for power of a
number?
Program:
#include<iostream>
#include<cmath>
using namespace std;
template<class T>
void powers(T a, int exp)
{
cout<<pow(a,exp)<<endl;
}
int main()
{
int x=2;
int exp;
cout<<"enter exponent value";
cin>>exp;
float y=2.5;
cout<<"power of integer is";
powers(x,exp);
cout<<"power of float is";
powers(y,exp);
return 0;
}
Exercise -5 c)
AIM: Write a Program to illustrate function template for swapping of
two numbers?
Program:
#include<iostream>
using namespace std;
template<class t>
void swap(t *x,t *y)
{
t temp=*x;
*x=*y;
*y=temp;
cout<<*x<<" "<<*y<<endl;
}
int main()
{
int a=10,b=20;
float x=10.2,y=20.2;
cout<<"swapping of integers"<<endl;
swap(&a,&b);
cout<<"swapping of floats"<<endl;
swap(&x,&y);
return 0;
}

You might also like