37
Programming Language :- C++ Presentation :-

Inheritance in C++

Embed Size (px)

DESCRIPTION

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

Citation preview

Page 1: Inheritance in C++

Programming Language :-

C++ Presentation :-

Page 2: Inheritance in C++

Topic

INHERITANCE

Presentation Group Members :-

08. Ankita

14. Ashutosh

18. Samiksha

19. Laxman

S.Y.BSc.(Computer Science)

Page 3: Inheritance in C++

ROAD MAP ......

I

Types of Inheritance :-

Demo !!!!

Doubt & Question Session ????

Page 4: Inheritance in C++

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.

Page 5: Inheritance in C++

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.

Page 6: Inheritance in C++

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.

Page 7: Inheritance in C++
Page 8: Inheritance in C++

-: TYPES OF INHERITANCE :-

Single Inheritance

Multilevel Inheritance

Multiple Inheritance

Hierarchal Inheritance

Hybrid Inheritance

Page 9: Inheritance in C++

SINGLE INHERITANCE

Page 10: Inheritance in C++

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.

Page 11: Inheritance in C++

Syntax :class base_class_name

{

access specifier:

:

};

Class derived_class_name : access specifier base_class_name

{

access specifier:

:

};

Page 12: Inheritance in C++

class A //Base Class

{

public:

int a, b;

void getdata ();

};

class B : public A

//Derived Class

{

int c;

public:

void calculate ();

void display ();

};

Example :-

Page 13: Inheritance in C++

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;

}

Page 14: Inheritance in 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 ();

Page 15: Inheritance in C++

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.

Page 16: Inheritance in C++

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

Page 17: Inheritance in C++

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

Page 18: Inheritance in C++

Multiple Inheritance :-

Page 19: Inheritance in C++

MULTIPLE INHERITANCE :-

Def :-

When a subclass inherits from multiple base classes ,

it is known as multiple inheritance.

Page 20: Inheritance in C++

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

<access_specifier> classn_name

{

:

: //members of derived class

:

}

SYNTAX:

Page 21: Inheritance in C++

class sub : public superA, private superB

{

:

: //members of class sub

:

}

FOR INSTANCE:

Page 22: Inheritance in C++

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;

}

};

Page 23: Inheritance in C++

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

}

Page 24: Inheritance in C++

/* Output:

Enter the Roll no: 100

Enter two marks

90

80

Enter the Sports Mark: 90

Roll No: 100

Total : 260

Average: 86.66 */

Page 25: Inheritance in C++

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.

Page 26: Inheritance in C++

Declaration :-

class A

{………..

……….}; // Base class

class B : public A

{………..

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

class C : public B

{……….

………}; // C derived from B

Page 27: Inheritance in C++

Fig. Multilevel Inheritance :-

Student

Test Records

Result

Base Class

Intermediate base class

Derived class

Page 28: Inheritance in C++

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.”

Page 29: Inheritance in C++

Fig. Hybrid Inheritance :-

Student

Test Records

Result

Sports

Base class

Intermediate base class

Derived class

Base class

Page 30: Inheritance in C++

class A

{……….

………};

class B:public A

{……….

……….};

class C

{……….

………};

class D: public b, public c

{……….

……….};

Page 31: Inheritance in C++

Hierarchical Inheritance :-

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

Page 32: Inheritance in C++

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()".

Page 33: Inheritance in C++

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 }

};

Page 34: Inheritance in C++

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

Page 35: Inheritance in C++

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

Page 36: Inheritance in C++

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.

Page 37: Inheritance in C++

For your Attention !!!!