Sunteți pe pagina 1din 9

C++ Pure Virtual Function and Base Class

C++ Pure Virtual Function and Virtual Base Class

In this C++ tutorial, you will learn about pure virtual function, declaration of pure virtual
function and virtual base class, virtual base class and how to implement a virtual base
class, explained with examples.

What is Pure Virtual Function:

Pure Virtual Function is a Virtual function with no body.

Declaration of Pure Virtual Function:

Since pure virtual function has no body, the programmer must add the notation =0
for declaration of the pure virtual function in the base class.

General Syntax of Pure Virtual Function takes the form:

class classname //This denotes the base class of C++ virtual function
{
public:
virtual void virtualfunctioname() = 0; //This denotes the pure virtual function in
C++
};

The other concept of pure virtual function remains the same as described in the previous
section of virtual function.

To understand the declaration and usage of Pure Virtual Function, refer to this example:

class Exforsys
{
public:
virtual void example()=0; //Denotes pure virtual Function Definition
};

class Exf1:public Exforsys


{
public:
void example()
{
cout<<"Welcome";
}
};
class Exf2:public Exforsys
{
public:
void example()
{
cout<<"To Training";
}
};

void main()
{
Exforsys* arra[2];
Exf1 e1;
Exf2 e2;
arra[0]=&e1;
arra[1]=&e2;
arra[0]->example();
arra[1]->example();
}

Since the above example has no body, the pure virtual function example() is declared
with notation =0 in the base class Exforsys. The two derived class named Exf1 and Exf2
are derived from the base class Exforsys. The pure virtual function example() takes up
new definition. In the main function, a list of pointers is defined to the base class.

Two objects named e1 and e2 are defined for derived classes Exf1 and Exf2. The address
of the objects e1 and e2 are stored in the array pointers which are then used for accessing
the pure virtual function example() belonging to both the derived class EXf1 and EXf2
and thus, the output is as in the above example.

The programmer must clearly understand the concept of pure virtual functions having no
body in the base class and the notation =0 is independent of value assignment. The
notation =0 simply indicates the Virtual function is a pure virtual function as it has no
body.

Some programmers might want to remove this pure virtual function from the base class
as it has no body but this would result in an error. Without the declaration of the pure
virtual function in the base class, accessing statements of the pure virtual function such
as, arra[0]->example() and arra[1]->example() would result in an error. The pointers
should point to the base class Exforsys. Special care must be taken not to remove the
statement of declaration of the pure virtual function in the base class.

Virtual Base Class


In the above example, there are two derived classes Exf1 and Exf2 from the base class
Exforsys. As shown in the above diagram, the Training class is derived from both of the
derived classes Exf1 and Exf2. In this scenario, if a user has a member function in the
class Training where the user wants to access the data or member functions of the class
Exforsys it would result in error if it is performed like this:

class Exforsys
{
protected:
int x;
};

class Exf1:public Exforsys


{ };

class Exf2:public Exforsys


{ };

class Training:public Exf1,public Exf2


{
public:
int example()
{
return x;
}
};

The above program results in a compile time error as the member function example() of
class Training tries to access member data x of class Exforsys. This results in an error
because the derived classes Exf1 and Exf2 (derived from base class Exforsys) create
copies of Exforsys called subobjects.
This means that each of the subobjects have Exforsys member data and member
functions and each have one copy of member data x. When the member function of the
class Training tries to access member data x, confusion arises as to which of the two
copies it must access since it derived from both derived classes, resulting in a compile
time error.

When this occurs, Virtual base class is used. Both of the derived classes Exf1 and Exf2
are created as virtual base classes, meaning they should share a common subobject in
their base class.

For Example:

