-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFILE22.cpp
More file actions
62 lines (55 loc) · 1.17 KB
/
FILE22.cpp
File metadata and controls
62 lines (55 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
Let us look at another program that makes use of virtual functions.
In the program shown below we will use one base class from which we have one publicly derived class.
The program is an example of how text can be generated in a desired format.
The derived class overrides only certain functions of the base class.
*/
#include <iostream>
using namespace std;
class Format
{
public:
void display_form();
virtual void header()
{
cout << "This is a header" << endl;
}
virtual void body()
{
cout << " This is body text" << endl;
}
virtual void footer()
{
cout << "This is a footer" << endl << endl;
}
};
void Format::display_form()
{
header();
for(int index = 0; index < 3; index++)
{
body();
}
footer();
}
//This class overrides two of the virtual methods of the base class
class MyForm:public Format
{
void header()
{
cout << "This is the new header" << endl;
}
void footer()
{
cout << "This is the new footer" << endl;
}
};
int main()
{
Format* first_form = new Format;
first_form->display_form(); // A call to the base class
delete first_form;
first_form = new MyForm;
first_form->display_form(); // A call to the derived class
return 0;
}