A pure virtual function is a type of function which has only a function declaration. It does not have the function definition. The following program segment illustrates how to declare a pure virtual function:
Case 1.
//pure virtual functions
#include <iostream>
using namespace std;
class base {
private:
int x;
fl oat y;
public:
virtual void getdata();
virtual void display();
};
class derivedB: public base {
-------
-------
};
void base :: getdata() //pure virtual function definition
{
// empty statements
}
void base :: display() //pure virtual function definition
{
// empty statements
}
Example 1.
A program to demonstrate how a pure virtual function is defined, declared and invoked from the object of a derived class through the pointer of the base class.
//pure virtual functions
#include <iostream>
using namespace std;
class base {
private:
int x;
float y;
public:
virtual void getdata();
virtual void display();
};
class derivedB: public base {
private:
long int rollno;
char name[20];
public:
void getdata();
void display();
};
void base :: getdata() { } //pure virtual function
void base :: display() { } //pure virtual function
void derivedB :: getdata()
{
cout << “ enter roll number of a student ? \n”;
cin >> rollno;
cout << “ enter name of a student ? \n”;
cin >> name;
}
void derivedB :: display()
{
cout << “ Roll number student’s name \n”;
cout << rollno << ‘\t’ << name << endl;
}
int main()
{
base *ptr;
derivedB obj;
ptr = &obj;
ptr->getdata();
ptr->display();
return 0;
}
Output of the above program
enter roll number of a student?
20075
enter name of a student?
Antony
Roll number student’s name
20075 Antony
Case 2. A pure virtual function can also have the following format, when a virtual function is declared within the class declaration itself. The virtual function may be equated to zero if it does not have a function defi nition.
//pure virtual functions
#include <iostream> using namespace std;
class base {
private:
int x;
float y;
public:
virtual void getdata() = 0;
virtual void display() = 0;
};
class derivedB: public base {
-------
-------
};
Example 2.
A program to illustrate how to declare a pure virtual function and equate it to zero as it does not have any function parts.
//pure virtual functions
#include <iostream>
using namespace std;
class base {
private:
int x;
fl oat y;
public:
virtual inline void getdata() = 0;
virtual inline void display() = 0;
};
class derivedB: public base {
private:
long int rollno;
char name[20];
public:
void getdata();
void display();
};
void derivedB :: getdata()
{
cout << “ enter roll number of a student ? \n”;
cin >> rollno;
cout << “ enter name of a student ? \n”;
cin >> name;
}
void derivedB :: display()
{
cout << “ Roll number student’s name \n”;
cout << rollno << ‘\t’ << name << endl;
}
int main()
{
base *ptr;
derivedB obj;
ptr = &obj;
ptr->getdata();
ptr->display();
return 0;
}
Output of the above program
enter roll number of a student?
20076
enter name of a student?
Ratanakumar
Roll number student’s name
20076 Ratana Kumar
Note:-When an object of the derived class tries to access through the pointer of the base class members, the function invoking message will reach only the derived class members but not to the base class members as the base class member function may not have a function definition.
ABSTRACT BASE CLASSES
A class which consists of pure virtual functions is called an abstract base class. In the previous section, it has been discussed that a function may be defined without any statement or the function declaration may be equated to zero if it does not have the function definition part.
Example 3.
A program to demonstrate how to define an abstract base class with pure virtual functions in which the function definition part has been defined without any statement. The members of the derived class objects are accessed through the base class objects through pointer technique.
//demonstration of abstract base classes
#include <iostream>
using namespace std;
class base {
private:
int x;
fl oat y;
public:
virtual void getdata();
virtual void display();
};
class derivedB: public base {
private:
long int rollno;
char name[20];
public:
void getdata();
void display();
};
class derivedC: public base {
private:
fl oat height;
fl oat weight;
public:
void getdata();
void display();
};
void base :: getdata() {} //pure virtual function
void base :: display() {} // pure virtual function
void derivedB :: getdata()
{
cout << “ enter a roll number of student ? \n”;
cin >> rollno;
cout << “ enter a name of student ? \n”;
cin >> name;
}
void derivedB :: display()
{
cout << “ Roll number student’s name \n”;
cout << rollno << ‘\t’ << name << endl;
}
void derivedC :: getdata()
{
cout << “ enter height of student ? \n”;
cin >> height;
cout << “ enter weight of student ? \n”;
cin >> weight;
}
void derivedC :: display()
{
cout << “ Height and weight of the student’s \n”;
cout << height << ‘\t’ << weight << endl;
}
int main()
{
base *ptr[3];
derivedB objb;
derivedC objc;
ptr[0] = &objb;
ptr[1] = &objc;
ptr[0]->getdata();
ptr[1]->getdata();
ptr[0]->display();
ptr[1]->display();
return 0;
}
Output of the above program
enter a roll number of student?
20075
enter a name of student?
Ramu
enter height of student?
167.56
enter weight of student?
64
Roll number student’s name
20075 Ramu
Height and weight of the student’s
167.56 64
Example 4.
A program to demonstrate how to define an abstract base class with pure virtual functions in which the function declaration is equated to zero as it does not have the function definition part.
//demonstration of abstract base classes
//case -2
#include <iostream>
using namespace std;
class base {
private:
int x;
fl oat y;
public:
virtual inline void getdata() = 0;
virtual inline void display() = 0;
};
class derivedB: public base {
private:
long int rollno;
char name[20];
public:
void getdata();
void display();
};
class derivedC: public base {
private:
fl oat height;
fl oat weight;
public:
void getdata();
void display();
};
void derivedB :: getdata()
{
cout << “ enter a roll number of student ? \n”;
cin >> rollno;
cout << “ enter a name of student ? \n”;
cin >> name;
}
void derivedB :: display()
{
cout << “ Roll number student’s name \n”;
cout << rollno << ‘\t’ << name << endl;
}
void derivedC :: getdata()
{
cout << “ enter height of student ? \n”;
cin >> height;
cout << “ enter weight of student ? \n”;
cin >> weight;
}
void derivedC :: display()
{
cout << “ Height and weight of the student’s \n”;
cout << height << ‘\t’ << weight << endl;
}
int main()
{
base *ptr[3];
derivedB objb;
derivedC objc;
ptr[0] = &objb;
ptr[1] = &objc;
ptr[0]->getdata();
ptr[1]->getdata();
ptr[0]->display();
ptr[1]->display();
return 0;
}
Output of the above program
enter a roll number of student?
20071
enter a name of student?
Velusamy.K
enter height of student?
175.6
enter weight of student?
78
Roll number student’s name
20071 Velusamy.K
Height and weight of the student’s
175.6 78

0 comments
Post a Comment