Assignment You have learnt about polymorphism in the lecturesdelivered to you. Y
ID: 3618680 • Letter: A
Question
Assignment
You have learnt about polymorphism in the lecturesdelivered to you. You are now required to recall the relatedlectures and answer the following questions:-
Q. a) What is the role of virtual function inpolymorphism? Give an example to example to explain it. Try to beprecise and to the point (3+5) (Your answer should not be copiedfrom the handouts)
Q. b) Give at least two advantagesand disadvantages of polymorphism (2+2)
You have learnt about inheritance in the lecturesdelivered to you. You are now required to recall the relatedlectures and answer the following questions:-
Q. c) Give the basic difference between simple inheritance andmultiple inheritance. What are the complexity problems in multipleinheritance? Give a brief description aboutthese. (2+6)
What to submit
You are required to submit your solution file in MS WORDformat.
Explanation / Answer
Virtual functions are accessed using a base class pointer. A pointer to the base class can be created. A base class pointer can contain the address of the derived object as the derived object contains the subset of base class object. Every derived class is also a base class. When a base class pointer contains the address of the derived class object, at runtime it is decided which version of virtual function is called depending on the type of object contained by the pointer. Here is a program which illustrates the working of virtual functions.
Example
include<iostream>
using namespace std;
class shape
{
public:
int side;
virtual int volume()
{
cout << endl <<"Virtual function of base class " << endl;
return(0);
};
class cube: public shape
{
public:
int volume()
{
cout << endl << "Volume function of cube " << endl;
return(side*side*side);
}
};
class cuboid:public shape
{
public:
int breadth;
int height;
int volume()
{
cout << endl << "Volume function of cuboid " << endl;
return(side*breadth*height);
}
};
int main()
{
shape *s,s1;
cube c1;
cuboid c2;
cout << "Enter the side of the cube" << endl;
cin >> c1.side;
cout << endl << "Enter the side of the cuboid " << endl;
cin >> c2.side;
cout << endl << "Enter the breadth of the cuboid" << endl;
cin >> c2.breadth;
cout << endl << "Enter the height of the cuboid" << endl;
cin >> c2.height;
s=&s1;
s->volume();
s=&c1;
cout << endl << "The volume of the cube " << s->volume() << endl;
s=&c2;
cout << endl << "The volume of the cuboid " << s->volume() << endl;
return(0);
}
Q1 b). Give at least two advantages and disadvantages of polymorphism (2+2)
Answer. Advantages
1. More concise.
2. Modular.
3. Easy to change and adapt.
Disadvantages
1. In C++, the code isn't nearly as important as the overall design
of the class hiearchy, which requires maintenance programmers to do
more studying of the code before they get up to speed.
2. Again, when you have classes inheriting from classes inheriting from
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.