Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- main.cpp
- //--------------------------------
- #include "parentClass.h"
- #include "ChildClass.h"
- #include <vector>
- int main()
- {
- std::vector<ParentClass*> objVector;
- for (int i = 0; i < 3; i++)
- {
- objVector.push_back(new ParentClass(i));
- }
- for (int i = 0; i < 2; i++)
- {
- objVector.push_back(new ChildClass(0, i + 3));
- }
- int choise;
- std::cout << "Enter id: ";
- std::cin >> choise;
- if (choise < 0 || choise >4)
- {
- exit(1);
- }
- for (auto it = objVector.begin(); it != objVector.end(); it++)
- {
- if ((*it)->GetId() == choise) //пошук в векторі
- {
- (*it)->ReadFromFile("pathToName.txt"); //зчитуємо імя з файлу
- (*it)->Print();
- std::cout << "Enter new name: ";
- std::string tmpName;
- std::cin >> tmpName;
- (*it)->SetName(tmpName); //міняємо його
- }
- }
- std::ofstream outfile;
- outfile.open("results.txt");
- for (auto& it : objVector) //вивід в файл і на екран
- {
- it->Print();
- outfile << *it;
- }
- outfile.close();
- return 0;
- }
- //--------------------------------
- ChildClass.h
- //--------------------------------
- #pragma once
- #include "parentClass.h"
- #include <fstream>
- #include <iostream>
- class ChildClass : public ParentClass
- {
- protected:
- int newField;//нове поле відносно батьківського
- public:
- ChildClass()
- {
- newField = 0;
- }
- ChildClass(int value, int id)
- {
- name = "Empty";
- this->id = new int(id);
- newField = value;
- }
- ChildClass(const ChildClass& that)
- {
- this->name = that.name;
- this->id = new int(*that.id);//id вказіник і шоб скопіювати значення а не адресу робимо так
- this->newField = that.newField;
- }
- ~ChildClass()
- {
- delete id;
- std::cout << "Object " << name << " has been destructed\n";
- }
- void Print()
- {
- std::cout << "Name: " << name << " ID: " << *id << "\tSometring new..." << std::endl;
- }
- friend std::ostream& operator << (std::ostream& os, const ChildClass& child)
- {
- //метод відображення обєкту в файл + перегружений оператор(бінарний)
- return os << "Name: " << child.name << " ID: " << *child.id << "\tSometring new..." << std::endl;;
- }
- //Методи редагування змісту обєкту???
- void SetNewField(int value)
- {
- newField = value;
- }
- int GetNewField() //новий метод відносно батьківстького
- {
- return newField;
- }
- };
- //--------------------------------
- parentClass.h
- //--------------------------------
- #pragma once
- #include "abstractClass.h"
- #include <string>
- #include <fstream>
- #include <iostream>
- class ParentClass :public AbstractClass
- {
- protected:
- std::string name; //назва обєкту
- int* id; //код обєкту
- public:
- ParentClass()
- {
- id = new int(0);
- }
- ParentClass(int value)
- {
- name = "Empty";
- id = new int(value);
- }
- ParentClass(const ParentClass& that)
- {
- this->name = that.name;
- this->id = new int(*that.id);//id вказіник і шоб скопіювати значення а не адресу робимо так
- }
- ~ParentClass()
- {
- delete id;
- std::cout << "Object " << name << " has been destructed\n";
- }
- void ReadFromFile(std::string path) override
- {
- std::ifstream inFile;
- inFile.open(path);
- if (inFile.is_open())
- {
- inFile >> name >> *id;
- inFile.close();
- }
- }
- void Print()//метод відображення обєкту на екран
- {
- std::cout << "Name: " << name << " ID: " << *id << std::endl;
- }
- friend std::ostream& operator << (std::ostream& os, const ParentClass& parent)
- {
- //метод відображення обєкту в файл + перегружений оператор(бінарний)
- return os << "Name: " << parent.name << " ID: " << *parent.id << std::endl;;
- }
- //Методи редагування змісту обєкту???
- void SetName(std::string name)
- {
- this->name = name;
- }
- std::string GetName()
- {
- return this->name;
- }
- void SetId(int id)
- {
- *(this->id) = id;
- }
- int GetId()
- {
- return *id;
- }
- };
- //--------------------------------
- abstractClass.h
- //--------------------------------
- #pragma once
- #include <string>
- class AbstractClass
- {
- public:
- virtual void ReadFromFile(std::string path) = 0; //чисто віртуальна функція
- };
Add Comment
Please, Sign In to add comment