Presentation is loading. Please wait.

Presentation is loading. Please wait.

Inheritance Inheritance-2. Inheritance Rewriting point and circle classes class Point { protected: int x,y; public: Point(int,int); void display(void);

Similar presentations


Presentation on theme: "Inheritance Inheritance-2. Inheritance Rewriting point and circle classes class Point { protected: int x,y; public: Point(int,int); void display(void);"— Presentation transcript:

1 Inheritance Inheritance-2

2 Inheritance Rewriting point and circle classes class Point { protected: int x,y; public: Point(int,int); void display(void); }; Point::Point(int a,int b) { x=a; y=b; } void Point::display(void) { cout<<"point = [" < { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/13/3620304/slides/slide_2.jpg", "name": "Inheritance Rewriting point and circle classes class Point { protected: int x,y; public: Point(int,int); void display(void); }; Point::Point(int a,int b) { x=a; y=b; } void Point::display(void) { cout<< point = [ <

3 Inheritance class Circle : public Point { double radius; public: Circle(int,int,double ); void display(void); }; Circle::Circle(int a,int b,double c):Point(a,b) { radius = c; } void Circle::display(void) { Point::display(); cout<<" and radius = "< { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/13/3620304/slides/slide_3.jpg", "name": "Inheritance class Circle : public Point { double radius; public: Circle(int,int,double ); void display(void); }; Circle::Circle(int a,int b,double c):Point(a,b) { radius = c; } void Circle::display(void) { Point::display(); cout<< and radius = <

4 Inheritance void main(void) { Circle c(3,4,5); c.display(); }

5 Inheritance Three-Level Inheritance Hierarchy  Three level point/circle/cylinder hierarchy  Point x-y coordinate pair  Circle x-y coordinate pair Radius  Cylinder x-y coordinate pair Radius Height

6 Inheritance class Point { protected: int x,y; public: Point(int,int ); void display(void); }; Point::Point(int a,int b) { x=a; y=b; } void Point::display(void) { cout<<"point = [" < { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/13/3620304/slides/slide_6.jpg", "name": "Inheritance class Point { protected: int x,y; public: Point(int,int ); void display(void); }; Point::Point(int a,int b) { x=a; y=b; } void Point::display(void) { cout<< point = [ <

7 Inheritance class Circle : public Point { protected: double radius; public: Circle(int, int, double); void display(void); double GetArea(void); }; Circle::Circle(int a,int b,double c):Point(a,b) { radius = c; } void Circle::display(void) { Point::display(); cout<<" radius = "< { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/13/3620304/slides/slide_7.jpg", "name": "Inheritance class Circle : public Point { protected: double radius; public: Circle(int, int, double); void display(void); double GetArea(void); }; Circle::Circle(int a,int b,double c):Point(a,b) { radius = c; } void Circle::display(void) { Point::display(); cout<< radius = <

8 Inheritance class Cylinder:public Circle { double height; public: Cylinder(int,int,double,double); void display(void); double GetVolume(void); }; Cylinder::Cylinder(int a,int b,double r,double h):Circle(a,b,r) { height=h; } double Cylinder::GetVolume(void) { return GetArea() * height; } void Cylinder::display(void) { Circle::display(); cout<<" height = "< { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/13/3620304/slides/slide_8.jpg", "name": "Inheritance class Cylinder:public Circle { double height; public: Cylinder(int,int,double,double); void display(void); double GetVolume(void); }; Cylinder::Cylinder(int a,int b,double r,double h):Circle(a,b,r) { height=h; } double Cylinder::GetVolume(void) { return GetArea() * height; } void Cylinder::display(void) { Circle::display(); cout<< height = <

9 Inheritance void main(void) { Cylinder c(3,4,2.5,3.7); c.display(); cout<<"\nVolume of cylinder is : << c.GetVolume(); } Output: point=[3,4] radius = 2.5 height = 3.7 Volume of cylinder is : 72.61

10 Inheritance Order of execution of Constructors/Destructors  Instantiating derived-class object  Chain of constructor calls Base of inheritance hierarchy –Last constructor called in chain –First constructor body to finish executing –Example: Point / Circle / Cylinder hierarchy »Point constructor called last »Point constructor body finishes execution first Initializing data members –Each base-class constructor initializes data members »Inherited by derived class

11 Inheritance Order of execution of Constructors/Destructors  Destroying derived-class object  Chain of destructor calls Reverse order of constructor chain Destructor of derived-class called first Destructor of next base class up hierarchy next –Continue up hierarchy until final base reached »After final base-class destructor, object removed from memory  Base-class constructors, destructors, assignment operators  Not inherited by derived classes  Derived class constructors, assignment operators can call Constructors Assignment operators

12 Inheritance Example class Point { protected: int x,y; public: Point(int,int ); void display(void); ~Point() { cout<<"\nPoint Class Destructor\n"; } }; Point::Point(int a,int b) { cout<<"\nPoint Class Constructor\n"; x=a; y=b; } void Point::display(void) { cout<<"point = [" < { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/13/3620304/slides/slide_12.jpg", "name": "Inheritance Example class Point { protected: int x,y; public: Point(int,int ); void display(void); ~Point() { cout<< \nPoint Class Destructor\n ; } }; Point::Point(int a,int b) { cout<< \nPoint Class Constructor\n ; x=a; y=b; } void Point::display(void) { cout<< point = [ <

13 Inheritance class Circle : public Point { protected: double radius; public: Circle(int,int,double); void display(void); ~Circle() { cout<<"\nCircle Class Destructor\n"; } }; Circle::Circle(int a,int b,double c):Point(a,b) { cout<<"\nCircle Class Constructor\n"; radius = c; } void Circle::display(void) { Point::display(); cout<<" radius = "< { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/13/3620304/slides/slide_13.jpg", "name": "Inheritance class Circle : public Point { protected: double radius; public: Circle(int,int,double); void display(void); ~Circle() { cout<< \nCircle Class Destructor\n ; } }; Circle::Circle(int a,int b,double c):Point(a,b) { cout<< \nCircle Class Constructor\n ; radius = c; } void Circle::display(void) { Point::display(); cout<< radius = <

14 Inheritance class Cylinder:public Circle { double height; public: Cylinder(int,int,double,double); void display(void); double GetVolume(void); ~Cylinder() { cout<<"\nCylinder Class Destructor\n"; } }; Cylinder::Cylinder(int a,int b,double r,double h):Circle(a,b,r) { cout<<"\nCylinder Class Constructor\n"; height=h; } double Cylinder::GetVolume(void) { return 3.14 * radius * radius * radius; } void Cylinder::display(void) { Circle::display(); cout<<" height = "< { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/13/3620304/slides/slide_14.jpg", "name": "Inheritance class Cylinder:public Circle { double height; public: Cylinder(int,int,double,double); void display(void); double GetVolume(void); ~Cylinder() { cout<< \nCylinder Class Destructor\n ; } }; Cylinder::Cylinder(int a,int b,double r,double h):Circle(a,b,r) { cout<< \nCylinder Class Constructor\n ; height=h; } double Cylinder::GetVolume(void) { return 3.14 * radius * radius * radius; } void Cylinder::display(void) { Circle::display(); cout<< height = <

15 Inheritance void main(void) { Cylinder c(3,4,2.5,3.7); } Output: Point Class Constructor Circle Class Constructor Cylinder Class Constructor Cylinder Class Destructor Circle Class Destructor Point Class Destructor

16 Inheritance Overriding  The function in the derived class with the same function (variable) name will override the functions (variables) in the base classes.  But you can still invoke or get the overrode functions(variables) with scope resolution operator ”::”.  We have used overriding in previous examples…  display function of Point, Circle and Cylinder

17 Inheritance Types of inheritance  public  private  protected

18 Inheritance Public Inheritance  With public inheritance, public and protected members of the base class become respectively public and protected members of the derived class.

19 Inheritance Protected Inheritance  Public and protected members of the base class become protected members of the derived class.

20 Inheritance Private Inheritance  With private inheritance, public and protected members of the base class become private members of the derived class.

21 Inheritance public, protected and private Inheritance


Download ppt "Inheritance Inheritance-2. Inheritance Rewriting point and circle classes class Point { protected: int x,y; public: Point(int,int); void display(void);"

Similar presentations


Ads by Google