09810144315 Why is the Inheritance concept needed? 12TH/… ·  · 2014-04-27Creating or deriving...

25
1 1. Define Inheritance. Give an example. Solution: Inheritance is the process by which new classes called derived classes are created from existing classes called base classes. The derived classes have all the features of the base class and the programmer can choose to add new features specific to the newly created derived class. For example, a programmer can create a base class named fruit and define derived classes as mango, orange, banana, etc. Each of these derived classes, (mango, orange, banana, etc.) has all the features of the base class (fruit) with additional attributes or features specific to these newly created derived classes. Mango would have its own defined features, Orange would have its own defined features, Banana would have its own defined features, etc. 2. Why is the Inheritance concept needed? Solution: Need for inheritance Inheritance is a splendid concept of object-oriented languages. There are several reasons why inheritance was introduced into oop language. We are discussing some major reasons behind the introduction of inheritance. 1. One major reason behind this is the capability to express the inheritance relationship which ensures the closeness with the real-world models. 2. Another reason is the idea of reusability. Inheritance allows the addition of additional features to an existing class without modifying it. One can derive a new class (subclass or derived class) from an existing one and add new features to it. 3. One reason is the transitive nature of inheritance. For example, if a class B inherits properties of another class A, then all subclasses of B will automatically inherit the properties of A. This property is called transitive nature of inheritance. 3. List out the advantages of Inheritance. Solution: Reusability: Inheritance helps the code to be reused in many situations. The base class is defined and once it is compiled, it need not be reworked. Using the concept of inheritance, the programmer can create as many derived classes from the base class as needed while adding specific features to each derived class as needed. Saves Time and Effort: The above concept of reusability achieved by inheritance saves the programmer time and effort. Since the main code that is written can be reused in various situations as needed. 4. List out the different forms of inheritance: MATHEMATIC CENTER D96 MUNIRKA VILLAGE NEW DELHI 110067 & VIKAS PURI NEW DELHI CONTACT FOR COACHING MATHEMATICS FOR 11TH 12TH NDA DIPLOMA SSC CAT SAT CPT CONTACT FOR ADMISSION GUIDANCE B.TECH BBA BCA, MCA MBA DIPLOMA AND OTHER COURSES 09810144315 CONTACT FOR MATHEMATICS GROUP/HOME COACHING FOR CLASS 11TH 12TH BBA,BCA, DIPLOMA CPT CAT SSC AND OTHER EXAMS Also home tutors for other subjects in south and west delhi EMAIL:1 [email protected] web site www.mathematic.in 1 www.mathematic.in 09810144315

Transcript of 09810144315 Why is the Inheritance concept needed? 12TH/… ·  · 2014-04-27Creating or deriving...

1

1. Define Inheritance. Give an example.

• Solution: Inheritance is the process by which new classes called derived classes are created from existing classes called base classes. The derived classes have all the features of the base class and the programmer can choose to add new features specific to the newly created derived class.

For example, a programmer can create a base class named fruit and define derived classes as mango, orange, banana, etc. Each of these derived classes, (mango, orange, banana, etc.) has all the features of the base class (fruit) with additional attributes or features specific to these newly created derived classes. Mango would have its own defined features, Orange would have its own defined features, Banana would have its own defined features, etc.

2. Why is the Inheritance concept needed?

• Solution: Need for inheritance

Inheritance is a splendid concept of object-oriented languages. There are several reasons why inheritance was introduced into oop language. We are discussing some major reasons behind the introduction of inheritance.

1. One major reason behind this is the capability to express the inheritance relationship which ensures the closeness with the real-world models.

2. Another reason is the idea of reusability. Inheritance allows the addition of additional features to an existing class without modifying it. One can derive a new class (subclass or derived class) from an existing one and add new features to it.

3. One reason is the transitive nature of inheritance. For example, if a class B inherits properties of another class A, then all subclasses of B will automatically inherit the properties of A. This property is called transitive nature of inheritance.

3. List out the advantages of Inheritance.

• Solution: Reusability:

Inheritance helps the code to be reused in many situations. The base class is defined and once it is compiled, it need not be reworked. Using the concept of inheritance, the programmer can create as many derived classes from the base class as needed while adding specific features to each derived class as needed.

Saves Time and Effort:

The above concept of reusability achieved by inheritance saves the programmer time and effort. Since the main code that is written can be reused in various situations as needed.

4. List out the different forms of inheritance:

MATHEMATIC CENTER D96 MUNIRKA VILLAGE NEW DELHI 110067 & VIKAS PURI NEW DELHI CONTACT FOR COACHING MATHEMATICS FOR 11TH 12TH NDA DIPLOMA SSC CAT SAT CPT CONTACT FOR ADMISSION GUIDANCE B.TECH BBA BCA, MCA MBA DIPLOMA AND OTHER COURSES 09810144315

CONTACT FOR MATHEMATICS GROUP/HOME COACHING FOR CLASS 11TH 12TH BBA,BCA, DIPLOMA CPT CAT SSC AND OTHER EXAMS Also home tutors for other subjects in south and west delhi EMAIL:1 [email protected] web site www.mathematic.in

1

www.mathem

atic.in

09810144315

2

• Solution: The different forms of Inheritance are:

o Single Inheritance

o Multilevel Inheritance

o Multiple Inheritances

