Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Inheritance Inheritance is a natural way to model the world in object-oriented programming. It is used when you have two types of objects where one is.

Similar presentations


Presentation on theme: "1 Inheritance Inheritance is a natural way to model the world in object-oriented programming. It is used when you have two types of objects where one is."— Presentation transcript:

1 1 Inheritance Inheritance is a natural way to model the world in object-oriented programming. It is used when you have two types of objects where one is an extension of the other. Examples: Vehicle - Car Shape - Circle In these examples, the first class provides a rather general, abstract description while the second class is more detailed. a circle has all the characteristics and functions of a shape, plus some circle-specific characteristics/functions.

2 2 Inheritance You may have several types of objects that are all extensions of the same type: The key is to correctly identify the similarities and differences between the types. A circle is a shape, a square is a shape, but a square is not a circle. Every circle is a shape but not every shape is a circle. Shape CircleSquareTriangle

3 3 Inheritance Advantages of inheritance: Code reuse A function that operates on a Shape object, can be used on any of the Circle, Square or Triangle objects. Structural simplicity By modeling the world in this way, we reduce its complexity

4 4 Inheritance The main class (e.g. Shape) is called the base class The subclasses (e.g. Circle, Square) are called derived classes The derived classes inherit attributes and operations from the main class. However, not everything that is inherited can be accessed directly. Anything that is private in the base class cannot be accessed in the derived class. However, we do not want to have to make certain data members public only for the benefit of a derived class. Instead, we can make them protected.

5 5 Inheritance A class member (data or function) may be public, protected, or private Furthermore, the type of inheritance may be public, protected, or private Depending on the inheritance type, there are different access rules for each kind of class member

6 6 Public inheritance If A is the base class and B is the derived class: In B, the public parts of A are public the protected parts of A are protected the private parts of A are inaccessible Public inheritance is the most common type of inheritance. It defines an is-a relationship

7 7 Protected inheritance If A is the base class and B is the derived class: In B, the public parts of A are protected the protected parts of A are protected the private parts of A are inaccessible Not very common...

8 8 Private inheritance If A is the base class and B is the derived class: In B, the public parts of A are private the protected parts of A are private the private parts of A are inaccessible Private inheritance is generally used to define a has-a relationship, as an alternative to composition.

9 9 Public inheritance We will concentrate on public inheritance. Syntax: class Shape { private:... protected:... public:... }; class Square : public Shape { private:... public:... };

10 10 Public inheritance class Shape { private:... public:... void returnPosition(); }; class Square : public Shape { private:... public:... }; int main () { Square s; s.returnPosition();... } If there is no returnPosition() defined in Square, the one from Shape is called.

11 11 Public inheritance class Shape { private:... public:... void printAttributes(); }; class Square : public Shape { private:... public:... void printAttributes(); }; int main () { Square s; s.printAttributes();... } This member hides the one defined in Shape

12 12 Public inheritance Shape::Shape() { cout << “Shape constructor\n”; } Square::Square() { cout << “Square constructor\n”; } int main () { Square s;... } this calls the Square constructor which automatically calls the Shape constructor. The program will print: Shape constructor Square constructor

13 13 Public inheritance int main () { Shape someshape; Square box; someshape = box; // OK (a box is a shape) // However, only the shape part // of the box is assigned to someshape box = someshape; // ERROR (a shape is not a box)... }


Download ppt "1 Inheritance Inheritance is a natural way to model the world in object-oriented programming. It is used when you have two types of objects where one is."

Similar presentations


Ads by Google