Assg3_OOP_solution (1)
Assg3_OOP_solution (1)
Assg3_OOP_solution (1)
Example:
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 ; }
};
Example :
Object Oriented Programming (C++) SY BCA SEM – III
Example :
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)
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.
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)
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
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 :
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
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 {...........};
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
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.
Example :
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 :
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