Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8b55652

Browse files
abstract classes
1 parent 5e35df3 commit 8b55652

File tree

1 file changed

+40
-0
lines changed
  • Learn_CPP_Programming_Deep_Dive/Section 15 Polymorphism/Abstract_functions

1 file changed

+40
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include <iostream>
2+
3+
using namespace std;
4+
5+
class Base
6+
{
7+
public:
8+
void fun1()
9+
{
10+
cout<<"fun1() in Base"<<endl;
11+
}
12+
13+
virtual void fun2() = 0; // this is a pure virtual function -> it forces the derived classes to implement them, otherwise they remain abstract classes too
14+
};
15+
16+
17+
class Child : public Base
18+
{
19+
public:
20+
void fun2()
21+
{
22+
cout<<"fun2() in Derived"<<endl;
23+
}
24+
};
25+
26+
27+
int main(void)
28+
{
29+
Base *b = new Child();
30+
b->fun1();
31+
b->fun2();
32+
33+
// Base bb; // it is not possible to instantiate an abstract class
34+
35+
/* The purpose of inheritance is either reusability of functions or polymorphism at runtime */
36+
37+
38+
39+
return 0;
40+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /