1 / 17

Object-Oriented Programming: Inheritance

Object-Oriented Programming: Inheritance. Inheritance Software reusability Create new class from existing class Absorb existing class’s data and behaviors Enhance with new capabilities Derived class inherits from base class Derived class More specialized group of objects

stewartd
Download Presentation

Object-Oriented Programming: Inheritance

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Object-Oriented Programming: Inheritance • Inheritance • Software reusability • Create new class from existing class • Absorb existing class’s data and behaviors • Enhance with new capabilities • Derived class inherits from base class • Derived class • More specialized group of objects • Behaviors inherited from base class • Can customize • Additional behaviors

  2. Introduction • Class hierarchy • Direct base class • Inherited explicitly (one level up hierarchy) • Indirect base class • Inherited two or more levels up hierarchy • Single inheritance • Inherits from one base class • Multiple inheritance • Inherits from multiple base classes • Base classes possibly unrelated

  3. Introduction • Three types of inheritance • public • Every object of derived class is also object of base class • Base-class objects are not objects of derived classes • Example: All cars vehicles, but not all vehicles cars • Can access non-private members of base class • Derived class can effect change to private base-class members • Through inherited non-private member functions • private • Alternative to composition • public and protected members are private • protected • public and protected members are protected

  4. Introduction • Abstraction • Focus on commonalities among objects in system • “is-a” vs. “has-a” • “is-a” • Inheritance • Derived class object treated as base class object • Example: Car is a vehicle • Vehicle properties/behaviors also car properties/behaviors • “has-a” • Composition • Object contains one or more objects of other classes as members • Example: Car has a steering wheel

  5. Base Classes and Derived Classes • Base classes and derived classes • Object of one class “is an” object of another class • Example: Rectangle is a shape. • Class Rectangle inherits from class Shape • Shape: base class • Rectangle: derived class • Base class typically represents larger set of objects than derived classes • Example: • Base class: Vehicle • Cars, trucks, boats, bicycles, … • Derived class: Car • Smaller, more-specific subset of vehicles

  6. Base Classes and Derived Classes • Inheritance examples

  7. Base Classes and Derived Classes • Inheritance hierarchy • Inheritance relationships: tree-like hierarchy structure • Each class becomes • Base class • Supply data/behaviors to other classes OR • Derived class • Inherit data/behaviors from other classes

  8. CommunityMember Single inheritance Single inheritance Single inheritance Multiple inheritance Employee Student Alumnus Faculty Staff Administrator Teacher AdministratorTeacher Inheritance hierarchy for university CommunityMembers.

  9. Shape TwoDimensionalShape ThreeDimensionalShape Circle Square Triangle Sphere Cube Tetrahedron Inheritance hierarchy for Shapes.

  10. Base Classes and Derived Classes • public inheritance • Specify with: Class TwoDimensionalShape : public Shape • Class TwoDimensionalShape inherits from class Shape • Base class private members • Not accessible directly • Still inherited • Manipulate through inherited member functions • Base class public and protected members • Inherited with original member access • friend functions • Not inherited

  11. protected Members • protected access • Intermediate level of protection between public and private • protected members accessible to • Base class members • Base class friends • Derived class members • Derived class friends • Derived-class members • Refer to public and protected members of base class • Simply use member names

  12. Protected Base Class Member #include <iostream> using namespace std; class B { protected: intbm; }; class D: public B { public: accessbm(int x){ bm=x+2; } }; int main(){ D obj; cout<<"bm VALUE IS "<<obj.accessbm(5)<<endl; return 0; }//MAIN

  13. Virtual Function Member function that is defined in base class and redefined by derived classes. Precede the function’s declaration with the keyword virtual in the base class. Pure Virtual Function A function in an abstract base class that does not have implementation in the base class and instead its body has =0. A pure virtual function must be preceded by the keyword virtual.   class employee { virtual double computesalary()=0; }; Different versions of the pure virtual function’s implementation are written in the subclasses

  14. #include<iostream> using namespace std; class Shape { public: virtual int computeArea()=0; };//SHAPEAREA class Circle: public Shape { private: int radius; public: Circle(int r){ radius=r;} int computeArea() { return 3.14*radius*radius; } };//CIRCLE class Square: public Shape{ private: int side; public: Square(int s){side=s;} int computeArea() { return side*side; } };//SQUARE

  15. class Rectangle: public Shape { private: int length; int width; public: Rectangle(int l, int w){length=l; width=w;} int computeArea() { return length*width; } };//RECTANGLE int main(){ Circle a(20); Square b(12); Rectangle c(5,10); cout<<"Area of circle is: "<<a.computeArea()<<endl; cout<<"Area of square is: "<<b.computeArea()<<endl; cout<<"Area of rectangle is: "<<c.computeArea()<<endl; return 0; }//MAIN

  16. Run-Time Polymorphism #include <iostream> using namespace std; class B { public: virtual void vm( ){cout <<"HERE IS THE BASE FUNCTION "<<endl;} };//B class D : public B { public: virtual void vm( ){ cout <<"HERE IS THE DERIVED FUNCTION "<<endl;} };//D int main(){ B obb; D obd; B* obp; obp= &obb; obp->vm(); obp=&obd; obp->vm(); return 0; }//MAIN

  17. public, protected and private Inheritance

More Related