Case Study: Building a Polymorphic Database System
Here's a case study of building a polymorphic database system in C++. This database system allows the user to store different types of objects (e.g. integers, strings, dates) in a single container. The system uses polymorphism, which is one of the key concepts of object-oriented programming, to achieve this functionality.
Consider the following code example:
#include <iostream>
#include <vector>
class Object {
public:
virtual void print() = 0;
};
class Integer : public Object {
public:
int value;
void print() {
std::cout << value << std::endl;
}
};
class String : public Object {
public:
std::string value;
void print() {
std::cout << value << std::endl;
}
};
class Date : public Object {
public:
int day;
int month;
int year;
void print() {
std::cout << day << "/" << month << "/" << year << std::endl;
}
};
class Database {
public:
std::vector<Object*> objects;
void add(Object *obj) {
objects.push_back(obj);
}
void printAll() {
for (int i = 0; i < objects.size(); i++) {
objects[i]->print();
}
}
};
int main() {
Database db;
Integer i1, i2;
i1.value = 5;
i2.value = 10;
db.add(&i1);
db.add(&i2);
String s1, s2;
s1.value = "Hello";
s2.value = "World";
db.add(&s1);
db.add(&s2);
Date d1, d2;
d1.day = 1;
d1.month = 1;
d1.year = 2021;
d2.day = 2;
d2.month = 2;
d2.year = 2022;
db.add(&d1);
db.add(&d2);
db.printAll();
return 0;
}
In this example, we define a base class called Object
which contains a virtual function print()
. This function is defined as a pure virtual function, which means that it must be overridden by any derived class that wants to use it.
We then define three derived classes called Integer
, String
, and Date
, each of which inherits from the Object
class. These classes override the print()
function to display their respective values in the desired format.
We also define a class called Database
which contains a vector of Object
pointers called objects
. The add()
function is used to add objects to the database, and the printAll()
function is used to print all objects in the database.
In the main function, we create instances of Integer
, String
, and Date
objects and add them to the database. Finally, we call the printAll()
function to display the objects in the database.
The output of this program would be:
5 10 Hello World 1/1/2021 2/2/2022
In this example, we can see the power of polymorphism in action. The Database
class doesn't need to know the specific type of object it's storing, it only needs to know that it's storing objects that have a print()
function. This allows the database to store different types of objects in a single container, making it much more flexible and reusable.
It's important to note that the Database
class uses pointer objects to store objects in the vector. This is because Object
is an abstract class and can't be instantiated directly. Using pointers allows us to store objects of derived classes in the objects
vector.
In conclusion, this case study demonstrates how polymorphism can be used to build a flexible and reusable database system in C++. By using inheritance and virtual functions, we were able to store different types of objects in a single container, while still being able to display their values in the desired format.