Beruflich Dokumente
Kultur Dokumente
PART-A
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.
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;
int main() {
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:
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.
Ans :
#include <iostream>
#include <string>
class Animal
{
public:
Animal(const string& name) : name(name) {}
virtual string talk() = 0;
const string name;
};
PART-B
4.Why we can have virtual destructors but cannot have virtual constructors?
Ans :
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:
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()
{
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.
Ans :
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).
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.