Basics of Inheritance Inheritance allows us to define
Basics 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.
Inheritance “the mechanism by which one class acquires the properties of another class”
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 Road vehicle Car 2 -door Bicycle 4 -door Boat
C++ and inheritance • The language mechanism by which one • • class acquires the properties (data and operations) of another class Base Class (or superclass): the class being inherited from Derived Class (or subclass): the class that inherits
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
Inheritance Basic Syntax • General form of Inheritance: class derived _class_name : visibility modifier base_class_name { Members of derived 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.
#include <iostream> using namespace std; // Base class Shape { public: void set. Width(int w) { width = w; } void set. Height(int h) { height = h; } protected: int width; int height; }; // Derived class Rectangle: public Shape { public: int get. Area() { return (width * height); } }; int main(void) { Rectangle Rect; Rect. set. Width(5); Rect. set. Height(7); // Print the area of the object.
What is protected ?
Effect of inheritance on the visibility of members
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
Types of Inheritance • There are different types of inheritance: • Single Inheritance • Multiple Inheritance • Multilevel Inheritance • Hierarchical Inheritance • Hybrid (Virtual) Inheritance
Single Inheritance • Single inheritance represents a form of inheritance when there is only one base class and one derived class. For example, a class describes a Person:
Multiple Inheritance • Multiple inheritance represents a kind of inheritance when a derived class inherits properties of multiple classes. For example, there are three classes A, B and C and derived class is D as shown below:
#include <iostream> using namespace std; // Base class Shape { public: void set. Width(int w) { width = w; } void set. Height(int h) { height = h; } protected: int width; int height; }; / / Base class Paint. Cost { public: int get. Cost(int area) { return area * 70; } }; // Derived class Rectangle: public Shape, public Paint. Cost { public: int get. Area() { return (width * height); } }; int main(void) { Rectangle Rect; int area; Rect. set. Width(5); Rect. set. Height(7); cout << "Total area: " << Rect. get. Area() << endl; // Print the total cost of painting cout << "Total paint cost: $" << Rect. get. Cost(area) << endl; return 0; }
Ambiguity in Multiple Inheritance class A {public: void print(){cout<<“n Hello I am Class A”; } }; class B {public: void print(){cout<<“n Hello I am Class B”; } }; Class C : public A, public B { public: void print(){cout<<“n Hello I am Class C”; } }; Int main(){C obj; obj. print(); } // which print function will be called
Ambiguity Resolution in Multiple Inheritance class A {public: void print(){cout<<“n Hello I am Class A”; } }; class B {public: void print(){cout<<“n Hello I am Class B”; } }; Class C : public A, public B { public: void print(){cout<<“n Hello I am Class C”; } }; Int main() {C obj; // derived class object obj. print(); //call print of C Obj. A: : print(); // call print of A Obj. B: : print(); // call print of B }
Multilevel Inheritance • Multilevel inheritance represents a type of inheritance when a Derived class is a base class for another class. In other words, deriving a class from a derived class is known as multi-level inheritance. Simple multi-level inheritance is shown in below image where Class A is a parent of Class B and Class B is a parent of Class C
Virtual Base class/ multipath inheritance • What is a virtual base class? • - An ambiguity can arise when several paths exist to a class from the same base class. This means that a child class could have duplicate sets of members inherited from a single base class. - C++ solves this issue by introducing a virtual base class. When a class is made virtual, necessary care is taken so that the duplication is avoided regardless of the number of paths that exist to the child class.
#include<iostream. h> class Class. A { public: int a; }; class Class. B : public Class. A { public: int b; }; class Class. C : public Class. A { public: int c; }; class Class. D : public Class. B, public Class. C { public: int d; }; void main() { Class. D obj; obj. a = 10; //Statement 1, Error occur obj. a = 100; //Statement 2, Error occur obj. b = 20; obj. c = 30; obj. d = 40; cout<< "n A : "<< obj. a; cout<< "n B : "<< obj. b; cout<< "n C : "<< obj. c; cout<< "n D : "<< obj. d; } #include<iostream. h> class Class. A { public: int a; }; class Class. B : public virtual Class. A { public: int b; }; class Class. C : virtual public Class. A { public: int c; }; class Class. D : public Class. B, public Class. C { public: int d; }; void main() { Class. D obj; obj. a = 10; //Statement 1, Error occur obj. a = 100; //Statement 2, Error occur obj. b = 20; obj. c = 30; obj. d = 40; Output cout<< "n A : "<< obj. a; A: 100 cout<< "n B : "<< obj. b; B: 20 cout<< "n C : "<< obj. c; C: 30 cout<< "n D : "<< obj. d; D: 40 }
• • Constructor in Derived Class As long as no base class constructor takes any arguments, the derived class need not have any constructor. if any base class contains constructor one or more arguments are used then it is mandatory for the derived class to have a constructor and pass the arguments to base class constructors. While applying inheritance, we usually create objects using derived class. Then it makes sense for the derived class to pass arguments to the base class constructor. When both the base and derived class contain constructors , the base class constructor is execute first. In case of multiple inheritance, the base classes are constructed , in the order in which they appear in in the declaration of the derived class. Since the derived class takes the responsibility to supply initial values to the base class, we supply the initial values that are required by all the classes together where the derived class object is declared. The constructor of the derived class receives the entire list of values of arguments and pass them on to the base constructors int the order in which they are declared in the derived class
Base class Default Constructor in Derived class Constructors
Base class Parameterized Constructor in Derived class Constructor
- Slides: 26