o Hierarchical Inheritance

o Multipath Inheritance

o Hybrid Inheritance

5. Define Derived and base classes.

• Solution: Creating or deriving a new class using another class as a base is called inheritance in C++. The new class created is called a Derived class and the old class used as a base is called a Base class in C++ inheritance terminology.

6. Define Inheritance graph and Inheritance Hierarchy.

• Solution: Inheritance graph:

The chain depicting the derivation of a class from multiple base classes is known as an Inheritance graph.

Inheritance Hierarchy:

The chain depicting a relationship between a base class and derived class is known as Inheritance - Hierarchy.

7. Define Containership:

• Solution: When a class contains objects of other class types as its members, it is referred to as containership or containment or aggregation.

8. Class Y has been derived from class X. The class Y does not contain any data members

of its own. Does the class Y require constructors? If yes, why?

• Solution: Yes, the class Y must define a constructor because it is the responsibility of the derived class constructor to invoke the base class constructor by passing its required arguments to it. The constructor of the derived class Y will look like as:

Y(type1 x, type2 y,….):X(x,y,…)

MATHEMATIC CENTER D96 MUNIRKA VILLAGE NEW DELHI 110067 & VIKAS PURI NEW DELHI CONTACT FOR COACHING MATHEMATICS FOR 11TH 12TH NDA DIPLOMA SSC CAT SAT CPT CONTACT FOR ADMISSION GUIDANCE B.TECH BBA BCA, MCA MBA DIPLOMA AND OTHER COURSES 09810144315

CONTACT FOR MATHEMATICS GROUP/HOME COACHING FOR CLASS 11TH 12TH BBA,BCA, DIPLOMA CPT CAT SSC AND OTHER EXAMS Also home tutors for other subjects in south and west delhi EMAIL:2 [email protected] web site www.mathematic.in

2

www.mathem

atic.in

09810144315

3

{. . . }

Where Y is passing x,y,(and so on) arguments to base class constructor X::X(). The function call to the base class constructor follows the colon.

9. Describe how an object of a class contains objects of other classes which are created?

• Solution: The object containing other objects is known as a nested object. A nested object is created in two stages. First, the member objects are created using their respective constructor in the order that they are defined in and then the other ‘ordinary’ members of the nested object which are created. That is, the constructors of all the member objects are called before the constructor of the object enclosing other objects.

10. List out the different types of visibility modes.

• Solution: The visibility mode (public, private, protected) in the definition of the derived class specifies whether the features of the base class are privately derived or publicly derived or derived by being protected. The visibility modes basically control the access-specifier in the following table:

Visibility of inherited base class member in derived class

Visibility mode is

Inheritable public member becomes (in derived class)

Inheritable protected member

becomes (in derived class)

Private

Public

Protected

Private

Public

Protected

Private

Protected

Protected

Private

members of the base class are not directly accessible to derived class.

11. Briefly explain the concept behind private and protected visibility mode with an example.

• Solution: The private derivation means the derived class can access the public and private members of the class privately. With privately derived class, the public and protected members of the base class become private members of the derived class. That means the inherited members can be accessed only through member functions of the derived class. Also as the inherited members become private in the derived class, they cannot be inherited further if the derived class happens to be the base class of any other class. The private derivations in the classes mentioned below are:

Class Super //the base class,Super

MATHEMATIC CENTER D96 MUNIRKA VILLAGE NEW DELHI 110067 & VIKAS PURI NEW DELHI CONTACT FOR COACHING MATHEMATICS FOR 11TH 12TH NDA DIPLOMA SSC CAT SAT CPT CONTACT FOR ADMISSION GUIDANCE B.TECH BBA BCA, MCA MBA DIPLOMA AND OTHER COURSES 09810144315

CONTACT FOR MATHEMATICS GROUP/HOME COACHING FOR CLASS 11TH 12TH BBA,BCA, DIPLOMA CPT CAT SSC AND OTHER EXAMS Also home tutors for other subjects in south and west delhi EMAIL:3 [email protected] web site www.mathematic.in

3

www.mathem

atic.in

09810144315

4

{

//same as before

};

Class Sub:private Super //the privately derived class,Sub

