Sie sind auf Seite 1von 7

HOMEWORK NO:4

Course : Object Oriented Program Code : CSE103

Date of Assignment : 27 October 2010 Date of Submission:16 nov

PART-A

1.What is a difference between abstract class and base class?

Ans :

Base classes have their own implementations for methods, and these implementations can be
used/added to in an inherited class.

Abstract classes have declarations for class methods, but no implementation. Any class
inheriting the abstract class must implement it's methods.

2.Explain in detail about pointer to class memeber with example program?

An s :

Pointers to Members

Pointers to members allow you to refer to nonstatic members of class objects. You cannot
use a pointer to member to point to a static class member because the address of a static member
is not associated with any particular object. To point to a static class member, you must use a
normal pointer.

You can use pointers to member functions in the same manner as pointers to functions. You can
compare pointers to member functions, assign values to them, and use them to call member
functions. Note that a member function does not have the same type as a nonmember function
that has the same number and type of arguments and the same return type.

Pointers to members can be declared and used as shown in the following example:

#include <iostream>
using namespace std;

class X {
public:
int a;
void f(int b) {
cout << "The value of b is "<< b << endl;

Make by raj 9464554250


}
};

int main() {

// declare pointer to data member


int X::*ptiptr = &X::a;

// declare a pointer to member function


void (X::* ptfptr) (int) = &X::f;

// create an object of class type X


X xobject;

// initialize data member


xobject.*ptiptr = 10;

cout << "The value of a is " << xobject.*ptiptr << endl;

// call member function


(xobject.*ptfptr) (20);
}

The output for this example is:

The value of a is 10
The value of b is 20

To reduce complex syntax, you can declare a typedef to be a pointer to a member. A pointer to a
member can be declared and used as shown in the following code fragment:

typedef int X::*my_pointer_to_member;


typedef void (X::*my_pointer_to_function) (int);

int main() {
my_pointer_to_member ptiptr = &X::a;
my_pointer_to_function ptfptr = &X::f;
X xobject;
xobject.*ptiptr = 10;
cout << "The value of a is " << xobject.*ptiptr << endl;
(xobject.*ptfptr) (20);
}

The pointer to member operators .* and ->* are used to bind a pointer to a member of a specific
class object. Because the precedence of () (function call operator) is higher than .* and ->*, you
must use parentheses to call the function pointed to by ptf.

3.Explain how run time polymorphism is achieved?

Ans :

Make by raj 9464554250


Polymorphism is one of the most important principle of object oriented
language. Polymorphism means many forms i.e. one interface multiple
methods. The specific method to call depends on the nature of the situation. No
matter what type of car it is the driving mechanism is the same. For example, you
might have a program that defines three different types of stacks. One stack is used
for integer values, one for character values, and one for floating-point values.
Because of polymorphism, you can define one set of names, push() and pop() , that
can be used for all three stacks. In your program you will create three specific
versions of these functions, one for each type of stack, but names of the functions
will be the same. The compiler will automatically select the right function based
upon the data being stored. Thus, the interface to a stack—the functions push() and
pop() —are the same no matter which type of stack is being used. C++ supports
both run time and compile time polymorphism. Functi on overloading is a form
of compile time polymorphism while dynamic binding is a form of runtime
polymorphism.

#include <iostream>
#include <string>

using namespace std;

class Animal
{
public:
Animal(const string& name) : name(name) {}
virtual string talk() = 0;
const string name;
};

class Cat : public Animal


{
public:
Cat(const string& name) : Animal(name) {}
virtual string talk() { return "Meow!"; }
};

class Dog : public Animal


{
public:
Dog(const string& name) : Animal(name) {}
virtual string talk() { return "Arf! Arf!"; }
};

// prints the following:


//
// Missy: Meow!
// Mr. Mistoffelees: Meow!
// Lassie: Arf! Arf!
//
int main()
{
Animal* animals[] =

Make by raj 9464554250


{
new Cat("Missy"),
new Cat("Mr. Mistoffelees"),
new Dog("Lassie")
};

for(int i = 0; i < 3; i++)


{
cout << animals[i]->name << ": " << animals[i]->talk() <<
endl;
delete animals[i];
}
return 0;
}

PART-B

4.Why we can have virtual destructors but cannot have virtual constructors?

Ans :

