lec 30.31 - inheritance
TRANSCRIPT
![Page 1: Lec 30.31 - inheritance](https://reader033.vdocuments.net/reader033/viewer/2022051314/555e347ad8b42a63048b4771/html5/thumbnails/1.jpg)
InheritanceInheritance
Chapter: Chapter: 0909
Lecture: 30 & 31Lecture: 30 & 31
Date: 08.10.2012Date: 08.10.2012
![Page 2: Lec 30.31 - inheritance](https://reader033.vdocuments.net/reader033/viewer/2022051314/555e347ad8b42a63048b4771/html5/thumbnails/2.jpg)
Inheritance: Base and Derived Classes Inheritance is the Inheritance is the
process of process of creating a new creating a new classes (classes (derivedderived classes) from classes) from existing classes existing classes (based classes).(based classes).
![Page 3: Lec 30.31 - inheritance](https://reader033.vdocuments.net/reader033/viewer/2022051314/555e347ad8b42a63048b4771/html5/thumbnails/3.jpg)
Inh
eri
tan
ce:
Base
an
d D
eri
ved
C
lass
es
![Page 4: Lec 30.31 - inheritance](https://reader033.vdocuments.net/reader033/viewer/2022051314/555e347ad8b42a63048b4771/html5/thumbnails/4.jpg)
Inheritance: AdvantagesInheritance: Advantages
Permits code reusabilityPermits code reusability
Reusing existing code saves time Reusing existing code saves time and money and increases a and money and increases a program’s reliabilityprogram’s reliability
![Page 5: Lec 30.31 - inheritance](https://reader033.vdocuments.net/reader033/viewer/2022051314/555e347ad8b42a63048b4771/html5/thumbnails/5.jpg)
Inheritance: ShapesInheritance: Shapes
RectangleTriangle
Polygon
![Page 6: Lec 30.31 - inheritance](https://reader033.vdocuments.net/reader033/viewer/2022051314/555e347ad8b42a63048b4771/html5/thumbnails/6.jpg)
No-Inheritance: ShapesNo-Inheritance: Shapesclass Rectangle{
private:
int numVertices; int xCoord, yCoord; public: void set(int x, int y, int nV); int area();
};
RectangleTriangle
Polygon
class Polygon{ private:
int numVertices; int xCoord, yCoord;
public: void set(int x, int y, int
nV); int area();};
class Triangle{
private: int numVertices;
int xCoord, yCoord; public:
void set(int x, int y, int nV); int area();};
![Page 7: Lec 30.31 - inheritance](https://reader033.vdocuments.net/reader033/viewer/2022051314/555e347ad8b42a63048b4771/html5/thumbnails/7.jpg)
Inheritance: Inheritance: ShapesShapes
RectangleTriangle
Polygonclass Polygon{ protected:
int numVertices; float xCoord, yCoord;
public: void set(int x, int y, int
nV);};
class Rectangle : public Polygon{public: int area();
};
![Page 8: Lec 30.31 - inheritance](https://reader033.vdocuments.net/reader033/viewer/2022051314/555e347ad8b42a63048b4771/html5/thumbnails/8.jpg)
Inheritance: Inheritance: ShapesShapes
RectangleTriangle
Polygonclass Polygon{ protected:
int numVertices; float xCoord, yCoord;
public: void set(float x, float y, int
nV);};
class Rectangle : public Polygon{public: float area();
};
class Triangle : public Polygon{public: int area();
};
![Page 9: Lec 30.31 - inheritance](https://reader033.vdocuments.net/reader033/viewer/2022051314/555e347ad8b42a63048b4771/html5/thumbnails/9.jpg)
Inheritance: SyntaxInheritance: Syntax Syntax:Syntax:
class class DerivedClassNameDerivedClassName : access-level : access-level BaseClassNameBaseClassName
where where access-levelaccess-level specifies the type of derivationspecifies the type of derivation
private by default, orprivate by default, or PublicPublic
Any class can serve as a base classAny class can serve as a base class Thus a derived class can also be a base classThus a derived class can also be a base class
![Page 10: Lec 30.31 - inheritance](https://reader033.vdocuments.net/reader033/viewer/2022051314/555e347ad8b42a63048b4771/html5/thumbnails/10.jpg)
Inheritance: SyntaxInheritance: Syntax Syntax:Syntax:
class class DerivedClassNameDerivedClassName : access-level : access-level BaseClassNameBaseClassName
Example:Example:
class class BaseClassBaseClass
{{
}}
class DerivedClass: public class DerivedClass: public BaseClassBaseClass
{{
}}
![Page 11: Lec 30.31 - inheritance](https://reader033.vdocuments.net/reader033/viewer/2022051314/555e347ad8b42a63048b4771/html5/thumbnails/11.jpg)
Access Specifier Access Specifier withoutwithout InheritanceInheritance
![Page 12: Lec 30.31 - inheritance](https://reader033.vdocuments.net/reader033/viewer/2022051314/555e347ad8b42a63048b4771/html5/thumbnails/12.jpg)
Access Specifier Access Specifier withwith InheritanceInheritance
![Page 13: Lec 30.31 - inheritance](https://reader033.vdocuments.net/reader033/viewer/2022051314/555e347ad8b42a63048b4771/html5/thumbnails/13.jpg)
Inheritance and Inheritance and AccessibilityAccessibility
![Page 14: Lec 30.31 - inheritance](https://reader033.vdocuments.net/reader033/viewer/2022051314/555e347ad8b42a63048b4771/html5/thumbnails/14.jpg)
Overriding member Overriding member functionsfunctions
Member functions in a derived class can Member functions in a derived class can have the have the same namessame names as in the base class as in the base class
When the same function exists in both the When the same function exists in both the base and derived classes, the function in base and derived classes, the function in the derived class will be executed.the derived class will be executed. In order to call a function in the base class use the use the In order to call a function in the base class use the use the
scope resolution operator, e.g.,scope resolution operator, e.g.,
Stack :: push()Stack :: push()
Name (base class)
Scope
resolution
operator
Call to a member function in base class
![Page 15: Lec 30.31 - inheritance](https://reader033.vdocuments.net/reader033/viewer/2022051314/555e347ad8b42a63048b4771/html5/thumbnails/15.jpg)
Stack ApplicationsStack Applications Memory managementMemory management Tower of HanoiTower of Hanoi
![Page 16: Lec 30.31 - inheritance](https://reader033.vdocuments.net/reader033/viewer/2022051314/555e347ad8b42a63048b4771/html5/thumbnails/16.jpg)
Stack ApplicationsStack Applications Converting a decimal number to binaryConverting a decimal number to binary
![Page 17: Lec 30.31 - inheritance](https://reader033.vdocuments.net/reader033/viewer/2022051314/555e347ad8b42a63048b4771/html5/thumbnails/17.jpg)
Inheritance: ShapesInheritance: Shapes
Point
Circle 3D-Point
class Point{protected: int x, y;public: void set (int a, int b);
};
class Circle : public Point{private:
double r;};
class 3D-Point: public Point{private:
int z;};
xy
xyr
xyz