-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInternet_Archive.cpp
More file actions
120 lines (99 loc) · 2.81 KB
/
Internet_Archive.cpp
File metadata and controls
120 lines (99 loc) · 2.81 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <bits/stdc++.h>
using namespace std;
// Base Class
class InternetEvent
{
protected:
string title;
int year;
string description;
public:
// Constructor
InternetEvent(string t, int y, string d) {
title = t;
year = y;
description = d;
}
// Use of virtual function (Polymorphism)
virtual void displayDetails() = 0;
// Getters (Encapsulation)
int getYear() {
return year;
}
string getTitle() {
return title;
}
};
// Derived class-1 (Inheritance)
class TechLaunchEvent : public InternetEvent
{
public:
TechLaunchEvent(string t, int y, string d)
: InternetEvent(t, y, d) {}
void displayDetails() override {
cout << "\033[34m[Tech Launch] \033[0m" << title << " (" << year << ")\n" << description << "\n";
cout << "\033[34m------------------------------\033[0m\n";
}
};
// Derived class-2 (Inheritance)
class MemeEvent : public InternetEvent
{
public:
MemeEvent(string t, int y, string d)
: InternetEvent(t, y, d) {}
void displayDetails() override {
cout << "\033[32m[Meme] \033[0m" << title << " (" << year << ")\n" << description << "\n";
cout << "\033[32m------------------------------\033[0m\n";
}
};
// Archive class (Encapsulation)
class Archive
{
private:
vector<shared_ptr<InternetEvent>> events;
public:
void addEvent(shared_ptr<InternetEvent> ev) {
events.push_back(ev);
}
void displayAll() const {
for (const auto &e : events)
e->displayDetails(); // Polymorphic call
}
// Load from file (File Handling)
bool loadFromFile(const string &filename) {
ifstream file(filename);
if (!file.is_open())
return false;
string line, title, description;
int year;
bool isMeme = false;
while (getline(file, title)) {
if (title.empty())
continue;
getline(file, line);
stringstream ss(line);
ss >> year;
getline(file, description);
getline(file, line); // Consume empty line
// find: is there "Meme" string present in title or not
if (title.find("Meme") != string::npos)
addEvent(make_shared<MemeEvent>(title, year, description));
else
addEvent(make_shared<TechLaunchEvent>(title, year, description));
}
file.close();
return true;
}
};
// Main function
int main()
{
Archive archive;
if (archive.loadFromFile("events.txt")) {
cout << "Events Loaded Successfully!\n\n";
archive.displayAll();
}
else {
cout << "Failed to open file.\n";
}
}