-
Notifications
You must be signed in to change notification settings - Fork 0
/
Virtual-Function-Creation-Rule.cpp
102 lines (83 loc) · 2.1 KB
/
Virtual-Function-Creation-Rule.cpp
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <iostream>
#include <cstring>
using namespace std;
class CWH
{
protected:
string title;
float rating;
public:
CWH(string title, float rating)
{
this->title = title;
this->rating = rating;
}
virtual void display() {}
};
class CWHVideo : public CWH
{
float videoLength;
public:
CWHVideo(string s, float r, float videoLength) : CWH(s, r)
{
this->videoLength = videoLength;
}
void display()
{
cout << "Title: " << title << endl;
cout << "Ratings: " << rating << endl;
cout << "video length: " << videoLength << endl;
}
};
class CWHText : public CWH
{
float words;
public:
CWHText(string s, float r, float words) : CWH(s, r)
{
this->words = words;
}
void display()
{
cout << "Title: " << title << endl;
cout << "Ratings: " << rating << endl;
cout << "Words: " << words << endl;
}
};
int main()
{
string title;
float rating, vlen;
float words;
// for video
title = "django vid";
rating = 45.89;
vlen = 4.23;
CWHVideo vid(title, rating, vlen);
// vid.display();
// for text
title = "django text";
rating = 78.39;
words = 4.19;
CWHText tex(title, rating, words);
// tex.display();
// making base class pointers and pointing it towards derived classes object
CWH *tut[2];
tut[0] = &vid;
tut[1] = &tex;
// see the result with and without virtual keyword in Line 18
tut[0]->display();
cout << endl;
tut[1]->display();
return 0;
}
/*
Rules for virtual functions:
1- They cannot be static
2- They are accessed by object pointer
3- Virtual function can be a friend of another class
4- A fucntion in base class can might not be used like **virtual void display() in current class**
5- If a virtual function is defined in a base class, there is no necessity of redefining it in the derived class
6- If one of the derived class do not have the display function then in place of derived class, base class display
function will be called (I M SPECIFICALLY TALKING ABOUT THIS CODE)
*/