Wednesday, November 15, 2023

PURE VIRTUAL FUNCTIONS

In practical applications the member functions of base classes are rarely used for doing any operation such functions are called as do-nothing functions or dummy functions or pure virtual functions.

Pure virtual functions are defined with null body, so that the derived classes can implement them.

syntax:

virtual void functionname( ) =0;

Ex:

virtual void display( ) =0;

Here the assignment operator is used just to instruct the compiler that the function is a pure virtual function and it will not have a definition.

The classes derived from pure abstract classes are required to redeclare the pure virtual function.

All these derived classes which redefine the pure virtual function are called as concrete classes.

These classes can be used to declare objects.

Ex:

#include<iostream.h>

#include<conio.h>

class first

{

protected:

int b;

public:

first()

{

b=10;

}

virtual void display() = 0;

};

class second : public first

{

int d;

public:

second()

{

d=20;

}

void display()

{

cout<<b<<d;

}

};

void main()

{

clrscr();

first *p;

second s;

p=&s;

p->display();

}

0 comments:

Post a Comment

Data Structures with C++



NET/SET/CS PG



Operating Systems



Computer Networks



JAVA



Design and Analysis of Algorithms



Programming in C++

Top