Sie sind auf Seite 1von 12

Practical No.

7
AIM: A. Write a C++ program to study function overloading.
B. Write a C++ program to adding 2 complex numbers using an overloaded operator.

Theory:

C++ Overloading (Operator and Function)

C++ allows you to specify more than one definition for a function name or an operator in the same
scope, which is called function overloading and operator overloading respectively.
An overloaded declaration is a declaration that had been declared with the same name as a previously
declared declaration in the same scope, except that both declarations have different arguments and
obviously different definition (implementation)

Function overloading in C++:


The definition of the function must differ from each other by the types and/or the number of arguments in
the argument list. You can not overload function declarations that differ only by return type.

Operator overloading in C++:


Overloaded operators are functions with special names the keyword operator followed by the symbol for
the operator being defined. Like any other function, an overloaded operator has a return type and a
parameter list.

Program A

#include <iostream.h>
/* Function arguments are of different data type */
long add(long, long);
float add(float, float);
void display(char []); // print the string passed as argument
void display(char [], char []);

int main()
{
long a, b, x;
float c, d, y;
char first[] = "C programming";
char second[] = "C++ programming";
cout << "Enter two integers\n";
cin >> a >> b;
x = add(a, b);

cout << "Sum of integers: " << x << endl;


cout << "Enter two floating point numbers\n";
cin >> c >> d;
y = add(c, d);
cout << "Sum of floats: " << y << endl;
display(first);
display(first, second);
return 0;
}
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;
}
void display(char s[])
{
cout << s << endl;
}
void display(char s[], char t[])
{
cout << s << endl << t << endl;
}

Output: Execute program and write output


Program B

#include<iostream.h>

