SlideShare a Scribd company logo
OOP Inheritance (Backbone of the OOP) Instructor Mr.Fasee Ullah CUSIT Peshawar
C++ and inheritance The language mechanism by which one class acquires the properties (data and operations) of another class   Base Class (or superclass ):  It’s the main class, where data members and function members can inherit Derived Class (or subclass ): the class that inherits data members and function members from base class
What a derived class doesn't inherit The base class's constructors and destructor
What a derived class can add New data members New member functions  (also overwrite existing ones) New constructors and destructor
When a  derived-class object  is created & destroyed Space is allocated  (on the stack or the heap) for the full object (that is, enough space to store the data members inherited from the base class plus the data members defined in the derived class itself) The  base class's constructor is called  to initialize the data members inherited from the base class The  derived class's constructor is then called  to initialize the data members added in the derived class The  derived-class object is then usable When the object is destroyed (goes out of scope or is deleted) the  derived class's destructor is called  on the object first Then the  base class's destructor is called  on the object Finally the  allocated space for the full object is reclaimed
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
Access Rights of Derived Classes Type of Inheritance
Inheritance For instance, graphics objects might be defined as follows:   This hierarchy could, of course, be continued for more levels. Each level inherits the attributes of the above level.  Shape is the base class.  2-D and 3-D are derived from Shape and Circle, Square, and Triangle are derived from 2-D.  Similarly, Sphere, Cube, and Tetrahedron are derived from 3-D.
Inheritance Concept Rectangle Triangle Polygon Point Circle 3D-Point
Parent and Child Classes   Inheritance Concept Rectangle Triangle Polygon class Polygon { private:   int width, length; public:   void set(int w, int l); } class Rectangle{ private:   int width, length; public:   void set(int w, int l);   int area(); } class Triangle{ private:   int width, length; public:   void set(int w, int l);   int area(); }
Inheritance Concept Rectangle Triangle Polygon class Polygon { protected:   int width, length; public:   void set(int w, int l); } class Rectangle : public Polygon { public: int area(); } class Rectangle{ protected:   int width, length; public:   void set(int w, int l);   int area(); }
Inheritance Concept Rectangle Triangle Polygon class Polygon { protected:   int width, length; public:   void set(int w, int l); } class Triangle : public Polygon { public: int area(); } class Triangle : public polygon{ protected:   int width, length; public:   void set(int w, int l);   int area(); }
Inheritance Concept Point Circle 3D-Point class Point { protected:   int x, y; public:   void set(int a, int b); } class Circle : public Point { private:  double r; } class 3D-Point: public Point { private:  int z; } x y x y r x y z
Example1 on Inheritance #include<iostream.h> #include<conio.h> Class test { Private:  int x; Public:  int y; Protected:  int  z; }; Class test1: public test{ Public:  int a; a=y; }; Main(){ Test  t1; t1.y; t1. z;  //error }
Example2 on Inheritance class X { public: void message(){ Cout<<“Welcome to Inheritance”; } }; class Y: public X { }; Void main() { X x; Y y; x.message(); y.message();  //for verification, call Y }
Example3 on Inheritance //inheritance program... #include<iostream.h> #include<conio.h> class point { int x,y; public: void getxy(int a,int b) { x=a; y=b; } }; class circle: public point{ int radius; public: void getradius(int r) { radius=r; } void area(){ int area; area=3.142*radius*radius; cout<<&quot;Area of circle is &quot;<<area; } }; void main(){ circle c; c.getxy(3,5); c.getradius(5); c.area(); getch(); }
Assignment #2 What are the practical issues in Inheritance.? Submission: Next Week

More Related Content

PPT
Inheritance OOP Concept in C++.
PPTX
inheritance c++
PPT
Inheritance
PPTX
Inheritance
PPT
inhertance c++
PPSX
PPTX
Inheritance in c++
PPTX
Inheritance in c++theory
Inheritance OOP Concept in C++.
inheritance c++
Inheritance
Inheritance
inhertance c++
Inheritance in c++
Inheritance in c++theory

What's hot (20)

PPTX
EASY TO LEARN INHERITANCE IN C++
PPTX
inheritance in C++
PPTX
#OOP_D_ITS - 6th - C++ Oop Inheritance
PPT
Inheritance
PPTX
Inheritance in C++
PPT
Inheritance
PPTX
Inheritance in c++
PPTX
Inheritance in c++
PPTX
Single inheritance
PPTX
Inheritance
PDF
OOP Inheritance
PPTX
Multiple inheritance in c++
PPT
Inheritance, Object Oriented Programming
PPT
C++ Inheritance
PPTX
inheritance
PPS
Inheritance
PDF
Inheritance
PPTX
Inheritance In C++ (Object Oriented Programming)
PPTX
Introduction to Inheritance
EASY TO LEARN INHERITANCE IN C++
inheritance in C++
#OOP_D_ITS - 6th - C++ Oop Inheritance
Inheritance
Inheritance in C++
Inheritance
Inheritance in c++
Inheritance in c++
Single inheritance
Inheritance
OOP Inheritance
Multiple inheritance in c++
Inheritance, Object Oriented Programming
C++ Inheritance
inheritance
Inheritance
Inheritance
Inheritance In C++ (Object Oriented Programming)
Introduction to Inheritance
Ad

Similar to Inheritance (20)

PPT
Lecture4
PPT
Lecture4
PPT
C plus plus Inheritance a complete guide
PPT
Inheritance, polymorphisam, abstract classes and composition)
PPT
Lecture4
PPTX
Introduction to inheritance and different types of inheritance
PPTX
Inheritance
PPTX
OOPS Basics With Example
PPT
PPT
Lecturespecial
PPT
PPTX
Opp concept in c++
PPTX
Inheritance
PDF
Object Oriented Programming (OOP) using C++ - Lecture 3
PPTX
inheriTANCE IN OBJECT ORIENTED PROGRAM.pptx
PDF
Chapter25 inheritance-i
PPTX
Week 8 - OOP Inheritance11111111111.pptx
PPT
Inheritance in C++
PDF
C++ L10-Inheritance
Lecture4
Lecture4
C plus plus Inheritance a complete guide
Inheritance, polymorphisam, abstract classes and composition)
Lecture4
Introduction to inheritance and different types of inheritance
Inheritance
OOPS Basics With Example
Lecturespecial
Opp concept in c++
Inheritance
Object Oriented Programming (OOP) using C++ - Lecture 3
inheriTANCE IN OBJECT ORIENTED PROGRAM.pptx
Chapter25 inheritance-i
Week 8 - OOP Inheritance11111111111.pptx
Inheritance in C++
C++ L10-Inheritance
Ad

