OOPs Assignment For Btech 2nd Year

You might also like

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

ASSIGNMENT – 4

VARUN UTSAV
2K19/CO/423

1. Describe various type of inheritance with suitable examples.

OOPs support the six different types of inheritance as given below :


1. Single inheritance
2. Multi-level inheritance
3. Multiple inheritance
4. Multipath inheritance
5. Hierarchical Inheritance
6. Hybrid Inheritance

Single inheritance
In this inheritance, a derived class is created from a single base class.
In the given example, Class A is the parent class and Class B is the child
class since Class B inherits the features and behavior of the parent class A.

Multi-level inheritance
In this inheritance, a derived class is created from another derived class.
In the given example, class c inherits the properties and behavior of class B
and class B inherits the properties and behavior of class B. So, here A is the
parent class of B and class B is the parent class of C. So, here class C
implicitly inherits the properties and behavior of class A along with Class B
i.e there is a multilevel of inheritance.
Multiple inheritance
In this inheritance, a derived class is created from more than one base class.
This inheritance is not supported by .NET Languages like C#, F# etc. and
Java Language.
In the given example, class c inherits the properties and behavior of class B
and class A at same level. So, here A and Class B both are the parent classes
for Class C.

Multipath inheritance
In this inheritance, a derived class is created from another derived classes
and the same base class of another derived classes. This inheritance is not
supported by .NET Languages like C#, F# etc.
In the given example, class D inherits the properties and behavior of class C
and class B as well as Class A. Both class C and class B inherits the Class A.
So, Class A is the parent for Class B and Class C as well as Class D. So it's
making it Multipath inheritance.
Hierarchical Inheritance
In this inheritance, more than one derived classes are created from a single
base class and futher child classes act as parent classes for more than one
child classes.
In the given example, class A has two childs class B and class D. Further,
class B and class C both are having two childs - class D and E; class F and G
respectively.

Hybrid inheritance
This is combination of more than one inheritance. Hence, it may be a
combination of Multilevel and Multiple inheritance or Hierarchical and
Multilevel inheritance or Hierarchical and Multipath inheritance or
Hierarchical, Multilevel and Multiple inheritance.
Since .NET Languages like C#, F# etc. does not support multiple and
multipath inheritance. Hence hybrid inheritance with a combination of
multiple or multipath inheritances is not supported by .NET Languages.

2. Give the definition of virtual base class in C++ syntax. Explain why virtual
base classes are required?
Virtual base classes are used in virtual inheritance in a way of preventing
multiple “instances” of a given class appearing in an inheritance hierarchy
when using multiple inheritances.
Need for Virtual Base Classes:
Consider the situation where we have one class A .This class is A is
inherited by two other classes B and C. Both these class are inherited into
another in a new class D as shown in figure below.
As we can see from the figure that data members/function of class A are
inherited twice to class D. One through class B and second through class C.
When any data / function member of class A is accessed by an object of
class D, ambiguity arises as to which data/function member would be
called? One inherited through B or the other inherited through C. This
confuses compiler and it displays error.

3. Define ambiguity in inheritance. How ambiguities can be resolved by using


scope resolution operator and virtual base class. Explain your answer with
example.

Ambiguity in C++ occur when a derived class have two base classes and
these two base classes have one common base class. Consider the followling
example:
#include<iostream.h>
#include<conio.h>

class ClassA
{
public:
int a;
};

class ClassB : public ClassA


{
public:
int b;
};
class ClassC : public ClassA
{
public:
int c;
};

class ClassD : public ClassB, public ClassC


{
public:
int d;
};

