![]() ![]() class ABC: XYZ //private derivation by default class ABC : protected XYZ //protected derivationĤ. class ABC : public XYZ //public derivationģ. class ABC : private XYZ //private derivationĢ. However, it does inherit a full parent object, which contains any private members which that class declares.ġ. Note: A derived class doesn’t inherit access to private data members. If neither is specified, PRIVATE is taken as default Implementing inheritance in C++: For creating a sub-class that is inherited from the base class we have to follow the below syntax.ĭerived Classes: A Derived class is defined as the class derived from the base class.ĭerived_class_name - name of the new class, which will inherit the base classĪccess-specifier - either of private, public or protected. ![]() Using inheritance, we have to write the functions only one time instead of three times as we have inherited the rest of the three classes from the base class (Vehicle). Look at the below diagram in which the three classes are inherited from vehicle class: If we create a class Vehicle and write these three functions in it and inherit the rest of the classes from the vehicle class, then we can simply avoid the duplication of data and increase re-usability. To avoid this type of situation, inheritance is used. This increases the chances of error and data redundancy. You can clearly see that the above process results in duplication of the same code 3 times. If we create these classes avoiding inheritance then we have to write all of these functions in each of the three classes as shown below figure: ![]() The methods fuelAmount(), capacity(), applyBrakes() will be the same for all three classes. You need to create classes for Bus, Car, and Truck. ISRO CS Syllabus for Scientist/Engineer ExamĬonsider a group of vehicles.ISRO CS Original Papers and Official Keys.GATE CS Original Papers and Official Keys. ![]()
0 Comments
Leave a Reply. |