170 likes | 177 Views
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
E N D
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
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
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
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
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
Base Classes and Derived Classes • Inheritance examples
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
CommunityMember Single inheritance Single inheritance Single inheritance Multiple inheritance Employee Student Alumnus Faculty Staff Administrator Teacher AdministratorTeacher Inheritance hierarchy for university CommunityMembers.
Shape TwoDimensionalShape ThreeDimensionalShape Circle Square Triangle Sphere Cube Tetrahedron Inheritance hierarchy for Shapes.
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
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
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
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
#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
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
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