void main()
{

ClassD obj;

//obj.a = 10; //Statement 1, Error occur


//obj.a = 100; //Statement 2, Error occur

obj.ClassB::a = 10; //Statement 3


obj.ClassC::a = 100; //Statement 4

obj.b = 20;
obj.c = 30;
obj.d = 40;

cout<< "\n A from ClassB : "<< obj.ClassB::a;


cout<< "\n A from ClassC : "<< obj.ClassC::a;

cout<< "\n B : "<< obj.b;


cout<< "\n C : "<< obj.c;
cout<< "\n D : "<< obj.d;

In the above example, both ClassB & ClassC inherit ClassA, they both have


single copy of ClassA. However ClassD inherit both ClassB & ClassC,
therefore ClassD have two copies of ClassA, one from ClassB and another
from ClassC.

If we need to access the data member a of ClassA through the object of ClassD,


we must specify the path from which a will be accessed, whether it is
from ClassB or ClassC, bco'z compiler can't differentiate between two copies
of ClassA in ClassD.

There are two ways to avoid c++ ambiguity

 Using scope resolution operator


 Using virtual base class

1. Avoid ambiguity using scope resolution operator

Using scope resolution operator we can manually specify the path from which
data member a will be accessed, as shown in statement 3 and 4, in the above
example.
obj.ClassB::a = 10; //Statement 3
obj.ClassC::a = 100; //Statement 4

Note : still, there are two copies of ClassA in ClassD.

2. Avoid ambiguity using virtual base class

To remove multiple copies of ClassA from ClassD, we must inherit ClassA in


ClassB and ClassC as virtual class.

Example to avoid ambiguity by making base class as a virtual base class

#include<iostream.h>
#include<conio.h>

class ClassA
{
public:
int a;
};

class ClassB : virtual public ClassA


{
public:
int b;
};
class ClassC : virtual public ClassA
{
public:
int c;
};

class ClassD : public ClassB, public ClassC


{
public:
int d;
};
void main()
{

ClassD obj;

obj.a = 10; //Statement 3


obj.a = 100; //Statement 4

obj.b = 20;
obj.c = 30;
obj.d = 40;

cout<< "\n A : "<< obj.a;


cout<< "\n B : "<< obj.b;
cout<< "\n C : "<< obj.c;
cout<< "\n D : "<< obj.d;

}
4. With an appropriate example , explain how ambiguities can be resolved for
public and protected attributes in case of multi path inheritance.
Multipath inheritance in C++ is derivation of a class from other derived
classes, which are derived from the same base class. In this type of
inheritance,  there involves other inheritance like multiple, multilevel,
hierarchical etc.
It is famously known as diamond problem in computer programming.

 To eliminate this problem, C++ has a mechanism to inherit a single copy of


properties from the common base class.
 This is done by declaring the base class as virtual while creating derive
classes from this base class.

The ambiguity can be resolved without making virtual base class as:
class A
{
private: //..
public:
void showdata()
{
//...
}
};
class B: virtual public A
{
//...
};
class C: virtual public A
{
//..
};
class D: public B, public C
{
public:
void show_data()
{
showdata();//ambiguous which showdata()
A::showdata();//still ambiguous because B and C both have
A::showdata();
B::showdata(); //ok, not ambiguous
C::showdata(); //ok
}
};

5. Explain difference between function overloading and function overriding


with suitable examples.
Function overloading is a feature that allows us to have same function more
than once in a program. Overloaded functions have same name but their
signature must be different.
Here we have the same function sum declared four times with different
signatures. Based on the parameters we pass, while calling function sum,
decides which method is to be called. This happens during compilation,
which is why it is also known as compile time polymorphism. If you are
wondering why I have suffixed each floating point value with “f” letter in
the example below, during function call then refer this: function overloading
float issue.
#include <iostream>
using namespace std;
// overloaded functions
float sum(int, int);
float sum(float, float);
float sum(int, float);
float sum(float, int);
int main(){
//This will call the second function
cout<<sum(15.7f, 12.7f)<<endl;

//This will call the first function


cout<<sum(200, 100)<<endl;

//This will call the third function


cout<<sum(100, 20.7f)<<endl;

//This will call the fourth function


cout<<sum(90.8f, 30)<<endl;

return 0;
}
float sum(int a, int b){
return a+b;
}
float sum(float a, float b){
return a+b;
}
float sum(int a, float b){
return a+b;
}
float sum(float a, int b){
return a+b;
}
Function overriding is a feature of OOPs Programming that allows us to
override a function of parent class in child class.
#include<iostream>
using namespace std;
//Parent class or super class or base class
class A{
public:
void disp() {
cout<<"Parent Class disp() function"<<endl;
}
void xyz() {
cout<<"xyz() function of parent class";
}
};
//child class or sub class or derived class
class B : public A{
public:
/* Overriding disp() function of parent class
* and giving a different definition to it.
*/
void disp() {
cout<<"Child class disp() function"<<endl;
}
};
int main(){
//Creating object of child class B
B obj;
obj.disp();
/* If you want to call the overridden function
* (the same function which is present in parent class)
* from the child class then assign the reference of
* parent class to the child class object.
*/
A obj2 = B();
obj2.disp();
}
6. Create a class called volume which contains a method called find_vol().
Write down appropriate code to create objects named as sphere and cylinder
of the above class and implement function overloading to calculate volume
of sphere and cylinder.
#include<iostream>
using namespace std;
float vol(int,int);
float vol(float);
int vol(int);

class Volume{
public:
float find_vol(int r,int h)
{
return(3.14*r*r*h);
}
float find_vol(float r1)
{
return((4*3.14*r1*r1*r1)/3);
}

int main()
{
int r,h,a;
float r1;
cout<<"Enter radius and height of a cylinder:";
cin>>r>>h;
cout<<"Enter radius of sphere: ";
cin>>r1;
cout<<"Volume of cylinder is "<<find_vol(r,h);
cout<<"\nVolume of sphere is "<<find_vol(r1);
return 0;
}
7. We know that a private member of a base class is not inheritable. Is it
anyway possible for the objects of a derived class to access the private
members of the base class? If yes how?
Derived class can not access the private members of it's base class. No type
of inheritance allows access to private members.

However if you use friend declaration you can do that.


There is no other way to access other class's private data then friendship.
What you can do with inheritance, however, is to access protected data of
the base class. But it doesn't mean you can access protected data of another
object of the base type. You can only access protected data of the base part
of the derived class:

class base{
protected: //protected instead of private
base *ptr1;
int data;
public:
base(){}
base(int d) { data=d; }
};
class derived:private base{
public:
void member();
};

void derived::member()
{
base *temp=new base(3);
//temp->ptr1 = 0; //you need friendship to access ptr1 in temp

this->ptr1 = 0; // but you can access base::ptr1 while it is protected


}
8. How the properties of the following two derived classes differ?
a) class D1 : private B( // .. .. );
b) class D2 : public B( // ..);

 private inheritance makes the public and protected members of the


base class private in the derived class.
 public inheritance makes public members of the base class public in
the derived class, and the protected members of the base class remain
protected in the derived class.

You might also like