abstract class and method in c++ code example
Example 1: what is abstract class in c++
#include <bits/stdc++.h>
using namespace std;
class person
{
string p_id;
public:
virtual void get_info()=0;
virtual void show()=0;
};
class student:public person
{
string name;
int roll_no;
public:
void get_info()
{
cout<<"Enter name of the student "<<endl;
cin>>name;
cout<<"Enter roll number of the student "<<endl;
cin>>roll_no;
}
void show()
{
cout<<"Name : "<<name<<" Roll number: "<<roll_no<<endl;
}
};
int main()
{
person *p;
p=new student;
p->get_info();
p->show();
return 0;
}
Example 2: abstract class in c++
struct Abstract {
virtual void f() = 0;
};
struct Concrete : Abstract {
void f() override {}
virtual void g();
};
struct Abstract2 : Concrete {
void g() override = 0;
};
int main()
{
Concrete b;
Abstract& a = b;
a.f();
}