[go: up one dir, main page]

0% found this document useful (0 votes)
53 views6 pages

Assignment 2 - Sol

The document discusses class inheritance in object-oriented programming. It defines class inheritance as objects of one class inheriting properties from another class. It then describes different types of inheritance such as single, multiple, multilevel, hierarchical, and hybrid inheritance. Multilevel inheritance allows a class to inherit from another inherited class. The document also defines inheritance syntax and discusses the use of public, private, and protected access modifiers in inheritance, as well as the calling sequence for constructors and destructors in subclasses. Finally, it provides an example program to demonstrate inheritance by creating Publication, Book, and Tape classes.

Uploaded by

attayyabkhan
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)
53 views6 pages

Assignment 2 - Sol

The document discusses class inheritance in object-oriented programming. It defines class inheritance as objects of one class inheriting properties from another class. It then describes different types of inheritance such as single, multiple, multilevel, hierarchical, and hybrid inheritance. Multilevel inheritance allows a class to inherit from another inherited class. The document also defines inheritance syntax and discusses the use of public, private, and protected access modifiers in inheritance, as well as the calling sequence for constructors and destructors in subclasses. Finally, it provides an example program to demonstrate inheritance by creating Publication, Book, and Tape classes.

Uploaded by

attayyabkhan
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/ 6

Assignment 2 PLO1, CLO2

1. What do you understand by class inheritance?


CLASS INHERITANCE:
Inheritance Is One of the Important Feature of the Oop. Inheritance Is the Method by Which
Objects of One Class Gets the Properties of Another Class.
In the inheritance we made a base class and derived other classes from base class. The new
class inherits all the member variables and functions of the class it is based on.

2. Describe different types of inheritance.

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.

3. Describe multiple level inheritance.


Multilevel inheritance
If a class is derived from another derived class, then it is called multilevel inheritance.
As shown in above block diagram, class C has class B and class A as parent classes. Depending
on the relation the level of inheritance can be extended to any level.

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.

4. Define and describe the inheritance syntax.


Answer
class base_class {
... .. ...}
class first_derived_class: public base_class {... .. ...
}
class second_derived_class: public base_class {
... .. ...}
class third_derived_class: public base_class {
... .. ...}
5. Explain the use of public, private, protected access modifiers in inheritance.
Public:
Public access modifier is access in same class derive class and as well as in outside the
code.
Private:
Private access modifier is access in same class it is not accessible in other class and in
outside the code.
Protected:
Protected access modifier is access in same class as well as in drive class it is not access
in outside the code.

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
{

cout << "class b have" << endl;


cout << "b = " << y << endl;
}
~B() //destructure
{
cout << "class B object is destroyed" << endl;
}
};
class C :public B
{
int z;
public:
C(int c, int x, int y) :B(x,y)
{
z = c;
}
void showc()
{
show();
showB();
cout << "class c have" << endl;
cout << "c = " << z << endl;
}
~C()
{
cout << "class C object is destroyed" << endl;
}

};
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();}

You might also like