Download presentation
Presentation is loading. Please wait.
1
C++ Inheritance
2
Arrange concepts into an inheritance hierarchy
Concepts at higher levels are more general Concepts at lower levels are more specific (inherit properties of concepts at higher levels) Vehicle Wheeled vehicle Boat Car Bicycle 2-door 4-door
3
C++ Inheritance One of the most important concepts in object-oriented programming is that of inheritance. Inheritance allows us to define a class in terms of another class, which makes it easier to create and maintain an application. This also provides an opportunity to reuse the code functionality and fast implementation time. When creating a class, instead of writing completely new data members and member functions, the programmer can designate that the new class should inherit the members of an existing class. This existing class is called the base class, and the new class is referred to as the derived class.
4
C++ Inheritance Inheritance is the process by which new classes called derived classes are created from existing classes called base classes. The derived classes have all the features of the base class and the programmer can choose to add new features specific to the newly created derived class.
5
The "is a" Relationship Inheritance establishes an "is a" relationship between classes. A poodle is a dog A car is a vehicle A flower is a plant A football player is an athlete
6
C++ Inheritance Features or Advantages of Inheritance: Reusability:
Inheritance helps the code to be reused in many situations. The base class is defined and once it is compiled, it need not be reworked. Using the concept of inheritance, the programmer can create as many derived classes from the base class as needed while adding specific features to each derived class as needed.
7
C++ Inheritance Features or Advantages of Inheritance:
Saves Time and Effort: The above concept of reusability achieved by inheritance saves the programmer time and effort. The main code written can be reused in various situations as needed. Increases Program Structure which results in greater reliability.
8
Advantages of inheritance
When a class inherits from another class, there are three benefits: (1) You can reuse the methods and data of the existing class (2) You can extend the existing class by adding new data and new methods (3) You can modify the existing class by overloading its methods with your own implementations
9
Rules for building a class hierarchy
Derived classes are special cases of base classes A derived class can also serve as a base class for new classes. There is no limit on the depth of inheritance allowed in C++ (as far as it is within the limits of your compiler) It is possible for a class to be a base class for more than one derived class
10
C++ Inheritance General Format for implementing the concept of Inheritance: class derived_classname: access specifier baseclassname For example, if the base class is MyClass and the derived class is sample it is specified as: class sample: public MyClass The above makes sample have access to both public and protected variables of base class MyClass
11
Inheritance – Terminology and Notation in C++
Base class (or parent) – inherited from Derived class (or child) – inherits from the base class Notation: class Student // base class { . . . }; class UnderGrad : public student { // derived class
12
Back to the ‘is a’ Relationship
An object of a derived class 'is a(n)' object of the base class Example: an UnderGrad is a Student a Mammal is an Animal A derived object has all of the characteristics of the base class
13
Inheritance and accessibility
A class inherits the behavior of another class and enhances it in some way Inheritance does not mean inheriting access to another class’ private members
14
Reminder about public, private and protected access specifiers:
C++ Inheritance Reminder about public, private and protected access specifiers: 1 If a member or variables defined in a class is private, then they are accessible by members of the same class only and cannot be accessed from outside the class. 2 Public members and variables are accessible from outside the class. 3 Protected access specifier is a stage between private and public. If a member functions or variables defined in a class are protected, then they cannot be accessed from outside the class but can be accessed from the derived class.
15
Access Specifiers When deriving a class from a base class, the base class may be inherited through public, protected or private inheritance. The type of inheritance is specified by the access- specifier. We hardly use protected or private inheritance, but public inheritance is commonly used. While using different type of inheritance, following rules are applied:
16
Access Specifiers Public Inheritance: When deriving a class from a public base class, public members of the base class become public members of the derived class and protected members of the base class become protected members of the derived class. A base class's private members are never accessible directly from a derived class, but can be accessed through calls to the public and protected members of the base class.
17
Access Specifiers Protected Inheritance: When deriving from a protected base class, public and protected members of the base class become protected members of the derived class. Private Inheritance: When deriving from private base class, public and protected members of the base class become private members of the derived class
18
Inheritance vs. Access private: x protected: y public: z
Base class members x is inaccessible private: y private: z protected: z How inherited base class members appear in derived class private base class protected public
19
class Test : public Grade
Inheritance vs. Access private members: char letter; float score; void calcGrade(); public members: void setScore(float); float getScore(); char getLetter(); class Grade int numQuestions; float pointsEach; int numMissed; Test(int, int); class Test : public Grade When Test class inherits from Grade class using public class access, it looks like this: int numQuestions:
20
class Test : protected Grade
Inheritance vs. Access private members: char letter; float score; void calcGrade(); public members: void setScore(float); float getScore(); char getLetter(); class Grade int numQuestions; float pointsEach; int numMissed; Test(int, int); When Test class inherits from Grade class using protected class access, it looks like this: int numQuestions: protected members: float getLetter(); class Test : protected Grade
21
class Test : private Grade
Inheritance vs. Access private members: int numQuestions: float pointsEach; int numMissed; void setScore(float); float getScore(); float getLetter(); public members: Test(int, int); char letter; float score; void calcGrade(); char getLetter(); class Grade int numQuestions; When Test class inherits from Grade class using private class access, it looks like this: class Test : private Grade
22
Constructors and Destructors in Base and Derived Classes
Derived classes can have their own constructors and destructors When an object of a derived class is created, the base class’s constructor is executed first, followed by the derived class’s constructor When an object of a derived class is destroyed, its destructor is called first, then that of the base class
23
C++ Inheritance Inheritance Example: class MyClass { private: int x;
public: MyClass(void) { x=0; } void f(int n1) { x= n1*5;} void output(void) { cout<<x; } };
24
C++ Inheritance Inheritance Example: class sample: public MyClass {
private: int s1; public: sample(void) { s1=0; } void f1(int n1) { s1=n1*10;} void output(void) { MyClass::output(); cout << s1; } };
25
C++ Inheritance Inheritance Example:
int main(void) { sample s; s.f(10); s.output(); s.f1(20); s.output(); } The output of the above program is 50 200
26
It is a Base class (super) it is a sub class (derived)
Types of Inheritance 1. Single class Inheritance: Single inheritance is the one where you have a single base class and a single derived class. Class Employee Class Manager It is a Base class (super) it is a sub class (derived)
27
Types of Inheritance 2. Multilevel Inheritance:
In Multi level inheritance, a class inherits its properties from another derived class. Class Grandfather Class Father it is a Base class (super) of Class Father it is a sub class (derived) of Class Grandfather and base class of class Class Child Class Child derived class(sub) of class Father
28
Types of Inheritance 3. Multiple Inheritances:
In Multiple inheritances, a derived class inherits from multiple base classes. It has properties of both the base classes. Class A Class B Base class Class C Derived class
29
Types of Inheritance 4. Hierarchical Inheritance:
In hierarchical Inheritance, it's like an inverted tree. So multiple classes inherit from a single base class. It's quite analogous to the File system in a unix based system. Class A Class B Class C Class D
30
Types of Inheritance 5. Hybrid Inheritance:
In this type of inheritance, we can have mixture of number of inheritances but this can generate an error of using same name function from no of classes, which will bother the compiler to how to use the functions. Therefore, it will generate errors in the program. This has known as ambiguity or duplicity. Ambiguity problem can be solved by using virtual base classes
31
Types of Inheritance 5. Hybrid Inheritance: Class A Class B Class C
Class D Class C
32
? Any Questions Please
33
Board Question Write a program in C++ to implement the following class hierarchy: Class student to obtain Roll Number, Class Test to obtain marks scored in two different subjects, Class Sports to obtain weight-age (marks) in sports and Class Result to calculate the total marks. The program must print the roll number, individual marks obtained in two subjects, sports and total marks. (15) Enter the program and verify proper execution of the same on the Computer. (10) Obtain a hard-copy of the program listing as well as output. (05)
34
Understanding Inheritance in Question
Class Student Class Sports Class Test Class Result
35
#include<iostream. h> #include<conio
#include<iostream.h> #include<conio.h> class student { protected: int roll_no; public: void get_no() cout<<"Enter roll no : "; cin>>roll_no; } void put_no(void) cout<<"Roll Number : "<<roll_no; };
36
class test :public student { protected: float m1,m2; public: void get_marks() cout<<"Enter marks of subject 1 : "; cin>>m1; cout<<"Enter marks of subject 2 : "; cin>>m2; } void put_marks(void) cout<<"\nMarks obtained : Subject 1="<<m1<<" and Subject 2="<<m2; };
37
class sports { protected: float score; public: void get_score() cout<<"Enter marks of sports : "; cin>>score; } void put_score(void) cout<<"\nSports marks : "<<score; };
38
class result : public test, public sports { protected: float total; public: void display(void) cout<<"\n\nStudents details are as follows : \n"; total=m1+m2+score; put_no(); put_marks(); put_score(); cout<<"\nTotal score : "<<total; } };
39
void main() { clrscr(); result r; r. get_no(); r. get_marks(); r
void main() { clrscr(); result r; r.get_no(); r.get_marks(); r.get_score(); r.display(); getch(); }
40
Sample Output
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.