C++ has no abstract keyword or equivalent, to make a class abstract is to declare a pure virtual function in it (which prevents instantiation).
For this same reason, in C++ there is not much difference between an interface and an abstract class - a C++ interface is simply a class containing only pure virtual functions.
C++ has no keyword abstract. However, you can write pure virtual functions; that's the C++ way of expressing abstract classes.
What is a Pure Virtual Function:
A 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.
Syntax of Pure Virtual Function:
class class_name //This denotes the base class of C++ virtual function
{
public:
virtual void virtualfunctioname() = 0 //This denotes the pure virtual function in C++
};
___________________________________________________________________
//Full code example
#include <iostream>
using namespace std;
class Sandeep
{
public:
virtual void example()=0; //Denotes pure virtual Function Definition
};
class Demo1:public Sandeep
{
public:
void example()
{
cout << " \n Welcome";
}
};
class Demo2:public Sandeep
{
public:
void example()
{
cout << " To My blog \n";
}
};
int main()
{
Sandeep* arra[2];
Demo1 d1;
Demo2 d2;
arra[0]=&d1;
arra[1]=&d2;
arra[0]->example();
arra[1]->example();
}
OUTPUT:
For this same reason, in C++ there is not much difference between an interface and an abstract class - a C++ interface is simply a class containing only pure virtual functions.
C++ has no keyword abstract. However, you can write pure virtual functions; that's the C++ way of expressing abstract classes.
What is a Pure Virtual Function:
A 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.
Syntax of Pure Virtual Function:
class class_name //This denotes the base class of C++ virtual function
{
public:
virtual void virtualfunctioname() = 0 //This denotes the pure virtual function in C++
};
___________________________________________________________________
//Full code example
#include <iostream>
using namespace std;
class Sandeep
{
public:
virtual void example()=0; //Denotes pure virtual Function Definition
};
class Demo1:public Sandeep
{
public:
void example()
{
cout << " \n Welcome";
}
};
class Demo2:public Sandeep
{
public:
void example()
{
cout << " To My blog \n";
}
};
int main()
{
Sandeep* arra[2];
Demo1 d1;
Demo2 d2;
arra[0]=&d1;
arra[1]=&d2;
arra[0]->example();
arra[1]->example();
}
OUTPUT:
No comments:
Post a Comment