Inheritance

  • 1. OOP Inheritance (Backbone of the OOP) Instructor Mr.Fasee Ullah CUSIT Peshawar
  • 2. C++ and inheritance The language mechanism by which one class acquires the properties (data and operations) of another class Base Class (or superclass ): It’s the main class, where data members and function members can inherit Derived Class (or subclass ): the class that inherits data members and function members from base class
  • 3. What a derived class doesn't inherit The base class's constructors and destructor
  • 4. What a derived class can add New data members New member functions (also overwrite existing ones) New constructors and destructor
  • 5. When a derived-class object is created & destroyed Space is allocated (on the stack or the heap) for the full object (that is, enough space to store the data members inherited from the base class plus the data members defined in the derived class itself) The base class's constructor is called to initialize the data members inherited from the base class The derived class's constructor is then called to initialize the data members added in the derived class The derived-class object is then usable When the object is destroyed (goes out of scope or is deleted) the derived class's destructor is called on the object first Then the base class's destructor is called on the object Finally the allocated space for the full object is reclaimed
  • 6. 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
  • 7. Access Rights of Derived Classes Type of Inheritance
  • 8. Inheritance For instance, graphics objects might be defined as follows: This hierarchy could, of course, be continued for more levels. Each level inherits the attributes of the above level. Shape is the base class. 2-D and 3-D are derived from Shape and Circle, Square, and Triangle are derived from 2-D. Similarly, Sphere, Cube, and Tetrahedron are derived from 3-D.
  • 9. Inheritance Concept Rectangle Triangle Polygon Point Circle 3D-Point
  • 10. Parent and Child Classes Inheritance Concept Rectangle Triangle Polygon class Polygon { private: int width, length; public: void set(int w, int l); } class Rectangle{ private: int width, length; public: void set(int w, int l); int area(); } class Triangle{ private: int width, length; public: void set(int w, int l); int area(); }
  • 11. Inheritance Concept Rectangle Triangle Polygon class Polygon { protected: int width, length; public: void set(int w, int l); } class Rectangle : public Polygon { public: int area(); } class Rectangle{ protected: int width, length; public: void set(int w, int l); int area(); }
  • 12. Inheritance Concept Rectangle Triangle Polygon class Polygon { protected: int width, length; public: void set(int w, int l); } class Triangle : public Polygon { public: int area(); } class Triangle : public polygon{ protected: int width, length; public: void set(int w, int l); int area(); }
  • 13. Inheritance Concept Point Circle 3D-Point class Point { protected: int x, y; public: void set(int a, int b); } class Circle : public Point { private: double r; } class 3D-Point: public Point { private: int z; } x y x y r x y z
  • 14. Example1 on Inheritance #include<iostream.h> #include<conio.h> Class test { Private: int x; Public: int y; Protected: int z; }; Class test1: public test{ Public: int a; a=y; }; Main(){ Test t1; t1.y; t1. z; //error }
  • 15. Example2 on Inheritance class X { public: void message(){ Cout<<“Welcome to Inheritance”; } }; class Y: public X { }; Void main() { X x; Y y; x.message(); y.message(); //for verification, call Y }
  • 16. Example3 on Inheritance //inheritance program... #include<iostream.h> #include<conio.h> class point { int x,y; public: void getxy(int a,int b) { x=a; y=b; } }; class circle: public point{ int radius; public: void getradius(int r) { radius=r; } void area(){ int area; area=3.142*radius*radius; cout<<&quot;Area of circle is &quot;<<area; } }; void main(){ circle c; c.getxy(3,5); c.getradius(5); c.area(); getch(); }
  • 17. Assignment #2 What are the practical issues in Inheritance.? Submission: Next Week