Page 2 :
2, ae :, WF Object Oriented Programming using C++ (MSBTE) 6-2 Inhertanes, , , , , , , , , , , @Q.6.1.8 State and explain various visibility modifiers in, inheritance. (Ref. Sec. 6.1.1), (S-16, W-16, 4 Marks), Q.6.1.9 Define derived class. Give one example., (Ref. Sec, 6.1.1 and Prog. 6.1.1), (S-17, 2 Marks), Q.6.1.10 State any two access specifier with example., (Ref. Sec. 6.1.1) (S- 15, W-16, 2 Marks), Q. 6.1.11 Explain various types of inheritance with example, (Ref. Secs. 6.1.1 to 6.1.5 and 6.1.7), (W-16, 8 Marks), 'Q. 6.1.12 How protected access specifier is different from!, private? (Ref. Sec. 6.1.1) (W-15, 4 Marks), , Q.6.1,11 What are the different types of inheritance?, Explain each with exampie., (Ref, Secs. 6.1.1 to 6.1.5 and 6.1.7), , (W-16, 10 Marks), , , , The members of a class may be public, protected or private, and the class may also be derived in the following ways :, , 1, Public, , 2. Protected, , 3. Private, , , , These keywords viz. public, protected and private are called, as “access specifiers”, as they decide the access of a member., , ‘The visibility of base class members within the derived class, , is as shown in Table 6.1.1, , Table 6.1.1 : Visibility of base class members, in the derived class, , , , , , , , Public members Public Protected Private, Protected members | Protected | Protected Private, Private members Not Not Not, inherited inherited inherited, , , , , , , , , , , , , , , , Hence, you will notice in the Table 6.1.1, in case of Public, , derivation the public and protected members of the base clasy, remain in the same visibility even in the derived class, The, private members are neyer derived., , In case of protected derivation the public and Protecteg, members of the base class become protected in the derivey, class. The private members are never derived., , In case of private derivation the public and Protectey, members of the base class become private in the deriveg, , class. The private members are never derived., , There are various types of inheritance namely ;, , Single Inheritance, , Multilevel Inheritance, Multiple Inheritance, Hybrid Inheritance, , Hierarchical Inheritance, , , , To derive a class from another we need to use the following, syntax:, , class derived_class_name : access_specifier, , basi, , , , Let us see these types of inheritance in the subsequent subsections, , ee, , Syllabus Topic : Single Type of Inheritance, , 6.1.2 Single Inheritance, , > (w-15, w-16, $47), ~ _ Inthis case only one class is derived from another class., , The class diagram and a program example is given below., , » Program 6.1.1: Write a program to add two numbers, using single inheritance such that the base clas*, function must accept the two numbers from the Us, and the derived class function must add the?, numbers and display the sum., , Solution :, , , , , , , , i in, The class diagram of this requirement is as shown *, , Fig, P. 6.1.1. This diagram shows that the class “Sum” ise", from class “Data” in public Visibility., , , , , , wre
Page 3 :
Explanation :, , , , , ‘As shown in the class diagram, the base class data has two, member variables namely ‘a’ and ‘pb’. These, , protected, they are accessible from, , members are kept protected, so that, the derived class function to calculate the sum., in public visibility to read, , The base class also has @ function, ed class also has public, , the inputs from user. The deriv, member functions add() and display() to find the sum of the, , two numbers taken and to display this sum respectively., , The class “Sum” is derived from the class “Data”. This, , derivation is implemented using the already discussed syntax, in the statement:, class Sum: public Data, , The members of the base class are shown available in the, , derived class in the class diagram in Fig. P.6.1.1 but these, member functions and member variables are not to be, , defined again in the derived class when actually writing the, , program. This concept of gaining access to the members of, the base class is called as code reusability., , The main() function has first a creation of the object of the, class “Sum”. The object of the class sum can access all the, functions i.e, read(), add() and display(); because the derived, class derives the protected members in the same visibility, when derived publicly as discussed in Table 6.1.1., , FF Techhnouledge, Publications