-->

abstract class in C++




abstract class in C++

 

By definition, an abstract class in C++ is a class that has at least one pure virtual function (i.e., a function that has no definition). The classes inheriting the abstract class must provide a definition for the pure virtual function; otherwise, the subclass would become an abstract class itself.

 Difference between Abstract Class and Interface in Java - GeeksforGeeks

What is a C++ abstract class? - Educative.io

 

pure virtual function

A pure virtual function is a virtual function in C++ for which we need not to write any function definition and only we have to declare it. It is declared by assigning 0 in the declaration. ... If an Abstract Class has derived class, they must implement all pure virtual functions, or else they will become Abstract too.03-Apr-2019

Pure Virtual Functions and Abstract Classes in C++

 How to download

 

Click Here

////Name Syed Syab Ahmad Shah
////Roll number *****
////University of SWAT
////BS SE
#include <iostream>///including header fil
using namespace std;
class Enemy///creating class
{
public:////public access spacifire
virtual void attack()///creating virtual function
{
cout<<"Showing virtual function now.."<<endl;
}
};
class Ninja : public Enemy///child class and inharting parent class
{
public:
void attack()///creating funtcion
{
cout<<"i am Ninja ! Showing Ninja function now.."<<endl;
}
};
class Monster : public Enemy//child class and inharting parent class
{
public:///////////////if we comment out the below function or we remove that then the virtula function will be the output instead of it.
/* void attack()///creating funtcion
{
cout<<"i am Monster ! Showing Monster function now.."<<endl;
} */
};
int main()///creating main function
{
Ninja n;///creating objects of the child class 1
Monster m;///creating objects of the child class 2

Enemy *enemy1 = &n;///creating objects of the parent class, making pointer and assigning addresses
Enemy *enemy2 = &m;///creating objects of the parent class, making pointer and assigning addresses

///enemy1->attackPower(20);
///enemy2->attackPower(30);

n.attack();/////calling function
m.attack();/////calling function
return 0;
}