You are on page 1of 11

1.

C++ Program to Inherit a Student class from Person Class


printing the properties of the Student

#include<iostream>
using namespace std;

class std_basic_info
{
private:
char name[30];
int age;
char gender;
public:
void getBasicInfo(void);
void putBasicInfo(void);
};

void std_basic_info :: getBasicInfo(void)


{
cout << "Enter Student's Basic Information :\n" << endl;
cout << "Name : ";
cin >> name;
cout << "Age : ";
cin >> age;
cout << "Gender : ";
cin >> gender;
}

void std_basic_info :: putBasicInfo(void)


{
cout << "\nName : " << name << ", Age : " << age << ", Gender : "
<< gender << endl;
}

class std_result_info : public std_basic_info


{
private:
int totalM;
float perc;
char grade;
public:
void getResultInfo(void);
void putResultInfo(void);
};

void std_result_info :: getResultInfo(void)


{
cout << "\nEnter Student's Result Information :\n" << endl;
cout << "Total Marks : ";
cin >> totalM;
perc = (float)((totalM*100)/500);
cout << "Grade : ";
cin >> grade;
}

void std_result_info :: putResultInfo(void)


{
cout << "Total Marks : " << totalM << ", Percentage : " << perc << ",
Grade : " << grade << endl;
}

int main()
{
std_result_info std;

std.getBasicInfo();
std.getResultInfo();

std.putBasicInfo();
std.putResultInfo();

return 0;
}
2. Write a C++ program which shows all types of inheritance.

#include<iostream>
using namespace std;
class A
{
public:
int a=200;
};

class B : virtual public A


{
public:
int b=100;
void addAB()
{
cout<<endl<<"a+b="<<a+b;
}
}b1;

/*Here class B inherits a from A. It is known as single inheritance*/

class C
{
public:
int c=20;
};

class D : public A, public C


{
public:
int d=10;
void addADC()
{
cout<<endl<<"a+c+d="<<a+c+d;
}
}d1;

/*Here D inherits a and c from A and C respectively. This is known as


Multiple Inheritance*/

class E :virtual public A


{
public:
int e=80;
void addAE()
{
cout<<endl<<"a+e="<<a+e;
}
}e1;

/*Here as you can see B,D and E inherits a from A. This is known as
Hierarchical Inheritance*/

class F: public E
{
public:
int f=90;
void addAEF()
{
cout<<endl<<"a+e+f="<<a+e+f;
}
}f1;

/*Here F inherited e from E and a from A through E.This is known as


Multilevel Inheritance*/

class G: public B, public E


{
public:
int g=30;
void addABEG()
{
cout<<endl<<"a+b+e+g="<<a+b+e+g<<endl;
}
}g1;

/*Here G is inheriting from B and E who themselves inherited from A.


This is known as Hybrid Inheritance*/

int main()
{
b1.addAB();
d1.addADC();
e1.addAE();
f1.addAEF();
g1.addABEG();
return 0;
}
3. Write C++ program to implement Virtual base class.

#include<iostream>
using namespace std;
class A
{
public:
int a;
};

class B : virtual public A


{
public:
int b;
};
class C : virtual public A
{
public:
int c;
};

class D : public B, public C


{
public:
int d;
};

int main()
{

D obj;

obj.a = 10;
obj.a = 100;

obj.b = 20;
obj.c = 30;
obj.d = 40;

cout<< "\n A : "<< obj.a;


cout<< "\n B : "<< obj.b;
cout<< "\n C : "<< obj.c;
cout<< "\n D : "<< obj.d;
return 0;

4. Create a class named Shape with a function that prints "This is


a shape". Create another class named Polygon inheriting the
Shape class with the same function that prints "Polygon is a
shape". Create two other classes named Rectangle and
Triangle having the same function which prints "Rectangle is a
polygon" and "Triangle is a polygon" respectively. Again,
make another class named Square having the same function
which prints "Square is a rectangle"

Now, try calling the function by the object of each of these


classes.

#include<iostream>
using namespace std;
class Shape
{
public:
void printinfo()
{
cout<<endl<<"This is a Shape";
}
};

class Polygon : public Shape


{
public:
void printinfo()
{
cout<<endl<<"Polygon is a Shape";
}
};

class Rectangle : public Polygon


{
public:
void printinfo()
{
cout<<endl<<"Rectangle is a Polygon";
}
};

class Triangle : public Polygon


{
public:
void printinfo()
{
cout<<endl<<"Triangle is a Polygon";
}
};

class Square : public Rectangle


{
public:
void printinfo()
{
cout<<endl<<"Square is a Rectangle\n";
}
};

int main()
{
Shape s;
Polygon p;
Rectangle r;
Triangle t;
Square sq;
s.printinfo();
p.printinfo();
r.printinfo();
t.printinfo();
sq.printinfo();
return 0;
}

5. Write a C++ program to override getWeight() function. Create


three classes Animal , Herbivores and Carnivores where
Animal is base class and Herbivores and Carnivores are
derived class.

#include<iostream>
using namespace std;
class Animal
{
public:
int weight;
void getweight()
{
cout<<endl<<"Enter Weight:";
cin>>weight;
}
};

class Herbivores : public Animal


{
public:
void getweight()
{
cout<<endl<<"Enter Weight of Herbivore:";
cin>>weight;
}
};

class Carnivores : public Animal


{
public:
void getweight()
{
cout<<endl<<"Enter Weight of Carnivore:";
cin>>weight;
}
};

int main()
{
Herbivores h;
Carnivores c;
h.getweight();
c.getweight();
return 0;
}

You might also like