1//Code by Soumyadeep Ghosh
2//insta : @soumyadepp
3//linked in : https://www.linkedin.com/in/soumyadeep-ghosh-90a1951b6/
4#include <bits/stdc++.h>
5
6using namespace std;
7
8class person
9{
10 string p_id;
11 public:
12 virtual void get_info()=0; //declaring person as abstract class
13 virtual void show()=0;
14};
15
16class student:public person
17{
18 string name;
19 int roll_no;
20 public:
21 /*overriding the pure virtual function declared in base class otherwise
22 this class will become an abstract one and then objects cannot be created
23 for the same*/
24 void get_info()
25 {
26 cout<<"Enter name of the student "<<endl;
27 cin>>name;
28 cout<<"Enter roll number of the student "<<endl;
29 cin>>roll_no;
30 }
31 void show()
32 {
33 cout<<"Name : "<<name<<" Roll number: "<<roll_no<<endl;
34 }
35};
36
37int main()
38{
39 person *p;
40 p=new student;
41 p->get_info();
42 p->show();
43 return 0;
44}
45
1struct Abstract {
2 virtual void f() = 0; // pure virtual
3}; // "Abstract" is abstract
4
5struct Concrete : Abstract {
6 void f() override {} // non-pure virtual
7 virtual void g(); // non-pure virtual
8}; // "Concrete" is non-abstract
9
10struct Abstract2 : Concrete {
11 void g() override = 0; // pure virtual overrider
12}; // "Abstract2" is abstract
13
14int main()
15{
16 // Abstract a; // Error: abstract class
17 Concrete b; // OK
18 Abstract& a = b; // OK to reference abstract base
19 a.f(); // virtual dispatch to Concrete::f()
20 // Abstract2 a2; // Error: abstract class (final overrider of g() is pure)
21}
1struct Abstract
2{
3 virtual ~Abstract() = 0;
4};
5
6Abstract::~Abstract() {}
7
8struct Valid: public Abstract
9{
10 // Notice you don't need to actually overide the base
11 // classes pure virtual method as it has a default
12};
13
14
15int main()
16{
17 // Abstract a; // This line fails to compile as Abstract is abstract
18 Valid v; // This compiles fine.
19}