1
Q:-1 what is Inheritance? Explain Types of Inheritance with example.
The mechanism of deriving a new class from an old one is called inheritance (or
derivation).The old class is referred to as the base class and the new one is called the
derived class or subclass.
Inheritance is a mechanism of reusing and extending existing classes without
modifying them, thus producing hierarchical relationships between them.
Inheritance allows us to define a class in terms of another class, which makes it easier
to create and maintain an application. This also provides an opportunity to reuse the code
functionality and fast implementation time.
There are five types of Inheritance are available
1. Single Inheritance
4. Multilevel Inheritance
2. Multiple Inheritance
5. Hybrid Inheritance
3. Hierarchical Inheritance
1. Single Inheritance: - Single Inheritance is method in which a derived class has only
one base class.
#include <iostream.h>
class Value
{
protected:
int val;
public:
void set_values (int a)
{ val=a;}
In example shows a
};
Base class value and a
class Cube: public Value
Derived class cube
{
class value contain
public:
Protected member such
int cube()
As val
{ return (val*val*val); }
Protected access
};
int main ()
Specifier we can use in
{
Derived class.
Private member can not Cube cub;
cub.set_values (5);
Be Inheriting.
cout << "The Cube of 5 is::" << cub.cube() << endl;
return 0;
}
Result:
The Cube of 5 is:: 125
Inheritance
2
2. Multiple Inheritance:- You can derive a class from any number of base classes.
Deriving a class from more than one direct base class is called multiple inheritance.
Multiple inheritance enables a derived class to inherit members from more than one
parent.
Deriving directly from more than one class is usually called multiple inheritance.
Syntax:Class C: Public A, Public B
{
Body of D
}
Example
class student
{
protected:
int rno,m1,m2;
public:
void get()
{
cout<<"Enter the Roll no :";
cin>>rno;
cout<<"Enter the two marks :";
cin>>m1>>m2;
}
};
class sports
{
protected:
int sm;
// sm = Sports mark
public:
void getsm()
{
cout<<"\nEnter the sports mark :";
cin>>sm;
}
};
class statement:public student,public sports
{
int tot,avg;
Inheritance
public:
void display()
{
tot=(m1+m2+sm);
avg=tot/3;
cout<<"\n\n\tRoll No : "<<rno<<"\n\tTotal
cout<<"\n\tAverage : "<<avg;
}
};
void main()
{
clrscr();
statement obj;
obj.get();
obj.getsm();
obj.display();
getch();
}
: "<<tot;
Output:
Enter the Roll no: 100
Enter two marks
90
80
Enter the Sports Mark: 90
Roll No: 100
Total : 260
Average: 86.66
Ambiguity: - In multiple inheritance the ambiguity arises when same method name is
being used by two derived class and further derivation from these two base classes.
To resolve this ambiguity we are using scope resolution operator.
class M
{
public:
void display()
{
cout<<"vimal \n";
}
};
class N
{
public:
void display()
{
cout<<"Vaiwala\n";
}
};
Output: Vimal
Vaiwala
Inheritance
Arts
Syntax:class A
{
Engineering
Medical
.
.
-------------
};
class B : visibility_label A
{
------------};
class C : visibility_label A
{
------------};
class D : visibility_label A
{
------------};
Here the visibility_label can be private,
protected or public. If we do not specify
any visibility_label then by default is
private.
Mech.
Elec.
class A
{
public:
int val;
void getvalue(int j)
{
val=j;
}
};
class B : public A
{
public:
void square()
{
cout<<endl<<val*val;
}
};
class C : public A
{
public:
void cube()
{
cout<<endl<<val*val*val;
}
};
Civil
void main()
{
int i;
clrscr();
B b;
C c;
cin>>i;
b.getvalue(i);
b.square();
c.getvalue(i);
c.cube();
getch();
}
Output
2
4
8
In figure base class A and class B and C are derived from class A.
So that path is called Hierarchical Inheritance.
Class B and C are the base class for class D. so that path is called
multiple inheritance. because there are more then one base class.
Example:class stu
{
protected:
int rno;
public:
void get_no(int a)
{
rno=a;
}
void put_no(void)
{
out<<"Roll no"<<rno<<"\n";
};
class test:public stu
{
protected:
float part1,part2;
public:
void get_mark(float x,float y)
{
part1=x;
part2=y;
}
void put_marks()
{
cout<<"Marks obtained:"<<"part1="<<part1<<"\n"<<"part2="<<part2<<"\n";
}
};
class sports
{
protected:
float score;
public:
void getscore(float s)
int main()
{
{
score=s;
clrscr();
}
result stu;
void putscore(void)
stu.get_no(123);
{
stu.get_mark(27.5,33.0);
cout<<"sports:"<<score<<"\n";
stu.getscore(6.0);
stu.display();
}
return 0;
};
}
class result: public test, public sports
{
float total;
public:
void display(void);
OUTPUT
};
void result::display(void)
Roll no 123
{
Marks obtained : part1=27.5
total=part1+part2+score;
Part2=33
put_no();
Sports=6
put_marks();
Total score = 66.5
putscore();
cout<<"Total Score="<<total<<"\n";
}
as virtual base class. This can be achieved by preceding the base class name with the
word virtual.
Grandparent
Parent 1
Parent 2
Child
The duplication of inherited members due to these multiple paths can be avoided by
making the common base class (ancestor class) as virtual base class.
Syntax
Class A //grandparent
{
}
Class B1 : virtual public A //parent 1
{
}
Class B2 : public virtual A //parent 2
{
}
Class C : public B1, public B2 //child
{
//Only one copy of A
//will be inherited
}
Note: - The Keyword virtual and public
may be use in either order.
Example
class student
{
protected:
int roll_number;
public:
void get_number(int a)
{
roll_number =a;
}
void put_number(void)
{
out<<"Roll no"<< roll_number <<"\n";
}
};
class test: virtual public student
{
protected:
float part1,part2;
public:
void get_mark(float x,float y)
{
Test
Sports
Result
Virtual Base Class
part1=x;
part2=y;
}
void put_marks()
{
cout<<"Marks obtained:"<<"part1="<<part1<<"\n"<<"part2="<<part2<<"\n";
}
};
class sports:public virtual student
{
protected:
float score;
public:
void get_score(float s)
int main()
{
{
score=s;
clrscr();
}
result stu;
void put_score(void)
stu.get_number(123);
{
stu.get_mark(27.5,33.0);
cout<<"sports:"<<score<<"\n";
stu.get_score(6.0);
stu.display();
}
return 0;
};
}
class result: public test, public sports
{
float total;
public:
void display(void);
OUTPUT
};
void result::display(void)
Roll no 123
{
Marks obtained : part1=27.5
total=part1+part2+score;
Part2=33
put_number();
Sports=6
put_marks();
Total score = 66.5
put_score();
cout<<"Total Score="<<total<<"\n";
}
Q:-4 Explain This Pointer
C++ uses unique keyword called this to represent an object that invokes a member
function. this is a pointer that points to the object for which this function was called.
This unique pointer is automatically passed to a member function when it is called. The
pointer this acts as an implicit argument to all the member functions.
class abc
The Private variable a can be used directly inside a member
{
function, like a=123;
int a;
We can also use the following statement to do same job:
public:
thisa=123;
Since C++ permits the use of shorthand form a=123, we have not
been using pointer this explicitly so far. However, we have been
implicitly using the pointer this when overloading the operators using
member function.
void display()
{
this->a=123;
cout<<a;
}
};
main()
{
clrscr();
abc a;
a.display();
getch();
}
Output:=
123
Output
20
10
{
return (width * height / 2);
}
};
int main () {
clrscr();
CRectangle rect;
CTriangle trgl;
CPolygon * ppoly1 = ▭
CPolygon * ppoly2 = &trgl;
ppoly1->set_values (4,5);
ppoly2->set_values (4,5);
cout << rect.area() << endl;
cout << trgl.area() << endl;
return 0;
}
Q:-6 Explain Virtual Function With example
In object-oriented programming, a virtual function or virtual method is a function or
method whose behavior can be overridden within an inheriting class by a function with
the same signature.
When we use the same function name in both base and derived classes, the function in
base class is declared as virtual using keyword virtual preceding its normal declaration.
When a function is made virtual, C++ determines which function to use at run time
based by making the based on the type of object pointer to by base pointer, rather than the
type pointer. Thus, by making the base pointer to point to different objects, we can
execute different versions of the virtual function.
class Base
int main ()
{
{
public:
clrscr();
void display()
Base B;
{
Derived D;
cout<<"\n Display base";
Base *bptr;
}
cout<<"\n bptr points to base \n";
virtual void show()
bptr=&B;
{
bptr -> display();//call base version
cout<<"\n show base";
bptr -> show();//call base version
}
cout<<"\n\n bptr points to derived \n";
};
bptr=&D;
class Derived: public Base
bptr
-> display();//calls base version
Output
{
bptr
show();//calls
bptr ->
points
to base Derived version
public:
return 0;
void display()
} Display base
{
Show base
cout<<"\n Display Derived";
}
bptr points to Derived
Display base
Show derived
void show()
{
cout<<"\n show derived";
}
};
Rules for virtual function
1. The virtual function must be member of base class
2. They cannot be static members
3. They are accessed by using object pointers
4. Prototype of base class function & derived class must be same
5. Virtual function in base class must be defined even though it is not used
6. A virtual function can be friend function of another class
7. We could not have virtual constructor
8. If a virtual function is derived in base class, it need not be necessarily redefined in the
derived class
9. Pointer object of base class can point to any object of derived class but reverse is not
true
10. When a base pointer points to derived class, incrementing & decrementing it will not
make it point to the next object of derived class
Q:-7 Explain pure virtual function
A pure virtual function is a function declared in a base class that has no definition
relative to the base class. In such cases, the compiler requires each derived class to either
define the function or redeclare it as a pure virtual function
To create a pure virtual function, rather than define a body for the function, we simply
assign the function the value 0.
If you have a pure virtual, the class becomes abstract. You cannot create an object of it.
class base
{
public:
//pure virtual function
virtual void show()=0;
};
class derived1 : public base
{
public:
void show()
{
cout<<"\n Derived 1";
}
Q:-8 Explain Abstract
};
void main()
{
base *b; derived1 d1;
b = &d1;
b->show();
}
Output
Derived 1
Class
An abstract class is one that is not used to create objects. An abstract class is designed
only to act as a base class.
It is a design concept in program development and provides a base upon which other
classes may be built.
Allows the base class to provide only an interface for its derived classes.
Prevents anyone from creating an instance of this class.
A class is made abstract if at least one pure virtual function defined.