Você está na página 1de 14

1.

Single Inheritance

When a subclass inherits only from one base, it is known as Single Inheritance.

BASE CLASS - A BASE CLASS - C

Derived Class - X Derived Class - Y

2. Multiple Inheritance

When a subclass inherits from multiple base classes, it is known as Multiple Inheritance.

BASE CLASS - A BASE CLASS - B

Derived Class - Y

3. Hierarchical inheritance

When many subclasses inherit from a single base class, it is known as Hierarchical
inheritance.

BASE CLASS - A

Derived Class - X Derived Class - Y

4. Multilevel Inheritance

When a subclass inherits from a class that itself inherits from another class, it is known
as Multilevel Inheritance. i.e. A derived class becomes base class of another class.
BASE CLASS - A

Derived Class - X Derived Class of A and Base class of Y

Derived Class - Y Derived Class of X and Base class of Z

Derived Class - Z

5. Hybrid Inheritance

Hybrid Inheritance combines two or more forms of Inheritance.

Eg., When a sub class inherit from multiple base classes and all of its base classes
inherits from a single base class, This form of Inheritance is known as Hybrid
Inheritance.

BASE CLASS - H

BASE CLASS - A BASE CLASS - B

Derived Class - Y
1. Single inheritance

When a subclass inherits only from one base, it is known as Single Inheritance.

Syntax:

class derived-class-name : visibility-mode base-class-name

…….

};

EXAMPLE for Single Inheritance:

#include<iostream.h>

#include<conio.h>

#include<stdio.h>

const int LEN=25;

class Employee

private:

char name[LEN];

unsigned long enumber;

public:

void getdata()

cout<< "Enter Name:";


gets(name);

cout<< "Enter Employee Number";

cin>>enumber;

void putdata()

cout<<"Name:"<<name<<"\n";

cout<<"Employee Number:"<<enumber<<"\n";

cout<<"Basic Salary:"<<basic<<"\n";

protected:

float basic;

void getbasic()

cout<<"Enter Basic Salary";

cin>>basic;

};

class Manager : public Employee

private:

char title[LEN];

public:

void getdata()

{
Employee::getdata();

getbasic();

cout<<"Enter Title:";

gets(title); cout <<"\n";

void putdata()

Employee::putdata();

cout<<"Title:"<<title<<"\n";

};

void main()

clrscr();

Manager m1,m2;

cout<<"Manager1\n"; m1.getdata();

cout<<"Manager2\n"; m2.getdata();

cout<<"\t\tManager1 Details:\n"; m1.putdata();

cout<<"\t\tManager2 Details:\n"; m2.putdata();

getch();

}
Function Overriding:

When the base class and derived class have member functions with exactly the same
name, same return-type, and same arguments list, then it is said to be function
overriding.

If you create an object of the derived class and call the member function which exists in
both classes (base and derived), the member function of the derived class is invoked and
the function of the base class is ignored.

This feature in C++ is known as function overriding.

Difference between Function overloading and Function overriding:


1) Function Overloading happens in the same class when we declare same functions
with different type of arguments/number of arguments/return type in the same class.
Function Overriding is happens in the child class when child class overrides parent class
function.

2) In function overloading function signature should be different for all the overloaded
functions. In function overriding the signature of both the functions (overriding function
and overridden function) should be same.

3) Overloading happens at the compile time that’s why it is also known as compile time
polymorphism while overriding happens at run time which is why it is known as run
time polymorphism.

4) In function overloading we can have any number of overloaded functions. In function


overriding we can have only one overriding function in the child class.
2. Multiple Inheritance

When a subclass inherits from multiple base classes, it is known as Multiple Inheritance.

Example code:

#include<iostream.h>

#include<conio.h>

class chairperson

long CID;

char CName[20];

protected:

char Description[40];

void allocate();

public:

chairperson();

void assign();

void show();

};

Class Director

int DID;

char DName[20];

Protected:

char profile[30];

public:
Director();

void input();

void output();

};

class company: private Chairperson, public Director

int CID;

char City[20], Country[20];

public:

company();

void Enter();

void Display();

};

void main()

clrscr();

Company comp1, comp2;

comp1.Enter();

comp1.Display();

comp2.Enter();

comp2.Display();

}
Consider, the following Scenarios of Inheritance,

(i)
BASE CLASS - A

DERIVED CLASS - B DERIVED CLASS - C

Derived Class - Y

(ii)
Base CLASS – A Base CLASS – B (With
(With same same member in Class A)
member in Class B)

Base CLASS - C

In Scenario1:

As B & C inherits Base Class A, they both have copies of A.

However, Y Inherits both B and C. This means that there will be two copies of Base class A in Derived
class Y.

Then when you are accessing the member of Base class A using the object of Derived class Y, the
Statement becomes ambiguous.

There are two ways to resolve this ambiguous

1. Use of Scope Resolution operator:: and call using class name.

Ex: obj.B1::a=25;

2. When 2 or more classes are derived from common base class, you can prevent multiple copies of the
base class from being present in an object of the derived class by declaring the base class as virtual when
it is inherited.
This is accomplished by putting the keyword virtual before the base class’ name when it is inherited.

Syntax:

Class base

};

class D1:virtual public base

};

class D2:virtual public base

};

class D3:virtual public D1, virtual public D2

};

4. Multilevel Inheritance

When a subclass inherits from a class that itself inherits from another class, it is known
as Multilevel Inheritance. i.e. A derived class becomes base class of another class.

Example:

5. Hybrid Inheritance

Hybrid Inheritance combines two or more forms of Inheritance.

Eg., When a sub class inherit from multiple base classes and all of its base classes
inherits from a single base class, This form of Inheritance is known as Hybrid
Inheritance.
Nesting of Classes:

One class may have another class defined within it known as nested class.

One other way of doing so is that a class contains objects of other classes as its members.

Example code:

class X { };

class Y { };

class Z

X obj1;

Y obj2;

};

All the objects of class Z will be containing objects obj1 and obj2 of classes X and Y types respectively.

This type of relationship is called containership or Nesting.

Definition:

When a class contains objects of other class types as its member, it is referred to as Containment or
Composition or Aggregation or Containership or Nesting.

You can also say that Class Z has Has-A Relationship with class X and class Y objects.

Relationship between classes:

(i) IS-A Relationship

(ii) HAS-A Relationship

(iii) HOLDS-A Relationship


(i) IS-A Relationship

When a class inherits from another class, is known as IS-A relationship between the classes.

(ii) HAS-A Relationship

When a class contains objects of other class types as its member, the relationship between the classes is
referred HAS-A Relationship.

(iii) HOLDS-A Relationship

A similar relation between two classes is HOLDS-A relation. It is similar to HAS-A relationship but
ownership is missing in HOLDS-A relationship. A class that indirectly containds another object i.e, via
pointer or reference, is said to have HOLDS-A relationship with class whose object is its indirect
member,

Example code:

class Fax

};

class modem

Fax *connect;

long bps;

char type;

public:

modem(long sp, char c, Fax *c) : connect(c)

bps=sp;

type=c;

};
In the example code, modem object has HOLDS-A relationship with a Fax object.

Você também pode gostar