OOP Inheritance (Backbone of the OOP) Instructor Mr.Fasee Ullah CUSIT Peshawar
C++ and inheritance <ul><li>The language mechanism by which one class acquires the properties (data and operations) of ano...
What a derived class doesn't inherit <ul><li>The base class's constructors and destructor </li></ul>
What a derived class can add <ul><li>New data members </li></ul><ul><li>New member functions  (also overwrite existing one...
When a  derived-class object  is created & destroyed <ul><li>Space is allocated  (on the stack or the heap) for the full o...
Advantages of inheritance <ul><li>When a class inherits from another class, there are three benefits: </li></ul><ul><li>(1...
Access Rights of Derived Classes Type of Inheritance
Inheritance <ul><li>For instance, graphics objects might be defined as follows:   </li></ul>This hierarchy could, of cours...
Inheritance Concept Rectangle Triangle Polygon Point Circle 3D-Point
Parent and Child Classes   Inheritance Concept Rectangle Triangle Polygon class Polygon { private:   int width, length; pu...
Inheritance Concept Rectangle Triangle Polygon class Polygon { protected:   int width, length; public:   void set(int w, i...
Inheritance Concept Rectangle Triangle Polygon class Polygon { protected:   int width, length; public:   void set(int w, i...
Inheritance Concept Point Circle 3D-Point class Point { protected:   int x, y; public:   void set(int a, int b); } class C...
Example1 on Inheritance <ul><li>#include<iostream.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>Class test { </...
Example2 on Inheritance <ul><li>class X { </li></ul><ul><li>public: </li></ul><ul><li>void message(){ </li></ul><ul><li>Co...
Example3 on Inheritance <ul><li>//inheritance program... </li></ul><ul><li>#include<iostream.h> </li></ul><ul><li>#include...
Assignment #2 <ul><li>What are the practical issues in Inheritance.? </li></ul><ul><li>Submission: Next Week </li></ul>
Upcoming SlideShare
Loading in …5
×

Inheritance

2,109 views

Published on

For BCS level student the concept of inheritance By Fasu

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
2,109
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
144
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Inheritance

  1. 1. OOP Inheritance (Backbone of the OOP) Instructor Mr.Fasee Ullah CUSIT Peshawar
  2. 2. C++ and inheritance <ul><li>The language mechanism by which one class acquires the properties (data and operations) of another class </li></ul><ul><li>Base Class (or superclass ): </li></ul><ul><li>It’s the main class, where data members and function members can inherit </li></ul><ul><li>Derived Class (or subclass ): the class that inherits data members and function members from base class </li></ul>
  3. 3. What a derived class doesn't inherit <ul><li>The base class's constructors and destructor </li></ul>
  4. 4. What a derived class can add <ul><li>New data members </li></ul><ul><li>New member functions (also overwrite existing ones) </li></ul><ul><li>New constructors and destructor </li></ul>
  5. 5. When a derived-class object is created & destroyed <ul><li>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) </li></ul><ul><li>The base class's constructor is called to initialize the data members inherited from the base class </li></ul><ul><li>The derived class's constructor is then called to initialize the data members added in the derived class </li></ul><ul><li>The derived-class object is then usable </li></ul><ul><li>When the object is destroyed (goes out of scope or is deleted) the derived class's destructor is called on the object first </li></ul><ul><li>Then the base class's destructor is called on the object </li></ul><ul><li>Finally the allocated space for the full object is reclaimed </li></ul>
  6. 6. Advantages of inheritance <ul><li>When a class inherits from another class, there are three benefits: </li></ul><ul><li>(1) You can reuse the methods and data of the existing class </li></ul><ul><li>(2) You can extend the existing class by adding new data and new methods </li></ul><ul><li>(3) You can modify the existing class by overloading its methods with your own implementations </li></ul>
  7. 7. Access Rights of Derived Classes Type of Inheritance
  8. 8. Inheritance <ul><li>For instance, graphics objects might be defined as follows: </li></ul>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. 9. Inheritance Concept Rectangle Triangle Polygon Point Circle 3D-Point
  10. 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. 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. 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. 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. 14. Example1 on Inheritance <ul><li>#include<iostream.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>Class test { </li></ul><ul><li>Private: int x; </li></ul><ul><li>Public: int y; </li></ul><ul><li>Protected: int z; </li></ul><ul><li>}; </li></ul><ul><li>Class test1: public test{ </li></ul><ul><li>Public: int a; </li></ul><ul><li>a=y; </li></ul><ul><li>}; </li></ul><ul><li>Main(){ </li></ul><ul><li>Test t1; </li></ul><ul><li>t1.y; </li></ul><ul><li>t1. z; </li></ul><ul><li>//error </li></ul><ul><li>} </li></ul>
  15. 15. Example2 on Inheritance <ul><li>class X { </li></ul><ul><li>public: </li></ul><ul><li>void message(){ </li></ul><ul><li>Cout<<“Welcome to Inheritance”; </li></ul><ul><li>} </li></ul><ul><li>}; </li></ul><ul><li>class Y: public X { </li></ul><ul><li>}; </li></ul><ul><li>Void main() { </li></ul><ul><li>X x; </li></ul><ul><li>Y y; </li></ul><ul><li>x.message(); </li></ul><ul><li>y.message(); //for verification, call Y </li></ul><ul><li>} </li></ul>
  16. 16. Example3 on Inheritance <ul><li>//inheritance program... </li></ul><ul><li>#include<iostream.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>class point { </li></ul><ul><li>int x,y; </li></ul><ul><li>public: </li></ul><ul><li>void getxy(int a,int b) </li></ul><ul><li>{ </li></ul><ul><li>x=a; </li></ul><ul><li>y=b; </li></ul><ul><li>} </li></ul><ul><li>}; </li></ul><ul><li>class circle: public point{ </li></ul><ul><li>int radius; </li></ul><ul><li>public: </li></ul>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. 17. Assignment #2 <ul><li>What are the practical issues in Inheritance.? </li></ul><ul><li>Submission: Next Week </li></ul>

×