class Exforsys
{
protected:
int x;
;

class Exf1:virtual public Exforsys


{ };

class Exf2:virtual public Exforsys


{ };

class Training:public Exf1,public Exf2


{
public:
int example()
{
return x;
}
};

In the above example, both Exf1 and Exf2 are created as Virtual base classes by using the
keyword virtual. This enables them to share a common subobject of their base class
Exforsys. This results in only one copy that the member function example() of Class
Training can access the member data x.
----------------------------------------------------------------------
Some important points to note while using friend functions in C++:

• The keyword friend is placed only in the function declaration of


the friend function and not in the function definition.
.
• It is possible to declare a function as friend in any number of
classes.
.
• When a class is declared as a friend, the friend class has access
to the private data of the class that made this a friend.
.
• A friend function, even though it is not a member function, would
have the rights to access the private members of the class.
.
• It is possible to declare the friend function as either private
or public.
.
• The function can be invoked without the use of an object. The
friend function has its argument as objects, seen in example
below.

Example to understand the friend function:

#include
class exforsys
{
private:
int a,b;
public:
void test()
{
a=100;
b=200;
}
friend int compute(exforsys e1)

//Friend Function Declaration with keyword friend and with the object of class exforsys to
which it is friend passed to it
};

int compute(exforsys e1)


{
//Friend Function Definition which has access to private data
return int(e1.a+e2.b)-5;
}

main()
{
exforsys e;
e.test();
cout<<"The result is:"<
//Calling of Friend Function with object as argument.
}
C++ Static Functions
Category: C++
Comments (23)

C++ Static Functions

Static member functions have a class scope and they do not have access to the 'this'
pointer of the class. When a member is declared as static, a static member of class, it has
only one data for the entire class even though there are many objects created for the class.
The main usage of static function is when the programmer wants to have a function
which is accessible even when the class is not instantiated.

Defining Static Function:

Static function is defined by using the keyword static before the member function that is
to be declared as static function.

General syntax:

static return_data_type fucntionname()


//Static function defined with keyword static
{
statement1;
//Statements for execution inside static function
statement2;
..........
..........
}

For example if a function exforsys returning nothing is to be


declared as staic function it is done as follows:

static void exforsys()


{
........;
.......;
}

Accessing Static Function:


A normal member function is accessed using the object and an operator called the dot
member access operator. The functions declared static or static functions are accessed
using only the class name and the scope resolution operator, unlike in normal member
functions where these are not used.

Example:
The declaration of static member function and how to access static member function:

#include <iostream.h>
class example
{
private:
static int sum; //Static data
int x;
public:
example() //Constructor of the class
{
sum=sum+1;
x=sum;
}

~example() //Destructor of the class


{
sum=sum-1;
}

static void exforsys()


//Static function exforsys( ) defined with keyword static
{
cout<<"\nResult is: "<<sum;
}

void number() //Normal member function number( )


{
cout<<"\nNumber is: "<<x;
}
};

void main()
{
example e1;
example::exforsys();
//Static function exforsys() accessed using class name example and the scope resolution
operator ::
example e2,e3,e4;
example::exforsys();
e1.number();
//Normal member function accessed using object e1 and the dot member access operator.
e2.number();
e3.number();
e4.number();
}

The output of the above program is:

Result is: 1
Result is: 4
Number is: 1
Number is: 2
Number is: 3
Number is: 4

In the above example, the function exforsys() is defined as static function and the

integer data type sum is declared as static data type. Four objects e1, e2, e3 and e4 are
created for the class example. The constructor of the class example increments the sum
by 1 and the destructor of the class decrements sum by 1.

The static function is accessed using the class name example and the scope resolution
operator :: as

example::exforsys();

But the normal member function number() is accessed using the object name and the dot
member access operator as

e1.number()
e2.number()
e3.number()
e4.number()

The first time the static function exforsys() is called, there was one object created and
thus, the sum is incremented by 1 in the constructor printing the result of sum as 1.When
the static function exforsys() is called the second time, there were three more objects
e2,e3 and e4 created which results in the sum incremented thrice from 1 in the
constructor of the corresponding class example, resulting in the value of sum as 4, which
is displayed in the second result. Applying the above explanation, it is clear that the static
function operates on the class and not in object. To access static function the programmer
can use the class name, followed by the scope resolution operator, as seen in example
above.

The programmer must note the following while using static member functions:

• A static member function can only access static member data, static member
functions and data and functions outside the class. The programmer must take
note not to use static member function in the same manner as non-static member
function, as non-static member function can access all of the above including the
static data member.
.
• A non-static member function can be declared as virtual but care must be taken
not to declare a static member function as virtual.
.
• The programmer must first understand the concept of static data while learning
the context of static functions. It is possible to declare a data member of a class as
static irrespective of it being a public or a private type in class definition. If a data
is declared as static, then the static data is created and initialized only once. Non-
static data members are created again and again. For each separate object of the
class, the static data is created and initialized only once. As in the concept of
static data, all objects of the class in static functions share the variables. This
applies to all objects of the class.
.
• A non-static member function can be called only after instantiating the class as an
object. This is not the case with static member functions. A static member
function can be called, even when a class is not instantiated.
.
• A static member function cannot have access to the 'this' pointer of the class.

S-ar putea să vă placă și