Virtual destructors: If an object (with a non-virtual destructor) is destroyed explicitly by


applying the delete operator to a base-class pointer to the object, the base-class destructor
function (matching the pointer type) is called on the object.
There is a simple solution to this problem – declare a virtual base-class destructor. This makes
all derived-class destructors virtual even though they don’t have the same name as the base-class
destructor. Now, if the object in the hierarchy is destroyed explicitly by applying the delete
operator to a base-class pointer to a derived-class object, the destructor for the appropriate class
is called.

Virtual constructor: Constructors cannot be virtual. Declaring a constructor as a virtual function


is a syntax error.

Or

Virtual constructor is not build-in C++ feature but it doesn't mean its not used by
devs in code and in conversations. There are many other things that doesn't exist in
particular language yet people find ways around to solve it (SingleTon Virtual
Constructor Double-Dispatching etc). What means by virtual ctr is a static member
function that returns pointer to a heap object (whether new or existing) or it might
return reference to static object declared in this function. It would be really wrong to
say that virtual ctr doesn't exist --it means you are not aware of such thing and
Make by raj 9464554250
absolutely nothing else. The nice thing about virt ctr each derived virt ctr can return
pointer to a type of the class it's declare in --thus we can say virtual functions have
different signatures depending where they implemented:

class base {public: virtual base * virtCtr(){return new base;}


class deriv:public base {public: virtual deriv * virtCtr(){retun new deriv;}

5.How can we execute differnernt versions of virtual function?

Ans:

Whenever a program has a virtual function declared, a v - table is constructed for the class. The
v-table consists of addresses to the virtual functions for classes that contain one or more virtual
functions. The object of the class containing the virtual function contains a virtual pointer that
points to the base address of the virtual table in memory. Whenever there is a virtual function
call, the v-table is used to resolve to the function address. An object of the class that contains one
or more virtual functions contains a virtual pointer called the vptr at the very beginning of the
object in the memory. Hence the size of the object in this case increases by the size of the
pointer. This vptr contains the base address of the virtual table in memory. Note that virtual
tables are class specific, i.e., there is only one virtual table for a class irrespective of the number
of virtual functions it contains. This virtual table in turn contains the base addresses of one or
more virtual functions of the class. At the time when a virtual function is called on an object, the
vptr of that object provides the base address of the virtual table for that class in memory. This
table is used to resolve the function call as it contains the addresses of all the virtual functions of
that class. This is how dynamic binding is resolved during a virtual function call.

The following code shows how we can write a virtual function in C++ and then use the same to
achieve dynamic or runtime polymorphism.

#include <iostream.h>
class base
{
public:
virtual void display()
{
cout<<”\nBase”;
}
};
class derived : public base
{
public:
void display()
{
cout<<”\nDerived”;
}
};
Make by raj 9464554250
void main()
{

base *ptr = new derived();


ptr->display();
}

In the above example, the pointer is of type base but it points to the derived class object. The
method display() is virtual in nature. Hence in order to resolve the virtual method call, the
context of the pointer is considered, i.e., the display method of the derived class is called and not
that of the base. If the method was non virtual in nature, the display() method of the base class
would have been called.

6.What is virtual function. Why do we need virtual function. When do we make a


function pure virtual function?

Ans :

What are Virtual Functions?


Virtual, as the name implies, is something that exists in effect but not in reality. The
concept of virtual function is the same as a function, but it does not really exist
although it appears in needed places in a program. The object-oriented
programming language C++ implements the concept of virtual function as a simple
member function, like all member functions of the class.

The functionality of virtual functions can be over-ridden in its derived classes. The programmer
must pay attention not to confuse this concept with function overloading. Function overloading is
a different concept and will be explained in later sections of this tutorial. Virtual function is a
mechanism to implement the concept of polymorphism (the ability to give different meanings to
one function).

Make by raj 9464554250


Need for Virtual Function:

The vital reason for having a virtual function is to implement a different functionality in the
derived class.

For example: a Make function in a class Vehicle may have to make a Vehicle with red color. A
class called FourWheeler, derived or inherited from Vehicle, may have to use a blue background
and 4 tires as wheels. For this scenario, the Make function for FourWheeler should now have a
different functionality from the one at the class called Vehicle. This concept is called Virtual
Function.

Make by raj 9464554250

Das könnte Ihnen auch gefallen