Assignment 2 - Sol
Assignment 2 - Sol
Single inheritance:
1 base class and 1 drive class.
MULTIPLE INHERITANCE
Many base classes and 1 drive class.
Multilevel inheritance
Every base class is inherited from drive classes.
HIERARCHICAL INHERITANCE
Hierarchical inheritance, several classes are derived from common base class. The base
class includes all the common features of derived classes
HYBRID INHERITANCE:
A hybrid inheritance is a combination of more than one types of inheritance.
As in other inheritance, based on the visibility mode used or access specifier used while deriving,
the properties of the base class are derived. Access specifier can be private, protected or public.
In this class c can use the functions and attributes of class A and class B.
6. Explain the calling sequence for constructor and destructor when we instantiate a subclass.
In the inheritance the constructor of base class run and compile first and then the
constructor of drive class in last. the drive class destructor run first and base class
destructor in last.
CODE:
#include<iostream>
using namespace std;
class A //class A
{
int a;
public:
A() //defalut constructor
{}
A(int x) //parameterized constuctor
{
a = x;
}
void show() //disply funcation
{
cout << "class A have" << endl;
cout << "a = " << a << endl;
}
~A() //destructure
{
cout << "class A object is destroyed" << endl;
}
};
class B :public A
{
int y;
public:
B(){} //defalut constructor
B(int b, int x) :A(x) //parameterized constuctor
{
y = b;
}
void showB() //disply funcation
{
};
int main()
{
C obj1(2,3,5);
obj1.showc();}
7. Design an application for a publication company. Create a class called Publication that
stores the Book_Title(string), Book_Price(float). From this class, derive
two classes Book that stores page_count (int) and Tape that stores
playing_duration(int) in minutes. Each of these classes have getData() method
to get the data from the user and displayData() to display data to the user. Write a main
function to test the Book and Tape classes by creating instances of them. Ask the user to
enter data using the getData method and then display the same back to the user.
#include<iostream>
#include<string>
using namespace std;
class Publication
{string Book_Title;
float Book_Price;
public:
void input()
{cout<<"enter the book tital"<<endl;
cin>>Book_Title;
cout<<"enter the book price"<<endl;
cin>>Book_Price;
}
void output()
{cout<<" book tital = "<<Book_Title<<endl;
cout<<" book price = "<<Book_Price<<endl;}
};
class book:public Publication
{int page_count;
public:
void get_data()
{cout<<"enter the total pages of book"<<endl;
cin>>page_count;
}
void show_data()
{cout<<"the total pages of book = "<<page_count<<endl;
}
};
class tape:public book
{ int playing_duration;
public:
void get_data1()
{cout<<"enter the time in minutes"<<endl;
cin>>playing_duration;
}
void show_data1()
{cout<<"the total time= "<<playing_duration<<"mint"<<endl;;
}
};
int main()
{ tape obj;
obj.input();
obj.get_data();
obj.get_data1();
obj.output();
obj.show_data();
obj.show_data1();}