Professional Documents
Culture Documents
OOPs Assignment For Btech 2nd Year
OOPs Assignment For Btech 2nd Year
OOPs Assignment For Btech 2nd Year
VARUN UTSAV
2K19/CO/423
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.
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;
};
void main()
{
ClassD obj;
obj.b = 20;
obj.c = 30;
obj.d = 40;
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
#include<iostream.h>
#include<conio.h>
class ClassA
{
public:
int a;
};
ClassD obj;
obj.b = 20;
obj.c = 30;
obj.d = 40;
}
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.
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
}
};
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.
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