{

//same as before

The protected visibility mode:

The protected derivation means that the derived class can access the public and private members of the base class protectively. With a protected derived class, the public and protected members of the base class become protected members of the derived class. That means the inherited members are not available to the outside world and can be accessed only through the member functions of the derived class and the class based upon the derived class. These members can be inherited further if any classes are inherited from the derived class. The following are the protected derivation in the classes mentioned below:

Class Super

{

//same as before

};

Class Sub:protected Super

{

//same as before

};

12. Explain the concept behind public visibility mode briefly with an example.

• Solution: The public derivation means that the derived class can access the public and protected members of the base class but not the private members of the base class. With publicly derived class, the public members of the base class become the public members of the derived class, and the protected members of the base class become the protected members of the derived class. In other words, the public derivation does not change the access specifiers for inherited members. In other words, the public derivation does not change the access specifiers for inherited members in the derived class.

Inherited from Super class

MATHEMATIC CENTER D96 MUNIRKA VILLAGE NEW DELHI 110067 & VIKAS PURI NEW DELHI CONTACT FOR COACHING MATHEMATICS FOR 11TH 12TH NDA DIPLOMA SSC CAT SAT CPT CONTACT FOR ADMISSION GUIDANCE B.TECH BBA BCA, MCA MBA DIPLOMA AND OTHER COURSES 09810144315

CONTACT FOR MATHEMATICS GROUP/HOME COACHING FOR CLASS 11TH 12TH BBA,BCA, DIPLOMA CPT CAT SSC AND OTHER EXAMS Also home tutors for other subjects in south and west delhi EMAIL:4 [email protected] web site www.mathematic.in

4

www.mathem

atic.in

09810144315

5

Class Super

{

Private:

Int x;

Void check(void);

Public:

Int y;

Void display(void);

Protected:

Int z;

Void getval(void);

};

Class Sub:public Super

{

Private:

Int a;

Void init(void);

Public:

Int b;

Void readit(void);

Protected:

Int c;

Void writeit(void);

};

13. Explain the concept about Single Inheritance with an example.

MATHEMATIC CENTER D96 MUNIRKA VILLAGE NEW DELHI 110067 & VIKAS PURI NEW DELHI CONTACT FOR COACHING MATHEMATICS FOR 11TH 12TH NDA DIPLOMA SSC CAT SAT CPT CONTACT FOR ADMISSION GUIDANCE B.TECH BBA BCA, MCA MBA DIPLOMA AND OTHER COURSES 09810144315

CONTACT FOR MATHEMATICS GROUP/HOME COACHING FOR CLASS 11TH 12TH BBA,BCA, DIPLOMA CPT CAT SSC AND OTHER EXAMS Also home tutors for other subjects in south and west delhi EMAIL:5 [email protected] web site www.mathematic.in

5

www.mathem

atic.in

09810144315

6

• Solution: Single Inheritance:

A derived class is defined by specifying its relationship with the base class in addition to its own details. The syntax of defining a derived class (using single inheritance) is given below:

Class derived-class-name: visibility-mode base-class name

{

//members of derived class

};

The base class(es) names follows the colon( : ). The names of all the base classes of a derived class follow and are separated by a comma. The visibility-mode may be either private or public or protected. If no visibility-mode is specified, then by default the visibility-mode is considered as private.

Following are some example of derived class definitions:

Class sub: Public Super // public derivation

{

//members of sub(the derived class)

};

Class sub: Private Super //private derivation

{

//members of sub

};

Class sub: protected Super //protected derivation

{

//members of sub

};

Class sub: Super // private derivation by default

{

};

MATHEMATIC CENTER D96 MUNIRKA VILLAGE NEW DELHI 110067 & VIKAS PURI NEW DELHI CONTACT FOR COACHING MATHEMATICS FOR 11TH 12TH NDA DIPLOMA SSC CAT SAT CPT CONTACT FOR ADMISSION GUIDANCE B.TECH BBA BCA, MCA MBA DIPLOMA AND OTHER COURSES 09810144315

CONTACT FOR MATHEMATICS GROUP/HOME COACHING FOR CLASS 11TH 12TH BBA,BCA, DIPLOMA CPT CAT SSC AND OTHER EXAMS Also home tutors for other subjects in south and west delhi EMAIL:6 [email protected] web site www.mathematic.in

6

www.mathem

atic.in

09810144315

7

In the above given definitions, sub is the derived class of Super (the base class). The colon indicates the sub class which is based upon the Super class. The visibility mode public indicates that Super is a public base class. Similarly, the visibility –modes private or protected indicate that super is a private base class or protected base class respectively.

Although, the private members of the base class cannot be accessed directly, yet the objects of the derived class are able to access them through the non-private inherited members.

14. Explain the concept about Multilevel Inheritance with an example.

• Solution: Sometimes, a derived class itself acts as a base class for another class. Such a situation is called multilevel inheritance. The chain of classes forming multilevel inheritance is known as inheritance hierarchy or inheritance path.

A derived class with multilevel inheritance is declared as follows:

Class A {………}; //base class

Class B: public A {……..}; //B derived from A

Class C: public B{………..}; //c derived from B

The multilevel inheritance implements the transitive nature of real life objects. There can be any number of classes in the inheritance hierarchy.

Let us consider a simple example. Assume that the test results of a batch of students are stored in three different classes. Class student stores the roll-number, class test stores, the marks obtained in two subjects and class result contains the total marks obtained in the test. The class result can inherit the details of the marks obtained in the test and the roll-number of students through multilevel inheritance. Example:

#include<iostream.h>

class student

{

protected:

int roll_number;

public:

void get_number(int);

void put_numbr(void);

};

void student::get_number(int a)

MATHEMATIC CENTER D96 MUNIRKA VILLAGE NEW DELHI 110067 & VIKAS PURI NEW DELHI CONTACT FOR COACHING MATHEMATICS FOR 11TH 12TH NDA DIPLOMA SSC CAT SAT CPT CONTACT FOR ADMISSION GUIDANCE B.TECH BBA BCA, MCA MBA DIPLOMA AND OTHER COURSES 09810144315

CONTACT FOR MATHEMATICS GROUP/HOME COACHING FOR CLASS 11TH 12TH BBA,BCA, DIPLOMA CPT CAT SSC AND OTHER EXAMS Also home tutors for other subjects in south and west delhi EMAIL:7 [email protected] web site www.mathematic.in

7

www.mathem

atic.in

09810144315

8

{

roll_number= a;

}

void student::put_number()

{

cout<<"roll number:"<<roll_number;

}

class test:public student //first level derivation

{

protected:

float sub1,sub2;

public:

void get_marks(float, float);

void put_marks(void);

};

void test::get_marks(float x,float y)

{

sub1=x;

sub2=y;

}

void test::put_marks()

{

cout<<"marks in sub1="<<sub1;

cout<<"marks in sub 2="<<sub2;

}

class result:public test

MATHEMATIC CENTER D96 MUNIRKA VILLAGE NEW DELHI 110067 & VIKAS PURI NEW DELHI CONTACT FOR COACHING MATHEMATICS FOR 11TH 12TH NDA DIPLOMA SSC CAT SAT CPT CONTACT FOR ADMISSION GUIDANCE B.TECH BBA BCA, MCA MBA DIPLOMA AND OTHER COURSES 09810144315

CONTACT FOR MATHEMATICS GROUP/HOME COACHING FOR CLASS 11TH 12TH BBA,BCA, DIPLOMA CPT CAT SSC AND OTHER EXAMS Also home tutors for other subjects in south and west delhi EMAIL:8 [email protected] web site www.mathematic.in

8

www.mathem

atic.in

09810144315

9

{

float total;

public:

void display(void);

};

void result::display(void)

{

total=sub1+sub2;

put_number();

put_marks();

cout<<"total="<<total;

}

int main ()

{

result student1;

student1.get_number(111);

student1.get_marks(75.0,59.5);

student1.display();

return 0;

}

15. Consider the following class declaration and answer the following questions.

i) Name the derived class and base class.

ii) List the data and functions inherited by the class Person.

Class Mydata

{

MATHEMATIC CENTER D96 MUNIRKA VILLAGE NEW DELHI 110067 & VIKAS PURI NEW DELHI CONTACT FOR COACHING MATHEMATICS FOR 11TH 12TH NDA DIPLOMA SSC CAT SAT CPT CONTACT FOR ADMISSION GUIDANCE B.TECH BBA BCA, MCA MBA DIPLOMA AND OTHER COURSES 09810144315

CONTACT FOR MATHEMATICS GROUP/HOME COACHING FOR CLASS 11TH 12TH BBA,BCA, DIPLOMA CPT CAT SSC AND OTHER EXAMS Also home tutors for other subjects in south and west delhi EMAIL:9 [email protected] web site www.mathematic.in

9

www.mathem

atic.in

09810144315

10

Protected:

int Data;

Public:

Void Get_Mydata(int);

Void Manip_data(int);

Void Show_data(int);

};

Class Mydata1{

Protected:

int Data1;

Public:

Void Get_Mydata1(int);

Void Show_data1(int);

};

Class Person:public Mydata,Mydata1

{

Public:

void Show_data(void);

};

• Solution: (i)Derived class-Person

Base classes-Mydata, MyData1

(ii)Inherited Data: All the data members of classes Data, Data1

Inherited functions: All the member function of classes Data and Data1

Get_Mydata, Show_data1()

But the private members of base classes are not directly accessible to the subclass Person.

MATHEMATIC CENTER D96 MUNIRKA VILLAGE NEW DELHI 110067 & VIKAS PURI NEW DELHI CONTACT FOR COACHING MATHEMATICS FOR 11TH 12TH NDA DIPLOMA SSC CAT SAT CPT CONTACT FOR ADMISSION GUIDANCE B.TECH BBA BCA, MCA MBA DIPLOMA AND OTHER COURSES 09810144315

CONTACT FOR MATHEMATICS GROUP/HOME COACHING FOR CLASS 11TH 12TH BBA,BCA, DIPLOMA CPT CAT SSC AND OTHER EXAMS Also home tutors for other subjects in south and west delhi EMAIL:10 [email protected] web site www.mathematic.in

10

www.mathem

atic.in

09810144315

11

16. Give the following declaration:

Class typeA

{ int x;

Public:

typeA(int l);

void showtypeA()

};

Class typeB : public typeA

{ float p,q;

Public:

typeB(float a, float b);

void showtypeB();

};

Class typeC: public typeA, private typeB

{

int u,v;

Public:

typeC(int a, int b);

void show typeC();

};

(i) List data members inherited by class typeC

(ii) Name the base class(es) of class typeB

(iii) Name the class(es) that can access showtypeB() declared in typeB.

• Solution:

(i) X has been inherited but it is not directly accessible by the derived class.

MATHEMATIC CENTER D96 MUNIRKA VILLAGE NEW DELHI 110067 & VIKAS PURI NEW DELHI CONTACT FOR COACHING MATHEMATICS FOR 11TH 12TH NDA DIPLOMA SSC CAT SAT CPT CONTACT FOR ADMISSION GUIDANCE B.TECH BBA BCA, MCA MBA DIPLOMA AND OTHER COURSES 09810144315

CONTACT FOR MATHEMATICS GROUP/HOME COACHING FOR CLASS 11TH 12TH BBA,BCA, DIPLOMA CPT CAT SSC AND OTHER EXAMS Also home tutors for other subjects in south and west delhi EMAIL:11 [email protected] web site www.mathematic.in

11

www.mathem

atic.in

09810144315

12

(ii) typeA

(iii) typeB and typeC

17. How does inheritance influence the working of constructors and destructors?

• Solution: Inheritance means a class(derived class) inherits properties from another class(the base class). When an object of the derived class is declared, in order to create it, firstly, the constructor of the base class is invoked and then, the constructor of the derived class is invoked.

On the other hand - when an object of the derived class is destroyed, first, the destructor of the derived class is invoked followed by destructor of the base class. That is, the destructors are invoked in the reverse order of the constructor invocation.

18. Given the following set of definitions.

Class X{ . . .};

Class Y:public X

{ . . .};

Class Z:public Y

{ . . .};

Z O1;

In what order will the constructors and destructors be invoked?

• Solution: The order of constructor invocation will be

X::X(), Y::Y() and Z::Z()

The order of destructor invocation will be reverse of the constructor invocation order

i.e.,

Z::~Z(), Y::~Y(), X::~X()

MATHEMATIC CENTER D96 MUNIRKA VILLAGE NEW DELHI 110067 & VIKAS PURI NEW DELHI CONTACT FOR COACHING MATHEMATICS FOR 11TH 12TH NDA DIPLOMA SSC CAT SAT CPT CONTACT FOR ADMISSION GUIDANCE B.TECH BBA BCA, MCA MBA DIPLOMA AND OTHER COURSES 09810144315

CONTACT FOR MATHEMATICS GROUP/HOME COACHING FOR CLASS 11TH 12TH BBA,BCA, DIPLOMA CPT CAT SSC AND OTHER EXAMS Also home tutors for other subjects in south and west delhi EMAIL:12 [email protected] web site www.mathematic.in

12

www.mathem

atic.in

09810144315

13

19. Define the constructors for the following definitions.

Class A { int x;

Float y;

Public:

A(int,float); . . .};

Class B : public A

{

Public: . . .};

• Solution: A::A(int i,float j)

{

X=i;

Y=j;

}

B::B(int p, float j):A(p,q)

{

Cout<<"B’s constructor";

}

20. Given the following set of definitions, what data members and functions are directly

accessible by the functions mentioned here?

void inform (void);

class X

MATHEMATIC CENTER D96 MUNIRKA VILLAGE NEW DELHI 110067 & VIKAS PURI NEW DELHI CONTACT FOR COACHING MATHEMATICS FOR 11TH 12TH NDA DIPLOMA SSC CAT SAT CPT CONTACT FOR ADMISSION GUIDANCE B.TECH BBA BCA, MCA MBA DIPLOMA AND OTHER COURSES 09810144315

CONTACT FOR MATHEMATICS GROUP/HOME COACHING FOR CLASS 11TH 12TH BBA,BCA, DIPLOMA CPT CAT SSC AND OTHER EXAMS Also home tutors for other subjects in south and west delhi EMAIL:13 [email protected] web site www.mathematic.in

13

www.mathem

atic.in

09810144315

14

{

int a;

float b;

void init(void);

public:

char ch;

void get(void);

protected:

double amt;

void getamt(void);

friend void A(void);

};

Class Y: public X

{

int x;

Public:

int j;

Void readit(void);

Protected:

Void inform(void);

Friend void B(void);

};

Void display(void);

• Solution: The function ::inform() can access public data members of classes X and Y and the function display() provided, the definition of ::inform() occurs after the declaration of X, Y and display().

X:: init(), X::get(), X:: getamt() and A() can access all the members of X and ::inform.().

MATHEMATIC CENTER D96 MUNIRKA VILLAGE NEW DELHI 110067 & VIKAS PURI NEW DELHI CONTACT FOR COACHING MATHEMATICS FOR 11TH 12TH NDA DIPLOMA SSC CAT SAT CPT CONTACT FOR ADMISSION GUIDANCE B.TECH BBA BCA, MCA MBA DIPLOMA AND OTHER COURSES 09810144315

CONTACT FOR MATHEMATICS GROUP/HOME COACHING FOR CLASS 11TH 12TH BBA,BCA, DIPLOMA CPT CAT SSC AND OTHER EXAMS Also home tutors for other subjects in south and west delhi EMAIL:14 [email protected] web site www.mathematic.in

14

www.mathem

atic.in

09810144315

15

Y::readit, Y::inform() and B() can access all the members of Y, public and protected members of X(the inherited members) and ::inform()(only with the scope resolution operator:: ).

::display() can access public members of X and Y, :: inform(), and A() and B() if only their declarations A( )’s and B( )’s occur before display( ).

21. Find errors in the following program. State your reasons.

#include<iostream.h>

Class A

{

int a1;

public:

int a2;

protected:

Int a3;

};

Class B : public A

{

public:

void func()

{

int b1,b2,b3;

b1=a1;

b2=a2;

b3=a3;

}

};

Class C : A

MATHEMATIC CENTER D96 MUNIRKA VILLAGE NEW DELHI 110067 & VIKAS PURI NEW DELHI CONTACT FOR COACHING MATHEMATICS FOR 11TH 12TH NDA DIPLOMA SSC CAT SAT CPT CONTACT FOR ADMISSION GUIDANCE B.TECH BBA BCA, MCA MBA DIPLOMA AND OTHER COURSES 09810144315

CONTACT FOR MATHEMATICS GROUP/HOME COACHING FOR CLASS 11TH 12TH BBA,BCA, DIPLOMA CPT CAT SSC AND OTHER EXAMS Also home tutors for other subjects in south and west delhi EMAIL:15 [email protected] web site www.mathematic.in

15

www.mathem

atic.in

09810144315

16

{

Public:

Void f()

{

int c1,c2,c3;

c1=a1;

c2=a2;

c3=a3;

}

};

int main()

{

int p,q,r,i,j,k;

B O1;

C O2;

P=O1.a1;

q=O1.a2;

r=O1.a3;

i=O2.a1;

j=O2.a2;

k=O2.a3;

return 0;

}

• Solution: The errors in the above given program are as described below:

1. B::func() cannot access A::a1 as a1 is a private member of A. Therefore,

b1=a1;

MATHEMATIC CENTER D96 MUNIRKA VILLAGE NEW DELHI 110067 & VIKAS PURI NEW DELHI CONTACT FOR COACHING MATHEMATICS FOR 11TH 12TH NDA DIPLOMA SSC CAT SAT CPT CONTACT FOR ADMISSION GUIDANCE B.TECH BBA BCA, MCA MBA DIPLOMA AND OTHER COURSES 09810144315

CONTACT FOR MATHEMATICS GROUP/HOME COACHING FOR CLASS 11TH 12TH BBA,BCA, DIPLOMA CPT CAT SSC AND OTHER EXAMS Also home tutors for other subjects in south and west delhi EMAIL:16 [email protected] web site www.mathematic.in

16

www.mathem

atic.in

09810144315

17

is illegal.

2. C::f() cannot access A::a1 as a1 is a private number of A. Therefore,

C1=a1;

is illegal.

3. In main(), the statement P=O1.a1; is illegal because a1 is not the public member of B and hence cannot be accessed directly by its objects.

4. In main(), the statement r=O1.a3; is illegal for the same reason as specified in point 3.

5. The statements

i=O2.a1;

f=O2.a2;

k=O2.a3;

are illegal as neither of a1,a2 and a3 are public members of C and hence cannot be accessed directly by the objects of the C class.

22. List out some rules for Virtual function.

• Solution: 1. The virtual functions must be members of some class.

2. They cannot be static members.

3. They are accessed by using object pointers.

4. A virtual function can be a friend of another class.

5. A virtual function in a base class must be defined, even though it may not be used.

