Presentation is loading. Please wait.

Presentation is loading. Please wait.

Inheritance Is a form of software reusability in which programmers create classes that absorb an existing class’s data and behaviors and enhance them with.

Similar presentations


Presentation on theme: "Inheritance Is a form of software reusability in which programmers create classes that absorb an existing class’s data and behaviors and enhance them with."— Presentation transcript:

1 Inheritance Is a form of software reusability in which programmers create classes that absorb an existing class’s data and behaviors and enhance them with new capabilities Programmer designate that the new class should inherit the members of an existing class

2 Class Point { int x; int y; public: void setX(int xval); void setY(int yval); int getX(); int getY(); void print() const; }; Class Circle {int x; int y; double r; public: void setX(int xval); void setY(int yval); void setR(double rad); int getX(); int getY(); double getR(); double area(); double diameter(); double circumference(); void print() const; };

3 Class Circle repeats part of class Point Software reusability saves time during program development Existing class is called base class The new class is referred to as derived class A derived class represents a more specialized group of objects A derived class contains behaviors inherited from its base class plus additional behaviors

4 Vehicle CarsTrucks Motorcycle 2 doors 4 doors Inheritance

5 Point Circle rectangle cylinder

6 shape Two Dim shape Three Dim shape Circle Square Trianglesphere cube

7 Person Employee student Full TimePart time Undergraduate Graduate Student worker

8 C++ offers three kind of inheritance Public Protected Private Syntax class derived : public base {... } Every object of a derived class is also an object of base class Base class object are not objects of their derived class

9 Public inheritance Derived class member function might required access to base class data member and member functions A derived class can access the non- private members of its base class A derived class cannot access the private member of its base class

10 Class Circle inherits class Point class Circle : public Point { double r; public: void setR(double rad); double getR(); void print(); double area(); double diameter(); double circumference(); };

11 Protected member A base class’s protected members can be accessed by members and friends of that base class and by members and friends of any classes derived from that base class

12 Rewrite Point and Circle class class Point { protected: int x; int y; public: void setX(int xval); void setY(int yval); int getX(); int getY(); void print() const; }; class Circle:public Point {double r; public: void setR(double rad); double getR(); double area(); double diameter(); double circumference(); void print() const; };

13 Constructors class Point { protected: int x; int y; public: Point(int=0,int=0); void setX(int xval); void setY(int yval); int getX(); int getY(); void print() const; }; class Circle:public Point {double r; public: Circle(int=0,int=0,double=0); void setR(int rad); double getR(); double area(); double diameter(); double circumference(); void print() const; };

14 implementation Point::Point(int xval, int yval) { x = xval; y = yval; } void Point::print() const { cout<<“Point is at (“ <<x<<“,”<<y<<“)”; } Circle::Circle(int xval, int yval, double rval) { x = xval; y = yval; r = rval; } void Circle::print() const { cout<<“center at (“<<x<<“,” <<y<<“)”; cout<<“radious is “<<r; }

15 Point want to have private data members class Point { int x; int y; public: Point(int=0,int=0); void setX(int xval); void setY(int yval); int getX(); int getY(); void print() const; }; class Circle:public Point {double r; public: Circle(int=0,int=0,double=0); void setR(int rad); double getR(); double area(); double diameter(); double circumference(); void print() const; };

16 implementation Point::Point(int xval, int yval) { x = xval; y = yval; } Void Point::print() const { Cout<<“Point is at (“ <<x<<“,”<<y<<“)”; } Circle::Circle(int xval, int yval, double rval):Point( xval, yval) { r = rval; } void Circle::print() const { cout<<“center at )“; Point::print(); cout<<“radious is “<<r; }


Download ppt "Inheritance Is a form of software reusability in which programmers create classes that absorb an existing class’s data and behaviors and enhance them with."

Similar presentations


Ads by Google