INHERITANCE

From WikiEducator
Jump to: navigation, search

INHERITANCE

The process of defining a new class in terms of existing class is called inheritance. The existing class is called the base class and new class is called the derived class.

It is a concept of linking two or more classes with each other in a hierarchical manner so that their properties and functions can be shared. The public and protected members of the base class will be inherited and are accessible in the derived class depending upon the visibility mode.

This leads to the biggest advantage of re-usability of the members and avoids redundancy.

TYPES OF INHERITANCE

SINGLE INHERITANCE

When one class is inherited from a single base class.


A----B

Here A is the base class and B is the derived class of A.

HIERARCHICAL INHERITANCE

In this type,two classes are derived from a single base case.


A ---- B

A ----C

Here A is the base class of two derived classES i.e. B and C

MULTIPLE INHERITANCE

In this a new class is inherited from two base classes.


A----C

B----C

Here C is the derived class of A and B.

MULTILEVEL INHERITANCE

A----B----C

Here A is the base class of B and B is the base class of A.

HYBRID INHERITANCE

It is the combination of two or more types of inheritance.

QUIZ

class Teacher
{
char TNo[5],
TName[20],
Deptf[l0];
int Workload;
protected:
float Salary;
void AssignSal(float);
public:
Teacher( ) ;
void TEntry( ) ;
void TDisplay( );
};
class Student
{
char Admno[10],
SName[20],
Stream[10];
protected:
int Attendance,
TotMarks;
public:
Student( );
void SEntry( );
void SDisplay( );
};
class School : public Student, public Teacher
{
char SCode[10],
SchName[20];
public:
School ( ) ;
void SchEntry( );
void SchDisplay( );
};


1. Which type of Inheritance is depicted by the above example

multi level.
single inheritance .
hybrid inheritance.
multiple inheritance.

2. Write name of all the data members accessible from member functions of class Student.

Admno, SName, Stream,Attendance, TotMarks.
Stream,Attendance .
SEntry( ),SDisplay( ),Admno, SName.
all of above.

3. What is the size of class school.

110 bytes.
78 bytes .
30 bytes.
115 bytes.

4. Name the base class name

student.
school .
schentry.
teacher.

5. Write name of all the member functions accessible from object of class school

SchEntry( ), SchDisplay() .
SEntry( ), SDisplay( ).
SchEntry( ), SchDisplay(),SEntry( ), SDisplay( ),TEntry( ), TDisplay( ).
SchDisplay(),SEntry( ), SDisplay( ),TEntry( ).

Your score is 0 / 0