-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path52virtualfnc_crrules.cpp
More file actions
83 lines (71 loc) · 1.72 KB
/
Copy path52virtualfnc_crrules.cpp
File metadata and controls
83 lines (71 loc) · 1.72 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <iostream>
#include <cstring>
using namespace std;
class CWH
{
protected:
string title;
float rating;
public:
CWH(string s, float r)
{
title = s;
rating = r;
}
virtual void display() {}
};
class videoLectures : public CWH
{
float videolen;
public:
videoLectures(string s, float r, float vl) : CWH(s, r)
{
videolen = vl;
}
void display()
{
cout << "Video Title : " << title << endl
<< "Video Ratings : " << rating << " out of 5 " << endl
<< "Length : " << videolen << endl;
}
};
class websiteLectures : public CWH
{
int words;
public:
websiteLectures(string s, float r, int wc) : CWH(s, r)
{
words = wc;
}
void display()
{
cout << "Website Page : " << title << endl
<< "Page Ratings : " << rating << " out of 5" << endl
<< "Words : " << words << endl;
}
};
int main()
{
string name = "C++ Tutorials";
float rate = 4.3, duration = 15.23;
int count = 514;
videoLectures v1(name, rate, duration);
v1.display();
websiteLectures w1(name, rate, count);
w1.display();
CWH * base_ptr[2];
base_ptr[0] = &v1;
base_ptr[1] = &w1;
base_ptr[0]->display();
base_ptr[1]->display();
return 0;
}
/*
RULES FOR VIRTUAL FUNCTIONS
1) They cannot be static
2) They are only accessed by class pointer otherwise they have no use
3) They can be present in a code without any use like the code above
4) They can be a friend function for a class
5) If a pointer doesn't find the function in a derived class, it will by default fetch the base class function as an alternative
instead of throwing any error
*/