-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path57_virtual_function.cpp
72 lines (61 loc) · 1.48 KB
/
57_virtual_function.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
#include<iostream>
#include<cstring>
using namespace std;
class Content{
protected:
string title;
float rating;
public:
Content(string s, float r){
title = s;
rating = r;
}
virtual void display(){}
};
class Video : public Content{
float videolength;
public:
Video(string s, float r, float vl) : Content(s, r){
videolength = vl;
}
void display(){
cout<<"The title of the video is: "<<title<<endl;
cout<<"Rating: "<<rating<<" out of 5 stars"<<endl;
cout<<"Video Length: "<<videolength<<" minutes"<<endl;
}
};
class Text : public Content{
int words;
public:
Text(string s, float r, int w) : Content(s, r){
words = w;
}
void display(){
cout<<"The title of the text is: "<<title<<endl;
cout<<"Rating: "<<rating<<" out of 5 stars"<<endl;
cout<<"The words count is: "<<words<<" words"<<endl;
}
};
int main(){
string title;
float rating, vlen;
int words;
//for video content
title="C++ tutorial";
vlen=15.42;
rating=4.85;
Video vd(title, rating, vlen);
// vd.display();
//for text content
title="C++ tutorial notes";
words=2165;
rating=4.26;
Text txt(title, rating, words);
// txt.display();
Content * cptr[2];
cptr[0] = &vd;
cptr[1] = &txt;
cptr[0]->display();
cptr[1]->display();
return 0;
}