Exercise -1 (Classes Objects)
AIM : To write a main function to create objects of DISTANCE class. Input two distances and output the sum.
Source code:
#include <iostream.h>
class Distance
{
private:
int feet,inches;
public:
void inputDistance()
{
cout<<"Enter feet & inches?";
cin>>feet>>inches;
}
void outputDistance()
{
cout<<"feet="<<feet<<"\t Inches="<<inches<<endl;
}
void addDistances(Distance d1,Distance d2)
{
int f,i;
f=d1.feet+d2.feet;
i=d1.inches+d2.inches;
if(i>=12){
f=f+i/12;}
i=i%12;
cout<<"feet="<<f<<"\t Inches="<<i<<endl;
}
};
int main()
{
Distance d1,d2;
d1.inputDistance();
d2.inputDistance();
Distance d3;
d3.addDistances(d1,d2);
return 0;
}
output:
Enter feet & inches?1 1
Enter feet & inches?1 12
feet=3 Inches=1
AIM: To write a C++ Program to illustrate the use of Constructors and Destructors (using the above program.)
class Distance
{
private:
int feet,inches;
public:
void inputDistance()
{
cout<<"Enter feet & inches?";
cin>>feet>>inches;
}
void outputDistance()
{
cout<<"feet="<<feet<<"\t Inches="<<inches<<endl;
}
void addDistances(Distance d1,Distance d2)
{
int f,i;
f=d1.feet+d2.feet;
i=d1.inches+d2.inches;
if(i>=12){
f=f+i/12;}
i=i%12;
cout<<"feet="<<f<<"\t Inches="<<i<<endl;
}
};
int main()
{
Distance d1,d2;
d1.inputDistance();
d2.inputDistance();
Distance d3;
d3.addDistances(d1,d2);
return 0;
}
output:
Enter feet & inches?1 1
Enter feet & inches?1 12
feet=3 Inches=1
AIM: To write a C++ Program to illustrate the use of Constructors and Destructors (using the above program.)
Source code:
#include <iostream.h>
class Distance
{
private:
int feet,inches;
public:
Distance()
{
feet=0;
inches=0;
cout<<"default constructor...."<<endl;
}
Distance(int f,inti)
{
feet=f;
inches=i;
cout<<"argument constructor...."<<endl;
}
void outputDistance()
{
cout<<"feet="<<feet<<"\t Inches="<<inches<<endl;
}
void addDistances(Distance d1,Distance d2)
{
int f,i;
f=d1.feet+d2.feet;
i=d1.inches+d2.inches;
if(i>=12){
f=f+i/12;}
i=i%12;
cout<<"feet="<<f<<"\t Inches="<<i<<endl;
}
~Distance()
{
feet=inches=0;
cout<<"distructor...."<<endl;
}
};
int main()
{
Distance d1(2,9),d2(2,9);
Distance d3;
d3.addDistances(d1,d2);
return 0;
}
output:
argument constructor....
argument constructor....
default constructor....
feet=5 Inches=6
distructor....
distructor....
distructor....
distructor....
distructor....
AIM : To write a program for illustrating function overloading in adding the distance between objects
Distance operator+ (Distance d) {
feet+=d.feet;
inches+=d.inches;
if(inches>=12)
{
feet++;
inches-=12;
}
return Distance(feet, inches);
}
};
int main() {
Distance D1(11, 10),D2(14,5);
Distance D3=D1+D2;
D3.displayDistance(); // display D3
return 0;
}
AIM : To overload the assignment operator "="
// deriving derived class from more than one base class
//base class
# include <iostream.h>
AIM: To Implement multi level inheritance:
Source code:
# include <iostream.h>
# include <iostream.h>
//base class
class student
{
protected:
int rno;
};
//der class
class test:public student
{
protected:
int sub1,sub2;
};
//base class
class sports
{
protected:
int score;
};
//der class
class result:public test,sports
{
int total;
public:
void getdata()
{
cout<<"Enter rno?";
cin>>rno;
cout<<"Enter ,sub1,sub2 and score?";
cin>>sub1>>sub2>>score;
}
void display()
{
cout<<rno<<" "<<sub1<<" "<<sub2<<endl;
cout<<score<<endl;
total=sub1+sub2+score;
cout<<"Total = "<<total<<endl;
}
};
int main()
{
result r;
r.getdata();
r.display();
}
#include <iostream>
using namespace std;
template<class T> T add(T &a,T &b)
{
T result = a+b;
return result;
}
int main()
{
int i =2;
int j =3;
float m = 2.3;
float n = 1.2;
cout<<"Addition of i and j is :"<<add(i,j);
cout<<'\n';
cout<<"Addition of m and n is :"<<add(m,n);
return 0;
}
output:
Addition of i and j is :5 Addition of m and n is :3.5
AIM : To write a Program for Exception Handling Divide by zero
#include <iostream>
using namespace std;
double division(int a, int b) {
if( b == 0 ) {
throw "Division by zero condition!";
}
return (a/b);
}
int main () {
int x = 50;
int y = 0;
double z = 0;
try {
z = division(x, y);
cout << z << endl;
}
catch (const char* msg) {
cerr << msg << endl;
}
return 0;
}
output:
Division by zero condition!
AIM : To write a Program to re throw an Exception
#include <iostream>
using namespace std;
// Function for Exception Thrown
void exceptionFunction() {
// try block - inside Function
try {
throw 0;
}
catch (int i) {
cout << "\nIn Function : Wrong Input :" << i;
throw;
}
}
int main() {
int var = 0;
cout << "Simple C++ Program for Re throwing Exception Handling : In Function\n";
try {
exceptionFunction();
}
catch (int ex) {
cout << "\nIn Main : Wrong Input :" << ex;
}
return 0;
}
output:
class Distance
{
private:
int feet,inches;
public:
Distance()
{
feet=0;
inches=0;
cout<<"default constructor...."<<endl;
}
Distance(int f,inti)
{
feet=f;
inches=i;
cout<<"argument constructor...."<<endl;
}
void outputDistance()
{
cout<<"feet="<<feet<<"\t Inches="<<inches<<endl;
}
void addDistances(Distance d1,Distance d2)
{
int f,i;
f=d1.feet+d2.feet;
i=d1.inches+d2.inches;
if(i>=12){
f=f+i/12;}
i=i%12;
cout<<"feet="<<f<<"\t Inches="<<i<<endl;
}
~Distance()
{
feet=inches=0;
cout<<"distructor...."<<endl;
}
};
int main()
{
Distance d1(2,9),d2(2,9);
Distance d3;
d3.addDistances(d1,d2);
return 0;
}
output:
argument constructor....
argument constructor....
default constructor....
feet=5 Inches=6
distructor....
distructor....
distructor....
distructor....
distructor....
AIM : To write a program for illustrating function overloading in adding the distance between objects
Source code:
class Distance
{
private:
int feet,inches;
public:
Distance()
{
feet=0;
inches=0;
cout<<"default constructor...."<<endl;
}
Distance(int f,inti)
{
feet=f;
inches=i;
cout<<"argument constructor...."<<endl;
}
void outputDistance()
{
cout<<"feet="<<feet<<"\t Inches="<<inches<<endl;
}
//overloaded functions
void addDistances(Distance d2)
{
int f,i;
f=this->feet+d2.feet;
i=this->inches+d2.inches;
if(i>=12){
f=f+i/12;}
i=i%12;
cout<<"feet="<<f<<"\t Inches="<<i<<endl;
}
void addDistances(Distance d1,Distance d2)
{
int f,i;
f=d1.feet+d2.feet;
i=d1.inches+d2.inches;
if(i>=12){
f=f+i/12;}
i=i%12;
cout<<"feet="<<f<<"\t Inches="<<i<<endl;
}
~Distance()
{
feet=inches=0;
cout<<"distructor...."<<endl;
}
};
int main()
{
Distance d1(2,9),d2(2,9);
Distance d3;
d1.addDistances(d2); // calling addSistance(Distance);
d3.addDistances(d1,d2); //calling addSistance(Distance,Distance);
return 0;
}
output:
argument constructor....
argument constructor....
default constructor....
feet=5 Inches=6
distructor....
distructor....
distructor....
distructor....
distructor....
distructor....
{
private:
int feet,inches;
public:
Distance()
{
feet=0;
inches=0;
cout<<"default constructor...."<<endl;
}
Distance(int f,inti)
{
feet=f;
inches=i;
cout<<"argument constructor...."<<endl;
}
void outputDistance()
{
cout<<"feet="<<feet<<"\t Inches="<<inches<<endl;
}
//overloaded functions
void addDistances(Distance d2)
{
int f,i;
f=this->feet+d2.feet;
i=this->inches+d2.inches;
if(i>=12){
f=f+i/12;}
i=i%12;
cout<<"feet="<<f<<"\t Inches="<<i<<endl;
}
void addDistances(Distance d1,Distance d2)
{
int f,i;
f=d1.feet+d2.feet;
i=d1.inches+d2.inches;
if(i>=12){
f=f+i/12;}
i=i%12;
cout<<"feet="<<f<<"\t Inches="<<i<<endl;
}
~Distance()
{
feet=inches=0;
cout<<"distructor...."<<endl;
}
};
int main()
{
Distance d1(2,9),d2(2,9);
Distance d3;
d1.addDistances(d2); // calling addSistance(Distance);
d3.addDistances(d1,d2); //calling addSistance(Distance,Distance);
return 0;
}
output:
argument constructor....
argument constructor....
default constructor....
feet=5 Inches=6
distructor....
distructor....
distructor....
distructor....
distructor....
distructor....
Exercise – 2
Write a program for illustrating Access Specifiers public, private, protected
AIM : To write a program implementing Friend Function
Write a program for illustrating Access Specifiers public, private, protected
AIM : To write a program implementing Friend Function
Source code:
#include <iostream>
using namespace std;
class Box {
double width;
public:
friend void printWidth( Boxbox );
void setWidth( doublewid );
};
// Member function definition
void Box::setWidth( double wid ) {
width = wid;
}
void printWidth( Boxbox ) {
cout<< "Width of box : " <<box.width<<endl;
}
int main() {
Box box;
box.setWidth(10.0);
printWidth( box );
return 0;
}
output:
Width of box : 10
using namespace std;
class Box {
double width;
public:
friend void printWidth( Boxbox );
void setWidth( doublewid );
};
// Member function definition
void Box::setWidth( double wid ) {
width = wid;
}
void printWidth( Boxbox ) {
cout<< "Width of box : " <<box.width<<endl;
}
int main() {
Box box;
box.setWidth(10.0);
printWidth( box );
return 0;
}
output:
Width of box : 10
AIM: To write a program to illustrate this pointer
Source code:
#include <iostream>
using namespace std;
class Box {
public:
Box(double l = 2.0, double b = 2.0, double h = 2.0) {
cout<<"Constructor called." <<endl;
length = l;
breadth = b;
height = h;
}
double Volume() {
return length * breadth * height;
}
int compare(Box box) {
return this->Volume() >box.Volume();
}
private:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
};
int main(void) {
Box Box1(3.3, 1.2, 1.5); // Declare box1
Box Box2(8.5, 6.0, 2.0); // Declare box2
if(Box1.compare(Box2)) {
cout<< "Box2 is smaller than Box1" <<endl;
}
using namespace std;
class Box {
public:
Box(double l = 2.0, double b = 2.0, double h = 2.0) {
cout<<"Constructor called." <<endl;
length = l;
breadth = b;
height = h;
}
double Volume() {
return length * breadth * height;
}
int compare(Box box) {
return this->Volume() >box.Volume();
}
private:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
};
int main(void) {
Box Box1(3.3, 1.2, 1.5); // Declare box1
Box Box2(8.5, 6.0, 2.0); // Declare box2
if(Box1.compare(Box2)) {
cout<< "Box2 is smaller than Box1" <<endl;
}
else
{
cout<< "Box2 is equal to or larger than Box1" <<endl;
}
return 0;
}
output:
Constructor called.
Constructor called.
Box2 is equal to or larger than Box1
AIM: To write a Program to illustrate pointer to a class
#include <iostream>
using namespace std;
class Box {
public:
Box(double l = 2.0, double b = 2.0, double h = 2.0) {
cout<< "Box2 is equal to or larger than Box1" <<endl;
}
return 0;
}
output:
Constructor called.
Constructor called.
Box2 is equal to or larger than Box1
AIM: To write a Program to illustrate pointer to a class
Source code:
using namespace std;
class Box {
public:
Box(double l = 2.0, double b = 2.0, double h = 2.0) {
cout<<"Constructor called." <<endl;
length = l;
breadth = b;
height = h;
}
double Volume() {
return length * breadth * height;
}
private:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
};
int main(void) {
Box Box1(3.3, 1.2, 1.5); // Declare box1
Box Box2(8.5, 6.0, 2.0); // Declare box2
Box *ptrBox; // Declare pointer to a class.
length = l;
breadth = b;
height = h;
}
double Volume() {
return length * breadth * height;
}
private:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
};
int main(void) {
Box Box1(3.3, 1.2, 1.5); // Declare box1
Box Box2(8.5, 6.0, 2.0); // Declare box2
Box *ptrBox; // Declare pointer to a class.
// Save the address of first object
ptrBox = &Box1;
// Now try to access a member using member access operator
cout<< "Volume of Box1: " <<ptrBox->Volume() <<endl;
// Save the address of second object
ptrBox = &Box2;
// Now try to access a member using member access operator
cout<< "Volume of Box2: " <<ptrBox->Volume() <<endl;
return 0;
}
output:
Constructor called.
Constructor called.
Volume of Box1: 5.94
Volume of Box2: 102
Exercise -3 (Operator Overloading)
ptrBox = &Box1;
// Now try to access a member using member access operator
cout<< "Volume of Box1: " <<ptrBox->Volume() <<endl;
// Save the address of second object
ptrBox = &Box2;
// Now try to access a member using member access operator
cout<< "Volume of Box2: " <<ptrBox->Volume() <<endl;
return 0;
}
output:
Constructor called.
Constructor called.
Volume of Box1: 5.94
Volume of Box2: 102
Exercise -3 (Operator Overloading)
To write a program to Overload Unary, and Binary Operators as Member Function
AIM: To write a program to illustrate Unary operator as member function
AIM: To write a program to illustrate Unary operator as member function
Source code:
#include <iostream>
using namespace std;
class Distance {
private:
int feet; // 0 to infinite
int inches; // 0 to 12
public:
// required constructors
Distance() {
feet = 0;
inches = 0;
}
Distance(int f, int i) {
feet = f;
inches = i;
}
// method to display distance
void displayDistance() {
cout << "F: " << feet << " I:" << inches <<endl;
}
Distance operator++ () {
++feet;
++inches;
if(inches>=12)
{
feet++;
inches-=12;
}
return Distance(feet, inches);
}
using namespace std;
class Distance {
private:
int feet; // 0 to infinite
int inches; // 0 to 12
public:
// required constructors
Distance() {
feet = 0;
inches = 0;
}
Distance(int f, int i) {
feet = f;
inches = i;
}
// method to display distance
void displayDistance() {
cout << "F: " << feet << " I:" << inches <<endl;
}
Distance operator++ () {
++feet;
++inches;
if(inches>=12)
{
feet++;
inches-=12;
}
return Distance(feet, inches);
}
};
int main() {
Distance D1(11, 10);
Distance D3=++D1;
D3.displayDistance(); // display D1
return 0;
}
output:
Distance D1(11, 10);
Distance D3=++D1;
D3.displayDistance(); // display D1
return 0;
}
output:
F: 12 I:11
AIM : To write a program to illustrate Binary operator as a member function
AIM : To write a program to illustrate Binary operator as a member function
Source code:
#include <iostream.h>
class Distance {
private:
int feet; // 0 to infinite
int inches; // 0 to 12
public:
// required constructors
Distance() {
feet = 0;
inches = 0;
}
Distance(int f, int i) {
feet = f;
inches = i;
}
// method to display distance
void displayDistance() {
cout << "F: " << feet << " I:" << inches <<endl;
}
class Distance {
private:
int feet; // 0 to infinite
int inches; // 0 to 12
public:
// required constructors
Distance() {
feet = 0;
inches = 0;
}
Distance(int f, int i) {
feet = f;
inches = i;
}
// method to display distance
void displayDistance() {
cout << "F: " << feet << " I:" << inches <<endl;
}
Distance operator+ (Distance d) {
feet+=d.feet;
inches+=d.inches;
if(inches>=12)
{
feet++;
inches-=12;
}
return Distance(feet, inches);
}
};
int main() {
Distance D1(11, 10),D2(14,5);
Distance D3=D1+D2;
D3.displayDistance(); // display D3
return 0;
}
AIM : To overload the assignment operator "="
Source code:
#include <iostream.h>
class Distance {
private:
int feet; // 0 to infinite
int inches; // 0 to 12
public:
// required constructors
Distance() {
feet = 0;
inches = 0;
}
Distance(int f, int i) {
feet = f;
inches = i;
}
void operator = (const Distance &D ) {
feet = D.feet;
inches = D.inches;
}
// method to display distance
void displayDistance() {
cout << "F: " << feet << " I:" << inches << endl;
}
};
int main() {
Distance D1(11, 10), D2(5, 11);
cout << "First Distance : ";
D1.displayDistance();
cout << "Second Distance :";
D2.displayDistance();
// use assignment operator
D1 = D2;
cout << "First Distance :";
D1.displayDistance();
return 0;
}
output:
class Distance {
private:
int feet; // 0 to infinite
int inches; // 0 to 12
public:
// required constructors
Distance() {
feet = 0;
inches = 0;
}
Distance(int f, int i) {
feet = f;
inches = i;
}
void operator = (const Distance &D ) {
feet = D.feet;
inches = D.inches;
}
// method to display distance
void displayDistance() {
cout << "F: " << feet << " I:" << inches << endl;
}
};
int main() {
Distance D1(11, 10), D2(5, 11);
cout << "First Distance : ";
D1.displayDistance();
cout << "Second Distance :";
D2.displayDistance();
// use assignment operator
D1 = D2;
cout << "First Distance :";
D1.displayDistance();
return 0;
}
output:
First Distance : F: 11 I:10
Second Distance :F: 5 I:11
First Distance :F: 5 I:11
Exercise -4 (Inheritance)
To write C++ Programs and incorporating various forms of Inheritance
AIM :To implement Single Inheritance
Second Distance :F: 5 I:11
First Distance :F: 5 I:11
Exercise -4 (Inheritance)
To write C++ Programs and incorporating various forms of Inheritance
AIM :To implement Single Inheritance
Source code:
# include <iostream.h>
// base class or parent class
class student
{
protected:
int sno;
char sname[20];
};
//derived class or child class
class mpc: public student
{
int maths,phy,che;
public:
void getdata()
{
cout<<”Enter no and name?”;
cin>>sno>>sname;
cout<<”Enter MPC marks?”;
cin>>maths>>phy>>che;
}
void disdata()
{
cout<<sno<<”\t”<<sname<<endl;
cout<<maths<<”\t”<<phy<<”\t”<<che;
}
};
int main()
{
mpc s1;
s1.getdata();
s1.disdata();
return 0;
}
output:
Enter no and name?101 Raj
Enter MPC marks?70 80 90
101 Raj
70 80 90
AIM : To Implement multiple inheritance
Source code:
// base class or parent class
class student
{
protected:
int sno;
char sname[20];
};
//derived class or child class
class mpc: public student
{
int maths,phy,che;
public:
void getdata()
{
cout<<”Enter no and name?”;
cin>>sno>>sname;
cout<<”Enter MPC marks?”;
cin>>maths>>phy>>che;
}
void disdata()
{
cout<<sno<<”\t”<<sname<<endl;
cout<<maths<<”\t”<<phy<<”\t”<<che;
}
};
int main()
{
mpc s1;
s1.getdata();
s1.disdata();
return 0;
}
output:
Enter no and name?101 Raj
Enter MPC marks?70 80 90
101 Raj
70 80 90
AIM : To Implement multiple inheritance
Source code:
// deriving derived class from more than one base class
//base class
# include <iostream.h>
class Father
{
protected:
int age;
char name[20];
public:
void getdata()
{
cout<<"Enter Father age & name?";
cin>>age>>name;
}
};
//base class
class Mother
{
protected:
int age;
char name[20];
public:
void getdata()
{
cout<<"Enter Mother age & name?";
cin>>age>>name;
}
};
// derived class
class Child :public Father,Mother
{
protected:
int age;
char name[20];
public:
void getdata()
{
Father::getdata();
Mother::getdata();
cout<<"Enter Child age & name?";
cin>>age>>name;
}
void disdata(){
cout<<Father::age<<"\t"<<Father::name<<endl;
cout<<Mother::age<<"\t"<<Mother::name<<endl;
cout<<age<<"\t"<<name<<endl;
}
};
int main()
{
Child obj;
c.getdata();
c.disdata();
return 0;
}
output:
{
protected:
int age;
char name[20];
public:
void getdata()
{
cout<<"Enter Father age & name?";
cin>>age>>name;
}
};
//base class
class Mother
{
protected:
int age;
char name[20];
public:
void getdata()
{
cout<<"Enter Mother age & name?";
cin>>age>>name;
}
};
// derived class
class Child :public Father,Mother
{
protected:
int age;
char name[20];
public:
void getdata()
{
Father::getdata();
Mother::getdata();
cout<<"Enter Child age & name?";
cin>>age>>name;
}
void disdata(){
cout<<Father::age<<"\t"<<Father::name<<endl;
cout<<Mother::age<<"\t"<<Mother::name<<endl;
cout<<age<<"\t"<<name<<endl;
}
};
int main()
{
Child obj;
c.getdata();
c.disdata();
return 0;
}
output:
Enter Father age & name?rao 32
Enter Mother age & name?Rani 30
Enter Child age & name?balu 2
rao 32
Rani 30
balu 2
AIM: To Implement multi level inheritance:
Source code:
# include <iostream.h>
class A1
{
protected:
int age;
char name[20];
};
class A2:public A1
{
protected:
int height;
};
class A3:public A2
{
protected:
int weight;
public:
void getdata()
{
cout<<"Enter age & name?";
cin>>age>>name;
cout<<"Enter height & weight?";
cin>>height>>weight;
}
void disdata()
{
cout<<age<<"\t"<<name<<endl;
cout<<height<<"\t"<<weight<<endl;
}
};
int main()
{
A3 obj;
obj.getdata();
obj.disdata();
return 0;
}
output:
enter age & name? 18 Raj
enter height & weight? 167 60
18 Raj
167 60
AIM : To Implement hierarchical inheritance
{
protected:
int age;
char name[20];
};
class A2:public A1
{
protected:
int height;
};
class A3:public A2
{
protected:
int weight;
public:
void getdata()
{
cout<<"Enter age & name?";
cin>>age>>name;
cout<<"Enter height & weight?";
cin>>height>>weight;
}
void disdata()
{
cout<<age<<"\t"<<name<<endl;
cout<<height<<"\t"<<weight<<endl;
}
};
int main()
{
A3 obj;
obj.getdata();
obj.disdata();
return 0;
}
output:
enter age & name? 18 Raj
enter height & weight? 167 60
18 Raj
167 60
AIM : To Implement hierarchical inheritance
Source code:
# include <iostream.h>
class student
{
protected:
int rno;
};
class engg:public student
{
protected:
int eng,maths,total;
public:
void getdata()
{
cout<<"Enter rno,eng & maths marks?";
cin>>rno>>eng>>maths;
}
};
class cse:public engg
{
private:
int cpp;
public:
void getcsedata()
{
getdata(); // calling parent function
cout<<"Enter cpp marks?";
cin>>cpp;
}
void disdata()
{
cout<<rno<<" "<<eng<<" "<<
maths<<" "<<cpp<<endl;
total=eng+maths+cpp;
cout<<"Total = "<<total<<endl;
}
};
class ece:public engg
{
private:
int dld;
public:
void getecedata()
{
getdata(); // calling parent function
cout<<"Enter dld marks?";
cin>>dld;
}
void disdata()
{
cout<<rno<<" "<<eng<<" "<<
maths<<" "<<dld<<endl;
total=eng+maths+dld;
cout<<"Total = "<<total<<endl;
}
};
int main()
{
cse c;
c.getcsedata();
c.disdata();
ece e;
e.getecedata();
e.disdata();
return 0;
}
output:
class student
{
protected:
int rno;
};
class engg:public student
{
protected:
int eng,maths,total;
public:
void getdata()
{
cout<<"Enter rno,eng & maths marks?";
cin>>rno>>eng>>maths;
}
};
class cse:public engg
{
private:
int cpp;
public:
void getcsedata()
{
getdata(); // calling parent function
cout<<"Enter cpp marks?";
cin>>cpp;
}
void disdata()
{
cout<<rno<<" "<<eng<<" "<<
maths<<" "<<cpp<<endl;
total=eng+maths+cpp;
cout<<"Total = "<<total<<endl;
}
};
class ece:public engg
{
private:
int dld;
public:
void getecedata()
{
getdata(); // calling parent function
cout<<"Enter dld marks?";
cin>>dld;
}
void disdata()
{
cout<<rno<<" "<<eng<<" "<<
maths<<" "<<dld<<endl;
total=eng+maths+dld;
cout<<"Total = "<<total<<endl;
}
};
int main()
{
cse c;
c.getcsedata();
c.disdata();
ece e;
e.getecedata();
e.disdata();
return 0;
}
output:
Enter rno,eng & maths marks? 101 77 99
Enter cpp marks? 88
Total = 264
AIM : To implement hybrid inheritance
Source code:
Enter cpp marks? 88
Total = 264
AIM : To implement hybrid inheritance
Source code:
# include <iostream.h>
//base class
class student
{
protected:
int rno;
};
//der class
class test:public student
{
protected:
int sub1,sub2;
};
//base class
class sports
{
protected:
int score;
};
//der class
class result:public test,sports
{
int total;
public:
void getdata()
{
cout<<"Enter rno?";
cin>>rno;
cout<<"Enter ,sub1,sub2 and score?";
cin>>sub1>>sub2>>score;
}
void display()
{
cout<<rno<<" "<<sub1<<" "<<sub2<<endl;
cout<<score<<endl;
total=sub1+sub2+score;
cout<<"Total = "<<total<<endl;
}
};
int main()
{
result r;
r.getdata();
r.display();
}
output:
Enter rno?101
Enter ,sub1,sub2 and score?77 99 88
Total = 264
Exercise -5(Templates, Exception Handling)
AIM : To write a C++ Program to illustrate template class
Enter rno?101
Enter ,sub1,sub2 and score?77 99 88
Total = 264
AIM : To Illustrate constructor and destructor invokation order in single inheritance
source code:
#include<iostream>
using namespace std;
class base
{
public:
base()
{
cout<<"base class constructor"<<endl;
} ~base()
{
cout<<"base class destructor"<<endl;
}
};
class derived:public base
{
public:
derived()
{
cout<<"derived class constructor"<<endl;
} ~derived()
{
cout<<"derived class destructor"<<endl;
}
};
int main()
{
derived d;
return 0;
}
output:
Exercise -5(Templates, Exception Handling)
AIM : To write a C++ Program to illustrate template class
Source code
#include <iostream>
using namespace std;
template<class T1, class T2>
class A
{
T1 a;
T2 b;
public:
A(T1 x,T2 y)
{
a = x;
b = y;
}
void display()
{
std::cout << "Values of a and b are : " << a<<" ,"<<b<<std::endl;
}
};
int main()
{
A<int,float> d(5,6.5);
d.display();
return 0;
}
Output:
values of a and b are : 5,6.5
AIM : To write a Program to illustrate member function templates
Source code :
using namespace std;
template<class T1, class T2>
class A
{
T1 a;
T2 b;
public:
A(T1 x,T2 y)
{
a = x;
b = y;
}
void display()
{
std::cout << "Values of a and b are : " << a<<" ,"<<b<<std::endl;
}
};
int main()
{
A<int,float> d(5,6.5);
d.display();
return 0;
}
Output:
values of a and b are : 5,6.5
AIM : To write a Program to illustrate member function templates
Source code :
#include <iostream>
using namespace std;
template<class T> T add(T &a,T &b)
{
T result = a+b;
return result;
}
int main()
{
int i =2;
int j =3;
float m = 2.3;
float n = 1.2;
cout<<"Addition of i and j is :"<<add(i,j);
cout<<'\n';
cout<<"Addition of m and n is :"<<add(m,n);
return 0;
}
output:
Addition of i and j is :5 Addition of m and n is :3.5
AIM : To write a Program for Exception Handling Divide by zero
Source code :
using namespace std;
double division(int a, int b) {
if( b == 0 ) {
throw "Division by zero condition!";
}
return (a/b);
}
int main () {
int x = 50;
int y = 0;
double z = 0;
try {
z = division(x, y);
cout << z << endl;
}
catch (const char* msg) {
cerr << msg << endl;
}
return 0;
}
output:
Division by zero condition!
AIM : To write a Program to re throw an Exception
Source code :
using namespace std;
// Function for Exception Thrown
void exceptionFunction() {
// try block - inside Function
try {
throw 0;
}
catch (int i) {
cout << "\nIn Function : Wrong Input :" << i;
throw;
}
}
int main() {
int var = 0;
cout << "Simple C++ Program for Re throwing Exception Handling : In Function\n";
try {
exceptionFunction();
}
catch (int ex) {
cout << "\nIn Main : Wrong Input :" << ex;
}
return 0;
}
output:
Simple C++ Program for Re throwing Exception Handling : In Function
In Function : Wrong Input :0
In Main : Wrong Input :0