Inheritance defines an "is a" relationship. The derived class is a type of its base class. A derived class inherits both the data members and methods of its base class. It may also define additional members and methods that support specialized functionality.
class DerivedClass : public BaseClass
{
....
};
The parts of a class that are declared public can be accessed by the class itself, publicly derived subclasses, and other classes, functions and code. The parts of a class declared as protected can be accessed by the class itself and by publicly derived subclasses. The parts of a class declared as private can be accessed only by the class itself.
Constructors, Copy constructors, and Destructors are not inherited.
The base class part of an object is always constructed first and destroyed last. The subclass part of an object is constructed last and destroyed first.
Argument passing
Base () : Age(30), Name("Srujan") {}
Base(int w) : Age(w), Name("Srujan") {}
BAse(int w, string f) : Age(w), Name(f) {}
Derived() {}
Derived(int w) : Base(w) {}
Derived(int w, string f) : Base(w,f) {}
Overriding Methods
A derived class can use the methods of its base class(es), or it can override them. The method in the derived class must have the same signature and return type as the base class method to override. The signature is number and type of arguments and the constantness (const, non- const) of the method. When an object of the base class is used, the base class method is called. When an object of the subclass is used, its version of the method is used if the method is overridden. If the base class had overloaded a particular method, overriding a single one of the overloads in the derived will hide the rest of the overloaded methods to the derived classes. Overridden methods should usually be virtual.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment