Assg3_OOP_solution (1)

Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

Object Oriented Programming (C++) SY BCA SEM – III

Objective Questions (2 marks)


1. What is the use of protected modifier ?
A member declared as protected is accessible by the member functions within its class and
any class immediately derived from it.
It cannot be accessed by the functions outside these two classes.
When a protected member is inherited in private mode, it becomes private in derived class.
When a protected member is inherited in public mode, it becomes protected in derived class.
 When a base class is inherited in protected mode, both public and protected members of
the base class become protected members in derived class. (Prof. Viral S. Patel)

2. What do you mean by method overriding ?


In the concept of inheritance, it is the redefinition of base class function in derived class with
same signature (same name and same parameters).

Example:

class BC void main()


{ {
public : void disp() { cout<< VSP ; } DC ob;
}; clrscr();
class DC : public BC ob.disp();
{ getch();
public : void disp() { cout<< HELLO ; } }
}; Output : HELLO

3. What is abstract class ?


A class containing pure virtual function is called abstract class.
We cannot create object of abstract class.
Each derived class of abstract class either define the function or redeclare it as a pure virtual
function.
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. (Prof. Viral S. Patel)

Example :

class A Here,
{ virtual void disp() = 0 is pure virtual function .
public : virtual void disp() = 0; And so class A is abstract class.
};
class B : public A A obj; // give error as A is abstract class
{
public : void disp() { cout<< HELLO ; }
};

4. Explain nested class.


A class which is declared in another class is called nested class.
A nested class is a member of outer class and so same access rights as any other member.

Example :
Object Oriented Programming (C++) SY BCA SEM – III

class A void main()


{ {
public : class B A::B obj;
{ obj.disp();
public : void disp() getch();
{ }
cout<< VSP ; Output : VSP
}
};
};

5. What does this pointer point do ?


(or What is the use of this pointer ?)
this pointer points to an object that invokes a member function.
this pointer automatically passed to a member function as implicit argument when it is
called. (Prof. Viral S. Patel)

Example :

class Person void main()


{ {
int age; Person p1(25),p2(23);
public : Person max;
Person() { }
Person(int a) max = p1.greater(p2);
{ // greater fun. return object p1 using this pointer
age = a; // this->age = a
} max.disp();
Person & greater(Person & x) getch();
{ }
if x.age > age
return x; // refer to p2 Output : 25
else
return *this; // refer to p1
}
void disp() { cout<<age; }
};

6. What is dynamic binding (or run time polymorphism or late binding) ? Give advantage of it.
Dynamic binding also called as run time polymorphism or late binding.
In run time polymorphism function binding occurs at runtime.
Run time polymorphism can achieved using virtual function and base class pointer.
When we use the same function name in both the base and derived classes and the function
in base class is declared as virtual then C++ determines which function to use at runtime based
on the type of object pointed to by the base class pointer, rather than the type of the pointer.
Advantages : Ability to refer to objects of different class using single pointer. And so
functions of different class can be called at runtime as required. (Prof. Viral S. Patel)

7. What is early binding (or static binding or compile time polymorphism) ?


Early binding also called as compile time polymorphism or static binding.
In compile time polymorphism function binding occurs at compile time.
Object Oriented Programming (C++) SY BCA SEM – III

Function overloading and operator overloading are examples of early binding.


In the case of overriding and base class pointer, compiler does not check type of object. Here
c++ compiler binds the function at compile time according to type of pointer.

8. What is the application of scope resolution operator (: :) in C++ ?


