Vector and Template
Feb 26, 2021 at 1:20am UTC
Hello everyone.
I am creating a lib. In this lib I need to create a template where the T object (class) will be added inside the vector.
I have questions about a code I created to work with this data structure. This data structure will create a large xml that has a parent and children, where many of them are containers.
The code I am using is working perfectly, but I have doubts if this is the correct way.
This code is intended to demonstrate the template class "Container". The only purpose is to have some idea of how to work with containers inside containers in two or more levels.
Does anyone have a better idea?
Thank you all.
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 121 122 123 124 125 126 127 128 129 130 131 132 133
#include <iostream>
#include <vector>
#include <memory>
#include <string>
using namespace std;
enum class Tvehicle {none = -1, bike, car, suv, truck};
template <class T>
class Container
{
public :
vector <T> items;
T obj;
public :
Container(){}
~Container(){ items.clear(); }
void add();
};
template <class T>
void Container<T>::add()
{
items.push_back(obj);
obj.clear();
}
//------------------------------------------------
class People
{
string m_name;
string m_lastName;
int m_age;
public :
People(){}
void clear() {
this ->m_name.clear();
this ->m_lastName.clear();
this ->m_age = 0;
}
string get_name() const { return this ->m_name; }
void set_name(const string &name) { this ->m_name = name; }
string get_lastName() const { return this ->m_lastName; }
void set_lastName(const string &lastName) { this ->m_lastName = lastName; }
int get_age(){ return this ->m_age; }
void set_age(const int age){ this ->m_age = age; }
};
//------------------------------------------------
class Property {
int m_bedroom;
public :
Property (){}
void clear() { this ->m_bedroom = 0; }
int get_bedroom(){ return this ->m_bedroom; }
void set_bedroom(const int bedroom){ this ->m_bedroom = bedroom; }
};
//------------------------------------------------
class Vehicle{
int m_year;
int m_power;
Tvehicle m_type;
public :
Vehicle(){}
void clear() {
this ->m_year = 0;
this ->m_power = 0;
this ->m_type = Tvehicle::none;
}
int get_year() const { return this ->m_year; }
void set_year(const int &year) { this ->m_year = year; }
int get_power() const { return this ->m_power; }
void set_power(const int &power) { this ->m_power = power; }
Tvehicle get_type() const { return this ->m_type; }
void set_type(const Tvehicle &type) { this ->m_type = type; }
};
//------------------------------------------------
class Employee: public People{
public :
Employee(){}
void clear() {
this ->child.obj.clear();
this ->child.items.clear();
this ->property.obj.clear();
this ->property.items.clear();
this ->vehicle.obj.clear();
this ->vehicle.items.clear();
}
Container<People> child;
Container<Property> property;
Container<Vehicle> vehicle;
};
//------------------------------------------------
class Company: public People{
int m_department;
public :
Company(){}
void clear() {
this ->set_age(0);
this ->m_department = 0;
this ->employee.obj.clear();
this ->employee.items.clear();
this ->vehicle.obj.clear();
this ->vehicle.items.clear();
}
int get_foundation(){ return this ->get_age(); }
void set_foundation(const int foundation){ this ->set_age(foundation); }
int get_department() const { return this ->m_department; }
void set_department(const int &department) { this ->m_department = department; }
Container<Employee> employee;
Container<Vehicle> vehicle;
};
//------------------------------------------------
class Corporation{
string m_name;
public :
Corporation(){}
void clear() {
this ->m_name.clear();
this ->company.obj.clear();
this ->company.items.clear();
}
string get_name() const { return this ->m_name; }
void set_name(const string &name) { this ->m_name = name; }
Container<Company> company;
};
Last edited on Feb 26, 2021 at 4:26am UTC
Feb 26, 2021 at 1:21am UTC
As the code is very large, the main function of the code above is in this post.
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 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
int main(int argc, char **argv)
{
//create corporation
shared_ptr<Corporation> corp(new Corporation);
corp->set_name("Mint" );
//company 1
corp->company.obj.set_name("COMP1" );
corp->company.obj.set_foundation(1980);
corp->company.obj.set_department(10);
//company1/employee1
corp->company.obj.employee.obj.set_name("Mary" );
corp->company.obj.employee.obj.set_lastName("Helix" );
corp->company.obj.employee.obj.set_age(35);
//employee1/child1
corp->company.obj.employee.obj.child.obj.set_name("Kya" );
corp->company.obj.employee.obj.child.obj.set_age(10);
corp->company.obj.employee.obj.child.add();
//employee1/child2
corp->company.obj.employee.obj.child.obj.set_name("Poul" );
corp->company.obj.employee.obj.child.obj.set_age(15);
corp->company.obj.employee.obj.child.add();
//employee1/property
corp->company.obj.employee.obj.property.obj.set_bedroom(4);
corp->company.obj.employee.obj.property.add();
//employee1/vehicle1
corp->company.obj.employee.obj.vehicle.obj.set_type(Tvehicle::car);
corp->company.obj.employee.obj.vehicle.obj.set_year(2018);
corp->company.obj.employee.obj.vehicle.add();
//employee1/vehicle2
corp->company.obj.employee.obj.vehicle.obj.set_type(Tvehicle::suv);
corp->company.obj.employee.obj.vehicle.obj.set_year(2020);
corp->company.obj.employee.obj.vehicle.add();
corp->company.obj.employee.add();//add employee
//employee2
corp->company.obj.employee.obj.set_name("Freddie" );
corp->company.obj.employee.obj.set_lastName("Allen" );
corp->company.obj.employee.obj.set_age(32);
//employee1/child1
corp->company.obj.employee.obj.child.obj.set_name("John" );
corp->company.obj.employee.obj.child.obj.set_age(8);
corp->company.obj.employee.obj.child.add();
//employee1/property
corp->company.obj.employee.obj.property.obj.set_bedroom(3);
corp->company.obj.employee.obj.property.add();
//employee1/vehicle1
corp->company.obj.employee.obj.vehicle.obj.set_type(Tvehicle::car);
corp->company.obj.employee.obj.vehicle.obj.set_year(2015);
corp->company.obj.employee.obj.vehicle.add();
//employee1/vehicle2
corp->company.obj.employee.obj.vehicle.obj.set_type(Tvehicle::bike);
corp->company.obj.employee.obj.vehicle.obj.set_year(2019);
corp->company.obj.employee.obj.vehicle.add();
corp->company.obj.employee.add();//add employee
//company1/vehicle
corp->company.obj.vehicle.obj.set_type(Tvehicle::bike);
corp->company.obj.vehicle.obj.set_year(2020);
corp->company.obj.vehicle.add();
corp->company.obj.vehicle.obj.set_type(Tvehicle::car);
corp->company.obj.vehicle.obj.set_year(2020);
corp->company.obj.vehicle.add();
corp->company.add();//add company1
//company 2
corp->company.obj.set_name("COMP2" );
corp->company.obj.set_foundation(1988);
corp->company.obj.set_department(20);
//company1/employee1
corp->company.obj.employee.obj.set_name("Ravena" );
corp->company.obj.employee.obj.set_lastName("fux" );
corp->company.obj.employee.obj.set_age(36);
//employee1/child
corp->company.obj.employee.obj.child.obj.set_name("Julia" );
corp->company.obj.employee.obj.child.obj.set_age(13);
corp->company.obj.employee.obj.child.add();
//employee1/property
corp->company.obj.employee.obj.property.obj.set_bedroom(4);
corp->company.obj.employee.obj.property.add();
//employee1/vehicle
corp->company.obj.employee.obj.vehicle.obj.set_type(Tvehicle::suv);
corp->company.obj.employee.obj.vehicle.obj.set_year(2020);
corp->company.obj.employee.obj.vehicle.add();
corp->company.obj.employee.add();//add employee
//employee2
corp->company.obj.employee.obj.set_name("Freddie" );
corp->company.obj.employee.obj.set_lastName("Allen" );
corp->company.obj.employee.obj.set_age(25);
//employee1/child
//no
//employee1/property
corp->company.obj.employee.obj.property.obj.set_bedroom(2);
corp->company.obj.employee.obj.property.add();
//employee1/vehicle
corp->company.obj.employee.obj.vehicle.obj.set_type(Tvehicle::bike);
corp->company.obj.employee.obj.vehicle.obj.set_year(2019);
corp->company.obj.employee.obj.vehicle.add();
corp->company.obj.employee.add();//add employee
//company1/vehicle
corp->company.obj.vehicle.obj.set_type(Tvehicle::truck);
corp->company.obj.vehicle.obj.set_year(2020);
corp->company.obj.vehicle.add();
corp->company.add();//add company2
cout << "Corporation: " << corp->get_name() << endl;
cout << "total companies: " << corp->company.items.size() << endl;
cout << "\n" ;
for (auto c = corp->company.items.begin(); c != corp->company.items.end(); c++) {
cout << "Company: " << c->get_name() << endl;
cout << "Fundation: " << c->get_foundation() << endl;
cout << "Department: " << c->get_department() << endl;
//employee
for (auto em = c->employee.items.begin(); em != c->employee.items.end(); em++) {
cout << "...Company/Employee: " << endl;
cout << "...name: " << em->get_lastName() << ", " << em->get_name() << endl;
cout << "...age: " << em->get_age() << endl;
//child
for (auto ch = em->child.items.begin(); ch != em->child.items.end(); ch++) {
cout << "......Employee/Children: " << endl;
cout << "......name: " << ch->get_name() << endl;
cout << "......age: " << ch->get_age() << endl;
}
for (auto pr = em->property.items.begin(); pr != em->property.items.end(); pr++) {
cout << "......Employee/Property: " << endl;
cout << "......Bedroom: " << pr->get_bedroom() << endl;
}
for (auto ve = em->vehicle.items.begin(); ve != em->vehicle.items.end(); ve++) {
cout << "......Employee/Vehicle: " << endl;
cout << "......Type: " << (int )ve->get_type() << endl;
cout << "......Year: " << ve->get_year() << endl;
}
cout << "\n" ;
}
for (auto ve = c->vehicle.items.begin(); ve != c->vehicle.items.end(); ve++) {
cout << "...Company/Vehicle: " << endl;
cout << "...Type: " << (int )ve->get_type() << endl;
cout << "...Year: " << ve->get_year() << endl;
}
cout <<"--------------------" << endl;
cout << "\n" ;
}
corp->clear();//clear all
return 0;
}
Topic archived. No new replies allowed.