You are on page 1of 25

Pointers to derived class

A base class pointer can also be used as a pointer to an


object of any class derived from that base.
Although a base class pointer can be used to point to a
derived object, the opposite is not true.
Although you can use a base pointer to point to a derived
object, you can access only the members of the derived
type that were imported from the base. That is, you won't
be able to access any members added by the derived class.



Pointers to derived class
#include <iostream.h>
class base {
int i;
public:
void set_i(int num) { i=num; }
int get_i() { return i; }
};
class derived: public base {
int j;
public:
void set_j(int num) { j=num; }
int get_j() { return j; }
};
int main()
{
base *bp;
derived d;
bp = &d; // base pointer points to derived object
// access derived object using base pointer
bp->set_i(10);
cout << bp->get_i() << " ";

bp->set_j(88);
cout << bp->get_j();
return 0;
}
Solution:

((derived *) bp) -> set_j();
((derived *) bp) -> get_j();


// error
// error
ONE NAME AND MANY FORMS
Polymorphism
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(), 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 stackthe functions push() and pop() are the
same no matter which type of stack is being used. The
individual versions of these functions define the specific
implementations (methods) for each type of data.

Compile time polymorphism
Function overloading
Operator overloading
Run time polymorphism
Inheritance and Virtual functions
Compile time polymorphism (early binding)
The overloaded member functions are selected for
invoking by matching arguments, both type and number.
This information is known to the compiler at the compile
time and is able to select the appropriate function for a
particular call at the compile time itself. This is called
early binding or static binding.
An object is bound to its function call at compile time.
Run time polymorphism
Virtual function
when we use the same function name in both the base and derived
classes, the function in base class is declared as virtual using the
keyword virtual preceding its normal declaration.
When a function is made virtual, C++ determines which function to
use at run time based on the type of object pointed to by the base
pointer, rather than type of the pointer.
#include <iostream.h>
class base {
public:
void function1(){cout<<Object;}
virtual void function2(){cout<<oriented;}
};
class derived: public base {
public:
void function1(){cout<<programming;}
void function2(){cout<<C++;}
};

int main()
{
base *bp,b;
derived d;
bp = &b;
bp->function1();bp->function2();

bp=&d;
bp ->function1();bp->function2();
return 0;
}

The Virtual Attribute Is Inherited
#include <iostream.h>
class base {
public:
virtual void vfunc() {
cout << "This is base's vfunc().\n";
}
};


class derived1 : public base {
public:
void vfunc() {
cout << "This is derived1's vfunc().\n";
}
};
/* derived2 inherits virtual function vfunc()
from derived1. */

class derived2 : public derived1 {
public:
// vfunc() is still virtual
void vfunc() {
cout << "This is derived2's vfunc().\n";
}
};
int main()
{
base *p, b;derived1 d1;derived2 d2; p = &b;
p->vfunc(); // access base's vfunc()
// point to derived1
p = &d1;
p->vfunc(); // access derived1's vfunc()
// point to derived2
p = &d2;
p->vfunc(); // access derived2's vfunc()
return 0;}


Virtual Functions Are Hierarchical
#include <iostream.h>
class base {
public:
virtual void vfunc() {
cout << "This is base's vfunc().\n";
}
};
class derived1 : public base {
public:
void vfunc() {
cout << "This is derived1's vfunc().\n";
}
};
class derived2 : public base {
public:
};
int main()
{
base *p, b;
derived1 d1;
derived2 d2;
p = &b;
p->vfunc();
p = &d1;
p->vfunc();
p = &d2;
p->vfunc();
return 0;
}


// access base's vfunc()
// access derived1's vfunc()
// use base's vfunc()
Pure Virtual Functions
A pure virtual function is a virtual function that has no
definition within the base class. To declare a pure virtual
function, use this general form:
virtual type func-name(parameter-list) = 0;
When a virtual function is made pure, any derived class
must provide its own definition. If the derived class fails
to override the pure virtual function, a compile-time error
will result.

Abstract Classes
A class that contains at least one pure virtual function is
said to be abstract. Because an abstract class contains one
or more functions for which there is no definition (that is,
a pure virtual function), no objects of an abstract class
may be created. Instead, an abstract class constitutes an
incomplete type that is used as a foundation for derived
classes.

Virtual function
The virtual functions must be members of some class.
They cannot be static members.
They are accessed by using object pointers
The prototypes of the base class version of a virtual function and
all the derived class versions must be identical. If two functions
with the same name have different prototypes, C++ considers
them as overloaded functions, and the virtual function mechanism
is ignored
Programs
Write a program to create a class called person having the
name,age,sex,occupation as its data members and
getdata() and putdata() as its member functions. Now,
create two new classes called student and employee. Add
the suitable members to these two classes. In the base
class, declare the member function as virtual. Implement
the above program using run time polymorphism.

Write a program to create a class called point with x and y
as its data members and readpoint() and print as its
member functions. From this class derive two new classes
called 2D and 3D. Add suitable members to these classes.
Use virtual functions and virtual destructors.

Write a program to declare a function show() in base and
derived class. Display message through the function to
know name of the class whose member function is
executed. Using late binding concept.

Create a base class called shape. Use this class to store two double type values
that could be used to compute the area of figures. Derive two specific classes
called triangle and rectangle from the base shape. Add to the base class, a
member function get_data() to initialize base class data members and another
member function display_area() to compute and display the area of figures.
Make display_area() as a virtual function and redefine this function in the
derived classes to suit their requirements.
Using these three classes, design a program that will accept dimensions of a
triangle or a rectangle interactively, and display the area.
Remember the two values given as input will be treated as lengths of two sides
in the case of rectangles, and as base and height in the case of triangles, and
used as follows:
Area of rectangle=X*y
Area of triangle=1/2*x*y

programs
Write a program to declare a function show() in base and
derived class. Display message through the function to
know name of the class whose member function is
executed. Use late binding concept using virtual keyword.
WAP to demonstrate use of abstract classes.
WAP to invoke member functions of base and derived
classes using pointer of base class.
WAP to access members of base and derived classes using
pointer objects of both classes.

You might also like