(How scope resolution operator used in different ways in object oriented programming ?
Scope resolution operator (: :) is used to define a member function outside a class.
It is also used to indicate global variable in the case of same name of local variable.
It is also used to refer base class in the concept of inheritance to access base class members
in child class.
It is also used to access class s static variables or functions. (Prof. Viral S. Patel)

9. How the constructor is called in multilevel and multiple inheritance ?


In multilevel inheritance parent class constructor is called first then child class constructor is
called. So constructors are called in the order of inheritance (top to bottom)(base to derived).
In multiple inheritance, the base classes are constructed in the order in which they appear in
the declaration of the derived class.
In multiple inheritance if virtual base classes are present then they invoked before any non-
virtual base classes. If there are multiple virtual base classes, they are invoked in the order in
which they are declared.

10. What is containership ?


If class contain objects of other classes as its members then this kind of relationship is called
containership.
Example :
class alpha class gama
{ {
.... .........
}; alpha a; // a is object of alpha
class beta beta b; // b is object of beta
{ public : gamma(arglist) : a(arglist1), b(arglist2) //a & b call their constructors
... {
} //constructor body
}
};

11. What is pure virtual function ? When it is required ? (Prof. Viral S. Patel)
 A pure virtual function is a function declared in a base class that has no definition relative to
the base class. It only serves as a placeholder. Such a function is called do-nothing function.
For example ,
virtual void display() = 0;
In such cases, the compiler requires each derived class to either define the function or
redeclare it as a pure virtual function.
The class contain pure virtual function is called abstract class.
 Sometimes implementation of all function cannot be provided in base class because we
don t know the implementation. Only derived class requires to define it. In such a case we have
to required pure virtual function to create abstract class. And nobody can create of abstract
class.

12. Define Hybrid Inheritance.


Object Oriented Programming (C++) SY BCA SEM – III

There could be situation where we need to apply two or more types of inheritance to design
program. For example,
Here, we can see Hybrid Inheritance
combination of
multilevel inheritance
(Student  Test  Result )
and
multiple inheritance
(Test  Result  Sports)

13. List types of inheritance.

14. Explain visibility modes. (Prof. Viral S. Patel)

class inherited modes


Public derivation Private derivation Protected derivation
Private members Not inherited Not inherited Not inherited
Protected members Protected Private Protected
Public members Public Private Protected

15. What is the syntax of derived constructor ?


Derived-constructor (arg1, arg2, ... ,argN,argD) : base1(arg1), base2(arg2), .... , base(argN)
{
Body of derived constructor use argD
}
Example :
D (int a1, int a2, int a3) : A(a1), B(a2) // A(a1) & B(a2) call to class A & B s constructors
{
d = a3;
}

Long Questions :
1. What is inheritance ? What is multiple and multilevel inheritance ? Explain with example.
The mechanism of deriving a new class from an old one is called inheritance. It strongly
support the concept of reusability. New class reusing the properties of existing class.
Object Oriented Programming (C++) SY BCA SEM – III

The old class is referred to as base class or parent class and new class is referred to as child
class or derived class or subclass.
Multilevel Inheritance :
Suppose class A is inherited by class B and class B inherited by class C. This type of concept is
called multilevel inheritance. More than one level inheritance is called multilevel inheritance.
For example :
class A { ............... }; // Base class A
class B : public A { .............. }; // B derived from A
class C : public B { ............. }; // C derived from B

Class A Class B : public A Class C : public B Void main()


{ { { {
public : public : public : C obC;
void funA() void funB() void funC() obC.funC();
{ { { obC.funB();
cout<< A ; cout<< B ; cout<< C ; obC.funA();
} } } }
} } } Output : CBA

Here funA inherited in class B so class B have 2 functions funA() and funB(),
fun B inherited in class C So class C have 3 functions funA(), funB() and funC()

This process can be extended to any number of levels. (Prof. Viral S. Patel)

Multiple Inheritance :
A class can inherited the attributes of two or more classes is known as multiple inheritance.
Multiple inheritance allows us to combine the features of several existing classes.
For example :
class A { .......... };
class B { .......... };
class D : public A, public B
{
......
}

some times we may face a problem in using the multiple inheritance, when a function with
same name appears in more than one base class. This ambiguity (overriding problem) can be
solved by scope resolution operator (: :).
Example :
Class A Class B Class C : public A, public B Void main()
{ { { {
public : public : public : C obC;
void disp() void disp() void disp() obC.disp();
{ { { obC.A::disp();
cout<< A ; cout<< B ; cout<< C ; obC.B::disp();
} } } }
} } } Output : CAB

2. What is ambiguity in hybrid inheritance ? How ambiguity remove from compile time ? Explain
with example. (7)
( or Explain ambiguity for hybrid inheritance with example )
Object Oriented Programming (C++) SY BCA SEM – III

 For Hybrid inheritance if the child class has two base class parent1 and parent2 which
themselves have a common base class grandparent . For such a case the child inherits the
traits of grandparent via two separate paths as shown in fig.

Here all the public and protected members of grandparent are inherited into child twice,
first via parent1 and again via parent2 . This situation create duplicate sets of members
inherited from grandparent . This introduces ambiguity and should be avoided.
The duplication of inherited members due to multiple paths can be avoided by making grand
parent class virtual while it is inherited and declaring parents classes. (Prof. Viral S. Patel)

For example :

class A // grand parent


{
....
};
class B1 : virtual public A // parent1
{
....
};
class B2 : public virtual A // parent2
{
....
};
class C : public B1, public B2 // child
{
....
};

 When a class is made a virtual base class, C++ takes necessary care to see that only one copy
of that class is inherited, regardless of how many inheritance paths exist between the virtual
base class and a derived class.

3. Explain pointer to object and pointer to members of a class with example. (5)
Pointer to object is useful in creating objects at run time. We can also use an object pointer
to access the public members of an object.

Example :
Object Oriented Programming (C++) SY BCA SEM – III

class item item x;


{ item *ptr = &x;
int code;
int price; x.getdata(100,75); equivalent to
ptr->getdata(100,75);
public :
x.show(); equivalent to
void getdata(int a, int b) ptr->show();
{
code = a; price = b; we can also use following method
} (*ptr).show();
void show()
{ item *ptr = new item;
cout<<code<< <<price; create object using pointer at run time.
}
We can also create an array of objects
}; item *ptr = new item[10];

It is possible to take the address of a member of a class and assign it to a pointer. A class
member pointer can be declared using the operator ::* with the class name. (Prof. Viral S. Patel)
For example :
class A int A ::* ip = &A::m; // pointer ip to member m
{ int *ip = &m; // invalid statement
private :
int m; A a; // a is object of A then
public : cout<<a.*ip; // display m
void getm(int x) cout<<a.m; // same as above
{
m = x; int *ap = &a; // ap is pointer to object a then
} cout << ap->*ip; // display m
void show() cout << ap->m; // same as above
{
cout<<m; void (A::*pf)(int) = &A::getm; // pointer pf to member function getm
} (a.*pf) (10); // call to getm function
}; (ap->*pf)(10) // same as above but using object pointer ap

4. What is containership ? How does it differ from inheritance ? Explain with example. (7)
If class contain objects of other classes as its members then this kind of relationship is called
containership.
Constructors of all the member objects should be called before outer class constructor body.
Example :
class alpha class gama
{ {
.... .........
}; alpha a; // a is object of alpha
class beta beta b; // b is object of beta
{ public : gamma(arglist) : a(arglist1), b(arglist2) //a & b call their constructors
... {
}; //constructor body
}
};
Object Oriented Programming (C++) SY BCA SEM – III

Inheritance is the ability for a class to inherit properties and extend behaviour of a parent
class by extending it.
On the other hand, containership is the ability of class to contain objects of different classes
like composition.
 Inheritance represents an is-a relationship. Means that B is also A, if class A is parent and
class B is child of it.
Containership represents a has-a relationship. Means that A is composed of B (A has a B), if
class A contain object of class B.
Inheritance contain override functionality. Containership does not contain override concept.
In inheritance we can see specialization concept like Person class could be extended to create
the Employee class. We can also see Generalization.
In containership there is no specialization & generalization concept. But we can see
aggregation concept.
In Inheritance if we change any behaviour of parent class it automatically affect all child
classes.
Containership does not have ability to change or add behaviour as class is in another class.

5. How do the properties of following two derived class differ (7) (Prof. Viral S. Patel)
(i) class D1 : private B, public C {............};
(ii) class D2 : protected B, private C {...........};

class B class B is privately inherited in class D1 so all public and


{ protected member of class B become private in class D1.
..... Private member of class B is not inherited.
}
class C class C is publicly inherited in class D1 so protected member
{ of class C become protected in class D1 and public member of
..... class C become public in class D1. Private member of class C is
} not inherited.
class D1 : private B, public C
{ Class B is protectedly inherited in class D2 so all public and
..... protected member of class B is become protected in class D2.
} Private member of class B is not inherited.
class D2 : protected B, private C
{ class C is privately inherited in class D2 so all protected and
..... public member of class C become private in class D2. Private
} member of class C is not inherited.

Class D1 can inherited member of class C. And we can access


directly outside the class.
But Class D2 cannot inherited member of class C.

Class D1 cannot inherited member of class B.


But class D2 can inherited member of class B. But no body
can access this inherited member from outside the class as
they are protected.

6. Differentiate between multilevel and multiple Inheritance (7)


Object Oriented Programming (C++) SY BCA SEM – III

Multilevel Multiple
In C++ programming, not only we can derive a In C++ programming, a class can be derived
class from the base class but we can also from more than one parent classes.
derive a class from the derived class. This form
of inheritance is known as multilevel
inheritance.
For example : For example :
class A { ............... }; class A { .......... };
class B : public A { .............. }; class B { .......... };
class C : public B { ............. }; class D : public A, public B
{
......
}
Multilevel inheritance is supported by OOPs Multiple inheritance is supported by C++ but
languages not by JAVA and C#. JAVA and C# uses
interfaces concept. (Prof. Viral S. Patel)
Example with Explanation : Example with Explanation :
Class A Class A
{ {
public : public :
void funA() void disp()
{ {
cout<< A ; cout<< A ;
} }
} }
Class B : public A Class B
{ {
public : public :
void funB() void disp()
{ {
cout<< B ; cout<< B ;
} }
} }
Class C : public B Class C : public A, public B
{ {
public : public :
void funC() void disp()
{ {
cout<< C ; cout<< C ;
} }
} }
Void main() Void main()
{ {
C obC; C obC;
obC.funC(); obC.disp();
obC.funB(); obC.A::disp();
obC.funA(); obC.B::disp();
} }
Output : CBA Output : CAB

Here funA inherited in class B so class B have 2 some times we may face a problem in using
Object Oriented Programming (C++) SY BCA SEM – III

functions funA() and funB(), the multiple inheritance, when a function


fun B inherited in class C So class C have 3 with same name appears in more than one
functions funA(), funB() and funC() base class. This ambiguity (overriding
problem) can be solved by scope resolution
This process can be extended to any number operator (: :).
of levels. (Prof. Viral S. Patel)

7. What is visibility modifier ? List them. Differentiate them with proper example. (7)
Data hiding is one of the important features of object oriented programming .
The access restriction to the class members is specified by the labels public,
private, and protected within the class body. These keywords are known as visibility modifiers.
When label is not given by default, all the members are private.
Private members Protected members Public members
Only visible to member Visible to member functions Visible to all.
functions of same class. of same class and derived
class

Class can also have visibility label when it is inherited in another class. It is called class
inherited modes. Following table show visibility of members of parent class in child class when
class is inherited in public, private or protected mode. (Prof. Viral S. Patel)
class inherited modes
Public derivation Private derivation Protected derivation
Private members Not inherited Not inherited Not inherited
Protected members Protected Private Protected
Public members Public Private Protected

In concept of inheritance private member of parent class is never inherited in any mode.
If class is inherited in public mode the protected members become protected and public
members become public in child class.
If class is inherited in protected mode the protected and public members both become
protected in child class.
If class is inherited in private mode the protected and public members both become private
in child class.

Example :
Object Oriented Programming (C++) SY BCA SEM – III

8. How polymorphism achieved at runtime and compile time give example of runtime ? (6)
Compile time polymorphism achieved by function or operator overloading. And runtime
polymorphism achieved by virtual function and pointer.
 Runtime polymorphism : When we use the same function name in both the base and derived
classes and the function in base class is declared as virtual then C++ determines which function
to use at runtime based on the type of object pointed to by the base class pointer, rather than
the type of the pointer. (Prof. Viral S. Patel)
Example :
class Base class Derived : public Base void main()
{ { {
public : public : Base *bptr; // base class pointer
void disp() void disp() Derived obD; // derived class object
{ {
cout<< HELLO ; cout<< HI ; bptr = &obD; // object given to pointer
} }
virtual void show() void show() bptr->disp();
{ { // call base class method & O/P HELLO
Cout<< VSP ; Cout<< Prof ; bptr->show();
} } //call derived class method & O/P Prof
}; }; }

Here bptr is made to point to the object obD, the bptr->disp() calls only the function associated
with the Base class, whereas the statement bptr->show() calls the Derived class function show()
because the function show( ) is virtual in the Base class.

9. Explain virtual base class giving example. (5)


[*note : see answer of Q-2 above for answer. ]

10. Explain this pointer giving example. (5)


this pointer points to an object that invokes a member function.
this pointer automatically passed to a member function as implicit argument when it is
called. (Prof. Viral S. Patel)

Example :

class Person void main()


{ {
int age; Person p1(25),p2(23);
public : Person max;
Person() { }
Person(int a) max = p1.greater(p2);
{ // greater fun. return object p1 using this pointer
age = a; // this->age = a
} max.disp();
Person & greater(Person & x) getch();
{ }
if x.age > age
return x; // refer to p2 Output : 25
else
return *this; // refer to p1
}
Object Oriented Programming (C++) SY BCA SEM – III

void disp() { cout<<age; }


};

11. Write note on inheritance. (7) (Prof. Viral S. Patel)


[*note : Here just points are given. Students have to prepare answer of this question them self. ]
Define inheritance
Types of inheritance
visibility modifiers
Overriding problem
Hierarchical (multipath inheritance) problem – solve by virtual base class.

12. What is Polymorphism ? Differentiate between Compile time and Runtime Polymorphism. (5)
Polymorphism, a Greek term, means the ability to take more than one form.
Polymorphism allowing objects to access different internal structures but same external view.
It simply means one name, multiple forms .
The concept of polymorphism is implemented using the overloaded functions and operators.
Two types of polymorphism : (1) compile time polymorphism and (2) run time polymorphism.
Difference :

Compiler time polymorphism Run time polymorphism


It is also known as static binding, early binding It is also known as Dynamic binding , late
and overloading as well. binding and overriding as well.
Function binding occurs at compiler time. Function binding occurs at run time.
Overloading is compile time polymorphism Overriding is run time polymorphism having
where more than one methods share the same name method with same parameters,
same name with different parameters and but associated in a class & its subclass.
different return type.
It is achieved by function overloading and It is achieved by virtual functions and pointers.
operator overloading.
It provides fast execution because know early It provides slow execution as compare to early
at compile time. binding because it is know at runtime.
It is less flexible as all things execute at It is more flexible as all things execute at run
compile time. time.

13. Explain pure virtual function. When it is necessary ? Explain rules of pure virtual function. (4)
A pure virtual function is a function declared in a base class that has no definition relative to
the base class. It only serves as a placeholder. Such a function is called do-nothing function.
For example ,
virtual void display() = 0;
In such cases, the compiler requires each derived class to either define the function or
redeclare it as a pure virtual function.
The class contain pure virtual function is called abstract class.
 Sometimes implementation of all function cannot be provided in base class because we
don t know the implementation. Only derived class requires to define it. In such a case we have
to required pure virtual function to create abstract class.
class containing pure virtual functions cannot be used to declare any object. It can be used to
create base class pointer and achieving run time polymorphism. (Prof. Viral S. Patel)

Example :
Object Oriented Programming (C++) SY BCA SEM – III

class A class B : public A void main()


{ { {
public : public : // A ob1; give error as A is abstract
virtual void disp() = 0; void disp() B ob2;
}; { A *ptr = &ob2;
cout<< VSP ; ptr->disp();
} }
} Output : VSP

You might also like