#include<conio.h>
class complex
{
float real;
float imag;
public:
complex(float tempreal=0, float tempimag=0)
{
real=tempreal;
imag=tempimag;
}
complex add(complex comp2)
{
float tempreal;
float tempimag;
tempreal=real+comp2.real;
tempimag=imag+comp2.imag;
return complex(tempreal, tempimag);
}
complex operator +(complex comp2)
{
float tempreal;
float tempimag;
tempreal=real+comp2.real;
tempimag=imag+comp2.imag;
return complex(tempreal, tempimag);
}
void display()
{
cout<<real<<+<<imag<<i \n;
}
};
void main
{
complex comp1(10,20);
complex comp2(20,30);
complex compresult1, compresult2;
compresult1=comp1.add(comp2);
compresult1.display();
compresult2=comp1+comp2;
compresult2.display();

Output: Execute program and write output


Conclusion:

Practical No. 8
AIM: A. Write a C++ Program to study Multilevel Inheritance. Program should implement
following hierarchy.

B. Write a C++ Program to study Multiple Inheritance. Program should implement following

hierarchy.

Theory
What is Inheritance?
Inheritance is the process by which objects of one class acquire the properties of objects of another class.
The most important advantage of the inheritance is that it permits code reusability. The mechanism of
deriving a new class from an existing class is called inheritance.

5 types of inheritances are there: Single inheritance, multilevel inheritance, multiple inheritance,
hierarchical inheritance and hybrid inheritance.
This is an example for multilevel inheritance.
Here class result is inherited from class mark and class mark is inherited from class student.
Program A:

#include<iostream.h>
#include<conio.h>
class student
{
protected :
char name[15];
int roll;
public:
void get();
void disp();
};
class mark: public student
{ protected :
int m,e;
public:
void get();
void disp();
};
class result: public mark
{ private :
int t;
public:
void get();
void disp();
};
void student::get()
{ cout<<"\n Enter name of the student : \t";
cin>>name;
cout<<"\n Enter roll number : \t";
cin>>roll;
}
void student::disp()
{ cout<< name <<" \t "<< roll <<" \t ";}
void mark::get()
{ student::get();
cout<<"\n Mark in English in 50 : \t ";
cin>>e;

cout<<"\n Mark in Maths in 50 : \t ";


cin>>m;
}
void mark::disp()
{ student::disp();
cout << e << " \t " << m << " \t " ;
}
void result::get()
{ mark::get();
t=e + m;
}
void result::disp()
{ mark::disp();
if (t >= 36)
cout<<"PASSED \n";
else
cout<<"FAILED \n";
}
void main()
{ int i,n;
clrscr();
result r[5];
cout<<"How many students :";
cin>>n;
for(i=0;i<n;i++)
r[i].get();
cout<<"\n\nName\tRoll\tEnglish\tMaths\tResult\n---------------------------------------\n";
for(i=0;i<n;i++)
r[i].disp();
getch();
}
Output: Execute program and write output
Program B:

#include <iostream.h>
class MyFather
{
protected:
char* EyeColor;
char* HairType;
double FamSaving;

int FamCar;
public:
MyFather(){}
~MyFather(){}
char* ShowEyeColor();
char* ShowHairType();
long double FamilySaving();
int FamilyCar();
};
class MyMother
{.
protected:
char* EyeColor;
char* HairType;
int FamHouse;
public:
MyMother(){}
~MyMother(){}
char* ShowMotherEye();
char* ShowMotherHair();
int FamilyHouse();
};
class MySelf:public MyFather
{
char* HairType;
char* Education;
public:
MySelf(){ }
~MySelf(){ }
char* ShowMyHair();
char* ShowMyEducation();
};
class MySister:public MyFather,public MyMother
{
char* SisEye;
float MonAllowance;
public:
MySister(){}

~MySister(){}
char* ShowSisEye();
float ShowSisAllownace();
};
char* MyFather::ShowEyeColor()
{
return EyeColor = "Brown";
}
char* MyFather::ShowHairType()
{
return HairType = "Straight Gray";
}
long double MyFather::FamilySaving()
{
return FamSaving = 100000L;
}
int MyFather::FamilyCar()
{
return FamCar = 4;
}
char* MyMother::ShowMotherEye()
{
return EyeColor = "Blue";
}
char* MyMother::ShowMotherHair()
{
return HairType = "Curly Blonde";
}
int MyMother::FamilyHouse()
{
return FamHouse = 3;
}
char* MySelf::ShowMyHair()
{
return HairType = "Straight Black";
}
char* MySelf::ShowMyEducation()

{
return Education = "Post Graduate";
}
char* MySister::ShowSisEye()
{
return SisEye = "Black";
}
float MySister::ShowSisAllownace()
{
return MonAllowance = 1000.00;
}
int main()
{
// instantiate the objects...
MyFather ObjFat;
MyMother ObjMot;
MySelf ObjSelf;
MySister ObjSis;
cout<<"--My father's data--"<<endl;
cout<<"His eye:
"<<ObjFat.ShowEyeColor()<<"\n"
<<"His hair:
"<<ObjFat.ShowHairType()<<"\n"
<<"Family Saving: USD"<<ObjFat.FamilySaving()<<"\n"
<<"Family Car: "<<ObjFat.FamilyCar()<<" cars.\n";
cout<<"\n--My mother's data--"<<endl;
cout<<"Her eye: "<<ObjMot.ShowMotherEye()<<endl;
cout<<"Her hair: "<<ObjMot.ShowMotherHair()<<endl;
cout<<"Our family house: "<<ObjMot.FamilyHouse()<<" houses."<<endl;
...
cout<<"\n--My data--"<<endl;
cout<<"My Hair: "<<ObjSelf. ShowMyHair()<<endl;
cout<<"My family saving: USD"<<ObjSelf.MySelf::FamilySaving()<<endl;
cout<<"My family car: "<<ObjSelf.MySelf::FamilyCar()<<" cars."<<endl;
cout<<"My education: "<<ObjSelf.ShowMyEducation()<<endl;
cout<<"\n--My sister's data--"<<endl;
cout<<"Her eye: "<<ObjSis. ShowSisEye()<<endl;
cout<<"Our family saving: USD"<<ObjSis.MySister::FamilySaving()<<endl;
cout<<"Our family car: "<<ObjSis.MySister::FamilyCar()<<" cars."<<endl;
cout<<"Our
family
house:
"<<ObjSis.MySister::FamilyHouse()<<"

houses."<<endl;
cout<<"Her monthly allowances: USD"<<ObjSis.ShowSisAllownace()<<endl;
return 0;
}

Output:
Execute program and write down output
Conclusion:

Practical No. 7
AIM: Write a C++ program to study abstract class and virtual function.

Theory: Abstract classes act as expressions of general concepts from which more specific
classes can be derived. You cannot create an object of an abstract class type; however, you can
use pointers and references to abstract class types.
A class that contains at least one pure virtual function is considered an abstract class. Classes
derived from the abstract class must implement the pure virtual function or they, too, are
abstract classes.
A virtual function is declared as "pure" by using the pure-specifier

Program B:

#include <iostream>
using namespace std;
// Base class
class Shape
{
public:
// pure virtual function providing interface framework.
virtual int getArea() = 0;

void setWidth(int w)
{
width = w;
}
void setHeight(int h)
{
height = h;
}
protected:
int width;
int height;
};
// Derived classes
class Rectangle: public Shape
{
public:
int getArea()
{
return (width * height);
}
};
class Triangle: public Shape
{
public:
int getArea()
{
return (width * height)/2;
}
};
int main(void)
{
Rectangle Rect;
Triangle Tri;
Rect.setWidth(5);
Rect.setHeight(7);
// Print the area of the object.
cout << "Total Rectangle area: " << Rect.getArea() << endl;
Tri.setWidth(5);
Tri.setHeight(7);
// Print the area of the object.
cout << "Total Triangle area: " << Tri.getArea() << endl;
return 0;
}

Output:
Execute program and write down output
Conclusion: