inheritance in c++

Post on 17-Dec-2014

651 Views

Category:

Software

3 Downloads

Preview:

Click to see full reader

DESCRIPTION

As per S.Y.BSc.(Computer Science) Syallbus.

TRANSCRIPT

Programming Language :-

C++ Presentation :-

Topic

INHERITANCE

Presentation Group Members :-

08. Ankita

14. Ashutosh

18. Samiksha

19. Laxman

S.Y.BSc.(Computer Science)

ROAD MAP ......

I

Types of Inheritance :-

Demo !!!!

Doubt & Question Session ????

WHAT IS INHERITANCE ???

• The Mechanism of deriving a new class from an old one is called inheritance (or derivation).

• The old class is referred to as the base class and the new one is called the derived class or subclass.

• Inheritance is the capability of one class to inherit properties from another class.

• The most important advantage of inheritance is code reusability.

NEED FOR INHERITANCE :-

• One major reason behind this is the capability to express the inheritance relationship.

• Another reason is the idea of reusability.

• One reason is transitive nature of inheritance.

Base & Derived Classes:

A class can be derived from more than one classes, which means it can inherit data and functions from multiple base classes. To define a derived class, we use a class derivation list to specify the base class(es). A class derivation list names one or more base classes and has the form:

class derived-class: access-specifier base-class

Where access-specifier is one of public, protected, or private, and base-class is the name of a previously defined class. If the access-specifier is not used, then it is private by default.

-: TYPES OF INHERITANCE :-

Single Inheritance

Multilevel Inheritance

Multiple Inheritance

Hierarchal Inheritance

Hybrid Inheritance

SINGLE INHERITANCE

SINGLE INHERITANCE :-

• Single inheritance enables a derived class to inherit properties and behavior from a single parent class.

• It allows a derived class to inherit the properties and behavior of a base class, thus enabling code reusability as well as adding new features to the existing code.

• This makes the code much more elegant and less repetitive.

• Inheritance is one of the key features of object-oriented programming (OOP). 

• If the class hierarchy contains only two classes one is a base class and another is derived class then this form of class hierarchy is known as single Inheritance.

Syntax :class base_class_name

{

access specifier:

:

};

Class derived_class_name : access specifier base_class_name

{

access specifier:

:

};

class A //Base Class

{

public:

int a, b;

void getdata ();

};

class B : public A

//Derived Class

{

int c;

public:

void calculate ();

void display ();

};

Example :-

void A :: getdata ()

{

cout<<"\n\n Enter value a:";

cin>>a;

cout<<"Enter value b:";

cin>>b;

}

void B :: calculate ()

{

c = (a + b);

}

void B :: display ()

{

cout<<"\n"<<a<<"+"<<b<<"="<<c;

}

getch ();

/* Output

Enter value a:2

Enter value b:5

2+5=7 */

void main ()

{

B b;

clrscr ();

b.getdata ();

b.calculate ();

b.display ();

Inheritance visibility mode :-

Private Inheritance:

It is the inheritance facilitated by private visibility mode. In private inheritance ,the protected and public members of base class become private members of the derived class.

Public Inheritance:

It is the inheritance facilitated by public visibility mode. In public inheritance ,the protected  members of base class become protected members of the derived class and public members of the base class become public members of derived class.

Protected Inheritance:

It is the inheritance facilitated by protected visibility mode.

In protected inheritance ,the protected and public members of base class become protected members of the derived class.

Access Public Protected Private

Same class/Base class

yes yes yes

Derived classes

yes yes no

Outside classes

yes no no

Public inheritance

Base access specifierDerived access

specifierDerived class access? Public access?

Public Public Yes Yes

Private Private No No

Protected Protected Yes No

Private inheritance

Base access specifierDerived access

specifierDerived class access? Public access?

Public Private Yes No

Private Private No No

Protected Private Yes No

Protected inheritance

Base access specifierDerived access

specifierDerived class access? Public access?

Public Protected Yes No

Private Private No No

Protected Protected Yes No

Multiple Inheritance :-

MULTIPLE INHERITANCE :-

Def :-

When a subclass inherits from multiple base classes ,

it is known as multiple inheritance.

class derived class_name:<access_specifier> class1_name,<access_specifier> class2_name,………………………………………..,

<access_specifier> classn_name

{

:

: //members of derived class

:

}

SYNTAX:

class sub : public superA, private superB

{

:

: //members of class sub

:

}

FOR INSTANCE:

EXAMPLE:

class student

{

protected:

int rno,m1,m2;

public:

void get()

{

cout<<"Enter the Roll no :";

cin>>rno;

cout<<"Enter the two marks :";

cin>>m1>>m2;

}

};

class sports

{

protected:

int sm; // sm = Sports mark

public:

void getsm()

{

cout<<"\nEnter the sports mark :";

cin>>sm;

}

};

class statement:public student,public sports

{

int tot, avg;

public:

void display()

{

tot=(m1+m2+sm);

avg=tot/3;

cout<<"\n\n\tRoll No : "<<rno<<"\n\tTotal : "<<tot;

cout<<"\n\tAverage : "<<avg;

}

};

void main()

{

clrscr();

statement obj;

obj.get();

obj.getsm();

obj.display();

getch();

}

/* Output:

Enter the Roll no: 100

Enter two marks

90

80

Enter the Sports Mark: 90

Roll No: 100

Total : 260

Average: 86.66 */

Multilevel Inheritance :-

Definition:

“It is the inheritance hierarchy wherein subclass acts as a base class for other classes”

It is implemented by defining at least three classes.

In multilevel inheritance, there is one base class and the remaining two is derived class.

Declaration :-

class A

{………..

……….}; // Base class

class B : public A

{………..

……….}; // B derived from A

class C : public B

{……….

………}; // C derived from B

Fig. Multilevel Inheritance :-

Student

Test Records

Result

Base Class

Intermediate base class

Derived class

Hybrid inheritance :-

Definition:“Inheritance hierarchy that reflects any legal combination of

other four types of inheritance.”

Simple word:-

“The method of combining any two or more forms of inheritance in single form is called hybrid inheritance.”

Fig. Hybrid Inheritance :-

Student

Test Records

Result

Sports

Base class

Intermediate base class

Derived class

Base class

class A

{……….

………};

class B:public A

{……….

……….};

class C

{……….

………};

class D: public b, public c

{……….

……….};

Hierarchical Inheritance :-

• Hierarchical Inheritance is a method of inheritance where one or more derived classes is derived from common base class.

Constructors in Inheritance :-

• Base class constructors are automatically called for you if they have no argument.

• If you want to call a superclass constructor with an argument, you must use the subclass's constructor initialization list.

• C++ supports multiple inheritance (for better or worse), so the base class must be referred to by name, rather than "super()".

Constructor Example :-class SuperClass

{

public:

SuperClass(int foo)

{ // do something with foo }

};

class SubClass : public SuperClass

{

public: SubClass(int foo, int bar) : SuperClass(foo)

{ // do something with bar }

};

Virtual Function :-

Virtual keyword determines if a member function of a class can be over-ridden in its derived classes. 

The non-virtual member functions are resolved at compiling time and it’s called static binding. However, the c++ virtual member functions are resolved during runtime and it’s called as dynamic binding

Virtual Class :-

• Suppose you have two derived classes B and C that have a common base class A, and you also have another class Dthat inherits from B and C.

• You can declare the base class A as virtual to ensure that B and C share the same subobject of A

Advantages :-

Sometimes we need to repeat code or we need repeat the whole class properties. So it helps in various ways.

1.) It saves memory space.

2.) It saves time.

3.) It will remove frustration.

4.) It Increases reliability of the code.

5.) It saves the developing and testing efforts.

For your Attention !!!!

top related