Inheritance

669 views
594 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
669
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
5
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Inheritance

  1. 1.  The OO principle of inheritance enables you to create a generalized class and then derive more specialized classes from it. Inheritance is the ability to take on the characteristics of the class or derived class on which it is based. Specifies an “is-a” kind of relationship
  2. 2. Person Employee StudentFull-time Part-timeEmployee Employee
  3. 3. ShapeRectangle Circle Square
  4. 4.  New classes that we create from the existing class are called derived classes; the existing classes are called base classes.
  5. 5. class className:memberAccessSpecifier baseClassName{ memberList;}; Where:  memberAccessSpecifier – is public, private, or protected. When no memberAccessSpecifier is specified, it is assumed to be a private inheritance.
  6. 6. class Circle : public Shape{ . . .};
  7. 7. class Circle : private Shape{ . . .};
  8. 8. 1. The private members of a base class are private to the base class; hence the members of the derived class cannot directly access them. In other words, when you write the definitions of the member functions of the derived class, you cannot directly access the private members of the base class.
  9. 9. 2. The public members of a base class can be inherited either as public members or as private members by the derived class. That is, the public members of the base class can become either public or private members of the derived class.
  10. 10. 3. The derived class can include additional members – data and/or functions.4. The derived class can redefine the public member functions of the base class. That is, in the derived class, you can have a member function with the same name, number and types of parameters as function in the base class. However, this redefinition applies only to the object of the derived class, not to the objects of the base class.
  11. 11. 5. All member variables of the base class are also member variables of the derived class. Similarly, the member functions of the base class(unless redefined) are also member functions of the derived class. (Remember Rule 1 when accessing a member of the base class in the derived class.
  12. 12. class Derived:Base class Base{ { int y; int x; public : public : void print() const; void print()const;}; };void Derived::print()const void Base::print()const{ { cout<<y<<endl; cout<<x<<endl;} }
  13. 13.  To redefine a public member function of a base class in the derived class, the corresponding function in the derived class must have the same name, number, and types of parameters.
  14. 14. class RectangleType{ public: void setDimension(double, double); double getLength()const; double getWidth() const; double area()const; double perimeter()const; void print()const; RectangleType(); RectangleType(double, double); private: double length; double width;};
  15. 15. #include "RectangleType.h" double RectangleType::getLength#include<iostream> ()constusing namespace std; { return length;void RectangleType::setDimension } (double l, double w) double RectangleType::getWidth{ ()const if (l>=0) { length = l; return width; else } length =0; if (w>=0) width = w; else width = 0;}
  16. 16. double RectangleType::area()const RectangleType::RectangleType(double l,{ double w) return length * width; {} setDimension(l,w);double RectangleType::perimeter } ()const RectangleType::RectangleType(){ { return 2*(length + width); length =0;} width =0;void RectangleType::print() const }{ cout<<"Length = "<<length <<"Width = " <<width;}
  17. 17.  Define a class named BoxType BoxType contains data members that stores the length, width and height of a box. It has the following member functions :  Function that sets the dimension of the box  Function that sets a value for each data member of the class  Function that returns the value of each data member of the class  Function that prints the values of the data members of the class  Function that computes and returns the area of the box  Function that computes and returns the volume of the box  Default constructor which initializes data members to 0  Parameterized constructor which initializes data member to a value set by the object of the class
  18. 18.  In general, while writing the definitions of the member functions of a derived class to specify a call to a public member function of the base class we do the following:  If the derived class overrides a public member function of the base class, then to specify a call to that public member function of the base class use the name of the base class followed by the scope resolution operator, ::, followed by the function name with the appropriate parameter list.
  19. 19.  If the derived class does not override a public member function of the base class, you may specify a call to that public member function by using the name of the function and the appropriate parameter list.
  20. 20.  Recall:  private members of a class are private to the class and cannot be directly accessed outside the class. Only member functions of that class can access the private members.  If public, anyone can access that member  So for a base class to give access to a member to its derived class and still prevent its direct access outside the class, you must declare the member under the memberAccessSpecifier protected. ▪ The accessibility of a protected class is between public and private ▪ A derived class can directly access the protected members of the base class.
  21. 21.  Example: class B : memberAccessSpecifier A { : : }; memberAccessSpecifier is either private, public or protected
  22. 22.  If memberAccessSpecifier is public – that is inheritance is public - then:  The public members of A are public members of B. They can be directly accessed in class B.  The protected members of A re protected members of B. They can be directly accessed by the member functions of B.  The private members of A are hidden in B. They can be accessed by the member functions of B through the public and protected members of A.
  23. 23.  If memberAccessSpecifier is protected– that is inheritance is protected - then:  The public members of A are protected members of B. They can be accessed by the member functions of B.  The protected members of A are protected members of B. They can be accessed by the member functions of B.  The private members of A are hidden in B. They can be accessed by the member functions of B through the private or protected members of A.
  24. 24.  If memberAccessSpecifier is private– that is inheritance is private - then:  The public members of A are private members of B. They can be accessed by the member functions of B.  The protected members of A are private members of B. They can be accessed by the member functions of B.  The private members of A are hidden in B. They can be accessed by the member functions of B through the private or protected members of A.

×