Unformatted text preview:

InheritanceSlide 2Slide 3Slide 4Slide 5Slide 6Slide 7C++ offers three kind of inheritancePublic inheritanceClass Circle inherits class PointProtected memberRewrite Point and Circle classConstructorsimplementationPoint want to have private data membersSlide 16Inheritance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 classClass 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;};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 behaviorsVehicleCars TrucksMotorcycle2 doors4 doorsInheritancePointCircle rectanglecylindershapeTwo Dim shapeThree Dim shapeCircleSquareTriangle sphere cubePersonEmployeestudentFull Time Part timeUndergraduateGraduateStudent workerC++ 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 classPublic 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 classClass Circle inherits class Pointclass Circle : public Point{ double r; public: void setR(double rad); double getR(); void print(); double area(); double diameter(); double circumference();};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 classRewrite Point and Circle classclass 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;};Constructorsclass 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;};implementationPoint::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;}Point want to have private data membersclass 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;};implementationPoint::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


View Full Document

CUNY CS 503 - Inheritance

Documents in this Course
Load more
Download Inheritance
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Inheritance and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Inheritance 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?