Class is used to represent behavior and properties of an object. E.g. class Car Sports Car Inheritance Inheritance allows us to define a class in terms of another class, which makes it easier to create and maintain an application. Reuse of code and fast implementation. The existing class is called the base class, and the new class is referred to as the derived class. The idea of inheritance implements the is a relationship. Inheritance(syntax)
A protected member variable or function is
very similar to a private member but it provided one additional benefit that they can be accessed in child classes/derived classes. Example What does a Derived Class Have? An object of the derived class has: all members defined in child class all members declared in parent class
An object of the derived class can use:
all public members defined in child class all public members defined in parent class Example Consider a base class Shape and its derived class Rectangle as follows: // Base class class Shape { public: void setWidth(int w) { width = w; } void setHeight(int h) { height = h; } protected: int width; int height; }; Example // Derived class class Rectangle: public Shape { public: int getArea() { return (width * height); int main(void) } { }; Rectangle Rect; Rect.setWidth(5); Rect.setHeight(7); // Print the area of the object. cout << "Total area: " << Rect.getArea() << endl; return 0; } Types of Inheritance Single Inheritance Multilevel Inheritance Multiple Inheritance Hierarchical Inheritance Hybrid Inheritance Single Inheritance Multilevel Inheritance Multiple Inheritance Hierarchical Inheritance Hierarchical Inheritance Visibility Modes A Private Protected Public
protected
Private Private Private
Protected Protected Protected Public Public Public B B B Example class A class B: public A { { int a; public: protected: void setdata(int x) void setvalue(int k) {setvalue(x);} {a=k;} }; };
int main() int main() int main()
{ { { B obj; A obj; B obj; obj.setvalue(5); obj.setvalue(5); obj.setdata(5); getch(); getch(); getch(); return 0; return 0; return 0; } } } ERROR! ERROR! Public Inheritance Is a relationship is always implemented as a public inheritance. An object of a derived class 'is a(n)' object of the base class Example: an UnderGrad is a Student a Mammal is an Animal A Dog is a mammal A derived object has all of the characteristics of the base class Inheritance A derived class inherits all base class methods with the following exceptions:
• Constructors, destructors and copy constructors of
the base class.
• Overloaded operators of the base class.
• The friend functions of the base class.
Order of Constructor Call. Derived classes can have their own constructors and destructors
When an object of a derived class is created, the base
class’s constructor is executed first, followed by the derived class’s constructor Order of Destructor Call. Derived classes can have its own destructor.
When an object of a derived class goes out of it’s
scope, the derived class’s destructor is executed first, followed by the child class’s destructor. Member Function Overriding Suppose, both base class and derived class have a member function with same name and arguments (number and type of arguments). If you create an object of the derived class and call the member function which exists in both classes (base and derived), the member function of the derived class is invoked and the function of the base class is ignored. This feature in C++ is known as function overriding. Function Overloading VS Overriding 1. Inheritance: Overriding of functions occurs when one class is inherited from another class. Overloading can occur without inheritance. 2. Function Signature: Overloaded functions must differ in function signature i.e. either number of parameters or type of parameters should differ. In overriding, function signatures must be same. 3. Behavior of functions: Overriding is needed when derived class function has to do some added or different job than the base class function. Overloading is used to have same name functions which behave differently depending upon parameters passed to them. Example How to access the overridden function in base class. Encapsulation Encapsulation. An act of combining properties and methods, related to the same object, is known as Encapsulation.
Book
InputData();
showData(); Methods
updateprice();
Variables Why Encapsulation. Object becomes equipped with sufficient information and set of operations.
Any system can be assumed as a collection of
objects.
These objects are capable to interact with each other
using various methods. So encapsulation has two concepts: Binding of data and functions. Controlled access. (Class) Why Encapsulation.