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. Note that a circle is a shape, a square is a shape, but a square is not a circle. Note that 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. There are three kinds of inheritance: public protected private

5 5 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

6 6 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 this becomes relevant for classes derived from B the private parts of A are inaccessible

7 7 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

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

9 9 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.

10 10 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

11 11 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

12 12 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