CS304 ENJOY

Suggestions & Comments
& Gup Shup

CS304 ENJOY

Postby abdulsaboor » Mon May 10, 2010 7:12 pm

A virtual function is a member function of the base class and which is redefined by the derived class. When a derived class inherits the class containing the virtual function, it has ability to redefine the virtual functions. A virtual function has a different functionality in the derived class. The virtual function within the base class provides the form of the interface to the function. Virtual function implements the philosophy of one interface and multiple methods. The virtual functions are resolved at the run time. This is called dynamic binding. The functions which are not virtual are resolved at compile time which is called static binding. A virtual function is created using the keyword virtual which precedes the name of the function.
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
classes, you had better understand why all of that was built the way
it was, or making a small change "here" will ripple throughout the
entire class hierarchy and cause changes in behaviour in the least
expected places.

Q1 c). Give the basic difference between simple inheritance and multiple inheritance. What are the complexity problems in multiple inheritances? Give a brief description about these. (2+6)

Answer. Difference between simple inheritance and multiple iheritance.

In simple inheritance and class is drived from a one base class whereas in multiple inheritances a class may be drived from two or more base classes.

Complexity problems in multiple inheritances

While multiple inheritances seem like a simple extension of single inheritance, multiple inheritance introduces a lot of issues that can markedly increase the complexity of programs and make them a maintenance nightmare. Let’s take a look at some of these situations.First, ambiguity can result when multiple base classes contain a function with the same name.When a function is evaluated, the compiler looks to see if Class contains a function with that name. If it doesn’t. The compiler then looks to see if any of the derived classes have a function with that name. See the problem here? The problem is that suppose if the class actually contains TWO functions with same name: one inherited from one class, and one inherited from other. Consequently, this function call is ambiguous, and you will receive a compiler error if you try to compile it.
DR ABDUL SABOOR
PHD Scholar at Superior University Lahore- Pakistan
MS Business Administration (HRM)
BS Business Administration (Marketing)
Member Editorial Board Science Publishing Group USA
Member Editorial Board International Journal of Marketing Studies
Cell=0308-6837987

Pakistan
User avatar
abdulsaboor
ADMIN
ADMIN
 
Posts: 2004
Joined: Fri Sep 28, 2007 3:42 am
Location: vehari-punjab-pakistan

Return to SUGGESTION & COMMENTS

Who is online

Users browsing this forum: No registered users and 2 guests
cron