6. The prototypes of the base class version of a `virtual function and all the derived class versions must be identical. If two functions with the same name have different prototypes++ considers them as overloaded functions, and the virtual function mechanism is ignored.

7. We cannot have virtual constructors, but we can have virtual destructors.

8. While a base pointer can point to any type of the derived object, the reverse is not true. That’s to say, we cannot use the pointer to a derived class to access an object of the base type.

9. When a base pointer points to the derived class, incrementing or decrementing it will not make it to point to the next object of the derived class. It is incremented or decremented only relative to its type. Therefore, we should not use this method to move the pointer to the next object.

10. If a virtual function is defined in the base class, it need not be necessarily redefined in the derived class. In such cases, calls will invoke the base function.

MATHEMATIC CENTER D96 MUNIRKA VILLAGE NEW DELHI 110067 & VIKAS PURI NEW DELHI CONTACT FOR COACHING MATHEMATICS FOR 11TH 12TH NDA DIPLOMA SSC CAT SAT CPT CONTACT FOR ADMISSION GUIDANCE B.TECH BBA BCA, MCA MBA DIPLOMA AND OTHER COURSES 09810144315

CONTACT FOR MATHEMATICS GROUP/HOME COACHING FOR CLASS 11TH 12TH BBA,BCA, DIPLOMA CPT CAT SSC AND OTHER EXAMS Also home tutors for other subjects in south and west delhi EMAIL:17 [email protected] web site www.mathematic.in

17

www.mathem

atic.in

09810144315

18

23. Explain Multiple Inheritance with an example. When does ambiguity arise in multiple

Inheritances? How can one resolve it?

• Solution: Multiple inheritance means deriving a class from more than one base class. With multiple inheritances, the new class inherits all the protected and public members of all the base classes. Multiple inheritances allow a derived class to combine the strengths of other classes.

Class X

{

Int a;

Void fx1 ();

Protected:

Char b;

Void fx2 ();

Public:

Float c;

Void fx3 ();

};

// definitions of X’s member functions

Class Y

{

Int d;

Void fy1 ();

Protected:

Char e;

Void fy2 ();

Public:

Float f;

Void fy3 ();

MATHEMATIC CENTER D96 MUNIRKA VILLAGE NEW DELHI 110067 & VIKAS PURI NEW DELHI CONTACT FOR COACHING MATHEMATICS FOR 11TH 12TH NDA DIPLOMA SSC CAT SAT CPT CONTACT FOR ADMISSION GUIDANCE B.TECH BBA BCA, MCA MBA DIPLOMA AND OTHER COURSES 09810144315

CONTACT FOR MATHEMATICS GROUP/HOME COACHING FOR CLASS 11TH 12TH BBA,BCA, DIPLOMA CPT CAT SSC AND OTHER EXAMS Also home tutors for other subjects in south and west delhi EMAIL:18 [email protected] web site www.mathematic.in

18

www.mathem

atic.in

09810144315

19

};

//definition of Y’s member functions

Class Z: public X, public Y

{

Int g;

Protected:

Int h;

Public:

Void display();

};

Definition of Z::display ()

Note: When base classes are publicly inherited, protected and public members retain their access specifiers (i.e.). They remain protected and public respectively in derived class. Now, if we inherit Z from X and Y base classes as:

Class Z:privateX,protected Y

{

//same as before

};

See now ,the members inherited from class X are private in class Z as X has been privately inherited by Z and the members inherited from class Y are protected in class Z as Y has been protectively inherited by Z.

Virtual Base classes:

An element ambiguity can be introduced into a C++ program when multiple base classes are inherited. For example, consider this incorrect program:

#include<iostream.h>

Class Base

{

Public:

Inta;

MATHEMATIC CENTER D96 MUNIRKA VILLAGE NEW DELHI 110067 & VIKAS PURI NEW DELHI CONTACT FOR COACHING MATHEMATICS FOR 11TH 12TH NDA DIPLOMA SSC CAT SAT CPT CONTACT FOR ADMISSION GUIDANCE B.TECH BBA BCA, MCA MBA DIPLOMA AND OTHER COURSES 09810144315

CONTACT FOR MATHEMATICS GROUP/HOME COACHING FOR CLASS 11TH 12TH BBA,BCA, DIPLOMA CPT CAT SSC AND OTHER EXAMS Also home tutors for other subjects in south and west delhi EMAIL:19 [email protected] web site www.mathematic.in

19

www.mathem

atic.in

09810144315

20

};

ClassD1:public Base

{

Public:

Int b;

};

Class D2: public Base

{

Public:

Int c;

}

Class D3:publicD1,public D2

{

Public:

Int total;

};

Main()

{

D3 ob;

Ob.a=25; //this is ambiguous, which a? since a is in D2 as well as in D3,and D3 inherits from both.

Ob.b=50;

Ob.c=75;

Ob.total=ob.a+ob.b+ob.c; //a is ambiguous

Cout<<ob.a<<"\t"<<ob.b<<"\t"<<ob.c<<"\t"<<ob.total<<"\n";

Return 0;

}

MATHEMATIC CENTER D96 MUNIRKA VILLAGE NEW DELHI 110067 & VIKAS PURI NEW DELHI CONTACT FOR COACHING MATHEMATICS FOR 11TH 12TH NDA DIPLOMA SSC CAT SAT CPT CONTACT FOR ADMISSION GUIDANCE B.TECH BBA BCA, MCA MBA DIPLOMA AND OTHER COURSES 09810144315

CONTACT FOR MATHEMATICS GROUP/HOME COACHING FOR CLASS 11TH 12TH BBA,BCA, DIPLOMA CPT CAT SSC AND OTHER EXAMS Also home tutors for other subjects in south and west delhi EMAIL:20 [email protected] web site www.mathematic.in

20

www.mathem

atic.in

09810144315

21

As both D1 and D2 inherit the base, they both have copies of base. However D3 inherits both D1 and D2. This means that there are two copies of Base present in an object of type D3. Therefore,

In an expression like:

Ob.a=25;

Which a is being referred to the one in D1 or the one in D2?

There are two copies of base present in the object ob, there are two ob.a1. Thus, the statement is inherently ambiguous.

There are two ways to remedy the preceding program. The first is to apply the scope resolution operator( :: ) to a and manually select one a. For example, if we use the following main() function .rest of the program remains the same.

Int main ()

{

D3 ob;

Ob.D1::a=25; //scope resolved D1’s a.

Ob.b=50;

Ob.c=75;

Ob.total=ob.a+ob.b+ob.c;

Cout<<ob.a<<"\t"<<ob.b<<"\t"<<ob.c<<"\t"<<ob.total<<"\n";

Return 0;

}

As you can see , because the :: operator was applied. The program has manually selected D1’s version of base. When two or more objects are derived from a common base class, you can prevent multiple copies of base class from being present in an object derived from those objects by declaring the base class as virtual when it is inherited. This is accomplished by putting the key word virtual before the base class name when it is inherited. For example, here is another version of the example program in which D3 contains only one copy of base.

#include<iostream.h>

Class Base

{

Public:

Inta;

MATHEMATIC CENTER D96 MUNIRKA VILLAGE NEW DELHI 110067 & VIKAS PURI NEW DELHI CONTACT FOR COACHING MATHEMATICS FOR 11TH 12TH NDA DIPLOMA SSC CAT SAT CPT CONTACT FOR ADMISSION GUIDANCE B.TECH BBA BCA, MCA MBA DIPLOMA AND OTHER COURSES 09810144315

CONTACT FOR MATHEMATICS GROUP/HOME COACHING FOR CLASS 11TH 12TH BBA,BCA, DIPLOMA CPT CAT SSC AND OTHER EXAMS Also home tutors for other subjects in south and west delhi EMAIL:21 [email protected] web site www.mathematic.in

21

www.mathem

atic.in

09810144315

22

};

ClassD1: virtual public Base

{

Public:

Int b;

};

Class D2: virtual public Base

{

Public:

Int c;

}

Class D3:publicD1, public D2

{

Public:

Int total;

};

Int main ()

{

D3 ob;

Ob.a=25;

Ob.b=50;

Ob.c=75;

Ob.total=ob.a+ob.b+ob.c;

Cout<<ob.a<<"\t"<<ob.b<<"\t"<<ob.c<<"\t"<<ob.total<<"\n";

Return 0;

}

MATHEMATIC CENTER D96 MUNIRKA VILLAGE NEW DELHI 110067 & VIKAS PURI NEW DELHI CONTACT FOR COACHING MATHEMATICS FOR 11TH 12TH NDA DIPLOMA SSC CAT SAT CPT CONTACT FOR ADMISSION GUIDANCE B.TECH BBA BCA, MCA MBA DIPLOMA AND OTHER COURSES 09810144315

CONTACT FOR MATHEMATICS GROUP/HOME COACHING FOR CLASS 11TH 12TH BBA,BCA, DIPLOMA CPT CAT SSC AND OTHER EXAMS Also home tutors for other subjects in south and west delhi EMAIL:22 [email protected] web site www.mathematic.in

22

www.mathem

atic.in

09810144315

23

24. Consider the following and answer the questions given below:

Class MNC

{

Char Cname[25]; //company name

Protected :

Char Hoffice[25]; // Head office

Public:

MNC();

Char Country[25];

Void EnterData();

Void DisplayData();

};

Class Branch : public MNC

{

Long NOE;

Char Ctry[25];

Protected:

Void Association();

Public :

Branch();

Void Add();

Void Show();

};

Class Outlet : public Branch

{

MATHEMATIC CENTER D96 MUNIRKA VILLAGE NEW DELHI 110067 & VIKAS PURI NEW DELHI CONTACT FOR COACHING MATHEMATICS FOR 11TH 12TH NDA DIPLOMA SSC CAT SAT CPT CONTACT FOR ADMISSION GUIDANCE B.TECH BBA BCA, MCA MBA DIPLOMA AND OTHER COURSES 09810144315

CONTACT FOR MATHEMATICS GROUP/HOME COACHING FOR CLASS 11TH 12TH BBA,BCA, DIPLOMA CPT CAT SSC AND OTHER EXAMS Also home tutors for other subjects in south and west delhi EMAIL:23 [email protected] web site www.mathematic.in

23

www.mathem

atic.in

09810144315

24

Char State[25];

Public:

Outlet();

Void Enter();

Void Output();

};

(i) Which class constructor will be called first at the time of declaration of an object of class Outlet ?

(ii) How many bytes does an object belonging to class Outlet require ?

(iii) Name the member function(s), which are accessed from the object(s) of class Outlet.

Name the data member(s), which are accessible from the object(s) of class Branch.

• Solution: (i) First of all constructor of class MNC will be called, then of Branch and then at last of Outlet.

(ii) 129

(iii) MNC :: EnterData(), MNC :: DisplayData(), Branch :: Add(), Branch :: show(), Outlet :: Enter(), Outlet :: Output().

MNC :: Country

25. Explain the concept behind Pure Virtual Function and nesting of classes.

• Solution: It is normal practice to declare a function virtual inside the base class and redefine it in derived classes. The function inside the base class is seldom used for performing any task. It only serves as a placeholder. Such functions are called "do-nothing" functions.

A do-nothing function may be defined as follows:

Virtual void display()= 0;

Such functions are called pure virtual functions. A pure virtual function is a function declared in a base class that has no definition relative to the base class. The compiler requires each derived class to either define the function or redeclare it as a pure virtual function. The class containing pure virtual cannot be used to declare objects of its own. Such classes are called abstract base classes. The main objective of an abstract base class is to provide some traits to the derived classes and to create a base pointer required for achieving run time polymorphism.

Nesting of classes

MATHEMATIC CENTER D96 MUNIRKA VILLAGE NEW DELHI 110067 & VIKAS PURI NEW DELHI CONTACT FOR COACHING MATHEMATICS FOR 11TH 12TH NDA DIPLOMA SSC CAT SAT CPT CONTACT FOR ADMISSION GUIDANCE B.TECH BBA BCA, MCA MBA DIPLOMA AND OTHER COURSES 09810144315

CONTACT FOR MATHEMATICS GROUP/HOME COACHING FOR CLASS 11TH 12TH BBA,BCA, DIPLOMA CPT CAT SSC AND OTHER EXAMS Also home tutors for other subjects in south and west delhi EMAIL:24 [email protected] web site www.mathematic.in

24

www.mathem

atic.in

09810144315

25

A class derives certain properties of another class through inheritance. One class defined within another class is called nested class. Other way of doing so is to the class that contains objects of other classes as its members, as shown below:

Class A

{

};

Class B

{

};

Class C

{A a;

B b;

};

MATHEMATIC CENTER D96 MUNIRKA VILLAGE NEW DELHI 110067 & VIKAS PURI NEW DELHI CONTACT FOR COACHING MATHEMATICS FOR 11TH 12TH NDA DIPLOMA SSC CAT SAT CPT CONTACT FOR ADMISSION GUIDANCE B.TECH BBA BCA, MCA MBA DIPLOMA AND OTHER COURSES 09810144315

CONTACT FOR MATHEMATICS GROUP/HOME COACHING FOR CLASS 11TH 12TH BBA,BCA, DIPLOMA CPT CAT SSC AND OTHER EXAMS Also home tutors for other subjects in south and west delhi EMAIL:25 [email protected] web site www.mathematic.in

25

www.mathem

atic.in

09810144315