//---------------------- файл main.h #ifndef PHONE_H #define PHONE_H /*информация о сотовых телефонах: производитель, модель, разрешение, вес, цена. */ class Phone { private: char *vendor; char *model; int width, height, weight; float price; public: Phone(char *vendor, char *model, int width = 0, int height = 0, int weight = 0, float price = 0.); Phone(Phone &from); virtual ~Phone(); char *getInfo(); //производитель+модель void setScreenSize(int width, int height); char *getScreenSize(); int getWeight() { return weight; } void setWeight(int weight) { this->weight = weight; } float getPrice() { return price; } void setPrice(float price) { this->price = price; } virtual void show(); }; Phone::Phone(char *vendor, char *model, int width, int height, int weight, float price) { this->vendor = new char[strlen(vendor) + 1]; strcpy(this->vendor, vendor); this->model = new char[strlen(model) + 1]; strcpy(this->model, model); setScreenSize(width, height); setWeight(weight); setPrice(price); } Phone::Phone(Phone &From) { this->vendor = new char[strlen(From.vendor) + 1]; strcpy(this->vendor, From.vendor); this->model = new char[strlen(From.model) + 1]; strcpy(this->model, From.model); this->setScreenSize(From.width, From.height); this->setWeight(From.weight); this->setPrice(From.price); } void Phone::setScreenSize(int width, int height) { this->width = width; this->height = height; } Phone::~Phone() { delete[] model; delete[] vendor; } char * Phone::getScreenSize() { char *buf = new char[10]; sprintf(buf, "%dx%d", this->width, this->height); return buf; } char * Phone::getInfo() { char *buf = new char[strlen(this->vendor) + strlen(this->model) + 2]; sprintf(buf, "%s %s", this->vendor, this->model); return buf; } void Phone::show() { printf("\n"); char *info = this->getInfo(); puts(info); printf(" (%s, %d, %.2f)", this->getScreenSize(), this->getWeight(), this->getPrice()); } class Communicator : public Phone { protected: char *os; public: inline Communicator(char *vendor, char *model, int width = 0, int height = 0, int weight = 0, float price = 0., char *os = NULL) : Phone(vendor, model, width, height, weight, price) { setOS(os); } void setOS(char *os) { if (os) { this->os = new char[strlen(os) + 1]; strcpy(this->os, os); } } Communicator(Communicator &from) : Phone(from) { setOS(from.os); } ~Communicator() { delete[] os; } char * getOS() { return this->os; } void show() { this->Phone::show(); printf(" %s", this->getOS()); } friend Communicator random_communcator(); }; #endif //---------------------- файл main.cpp #define _CRT_SECURE_NO_WARNINGS #include #include #include #include #include #include "main.h" using namespace std; Communicator random_communcator() { //функция-друг - вернет коммуникатор со случайным производителем char *marks[3] = { "Nokia","Philips","LG" }; Communicator c(marks[rand() % 3], "", 0, 0, 0, 0, ""); return c; } int main(void) { setlocale(LC_ALL, "Russian"); Communicator c0("LG", "500", 90, 125, 200, 10500, "Android"); c0.show(); //создание непосредственное Communicator *c1 = new Communicator("Dell", "Streak", 0, 0, 0, 0, "Android"); //создание через new + указатель c1->show(); Communicator c2 = *c1; //присваивание объектов c2.show(); delete c1; //удаление объектов //a с0 так удалять нельзя - он в стеке, а не в куче Communicator c3 = random_communcator(); //возврат объекта из функции void (Communicator:: *pointer)(void) = &Communicator::show; (c3.*pointer)(); //вызов метода через указатель на функцию //виртуальный метод - дополнили show родителя cout << endl; system("pause"); return 0; } ////////////////////////////////////////////////////////////////////////// /* Версия этого проекта с описанием каждого класса в отдельной паре файлов .h и .cpp и с использованием строк string вместо char * (это позволяет значительно упростить код, связанный с управлением памятью). Обратите внимание, что имена .cpp и .h файлов должны совпадать с именами описанных в них классов */ ////////////////////////////////////////////////////////////////////////// //--- Файл main.cpp /* Вариант 25. Информация о сотовых телефонах: производитель, модель, разрешение, вес, цена. */ #define _CRT_SECURE_NO_WARNINGS #include #include #include #include #include "phone.h" #include "communicator.h" using namespace std; int main(void) { setlocale(LC_ALL, "Russian"); SetConsoleCP(1251); SetConsoleOutputCP(1251); Communicator c0("LG", "500", 90, 125, 200, 10500, "Android"); c0.show(); //создание непосредственное Communicator *c1 = new Communicator("Dell", "Streak", 0, 0, 0, 0, "Android"); //создание через new + указатель c1->show(); Communicator c2 = *c1; //присваивание объектов c2.show(); delete c1; //удаление объектов //a с0 так удалять нельзя - он в стеке, а не в куче Communicator c3 = random_communcator(); //возврат объекта из функции void (Communicator:: *pointer)(void) = &Communicator::show; (c3.*pointer)(); //вызов метода через указатель на функцию cout << endl; system("pause"); return 0; } ////////////////////////////////////////////////////////////////////////// //--- Файл phone.h #ifndef PHONE_H #define PHONE_H #include #include using namespace std; class Phone { protected: string vendor, model; int width, height, weight; float price; public: Phone(string vendor, string model, int width = 0, int height = 0, int weight = 0, float price = 0.); Phone(Phone &from); virtual ~Phone(); string getInfo(); //производитель+модель void setScreenSize(int width, int height); string getScreenSize(); inline int getWeight() { return weight; } inline void setWeight(int weight) { this->weight = weight; } inline float getPrice() { return price; } inline void setPrice(float price) { this->price = price; } virtual void show(); }; #endif ////////////////////////////////////////////////////////////////////////// //--- Файл phone.cpp #include "phone.h" Phone::Phone(string vendor, string model, int width, int height, int weight, float price) { this->vendor = vendor; this->model = model; setScreenSize(width, height); setWeight(weight); setPrice(price); } Phone::Phone(Phone &From) { this->vendor = From.vendor; this->model = From.model; this->setScreenSize(From.width, From.height); this->setWeight(From.weight); this->setPrice(From.price); } void Phone::setScreenSize(int width, int height) { this->width = width; this->height = height; } Phone::~Phone() {} string Phone::getScreenSize() { return string(this->width + "x" + this->height); } string Phone::getInfo() { return this->vendor + " " + this->model; } void Phone::show() { cout << endl << this->getInfo() << "," << this->getScreenSize() << "," << this->getWeight() << "," << this->getPrice(); } ////////////////////////////////////////////////////////////////////////// //--- Файл communicator.h #ifndef COMMUNICATOR_H #define COMMUNICATOR_H #include #include #include "phone.h" using namespace std; class Communicator : public Phone { protected: string os; public: inline Communicator(string vendor, string model, int width = 0, int height = 0, int weight = 0, float price = 0., string os = "") : Phone(vendor, model, width, height, weight, price) { setOS(os); } inline void setOS(string os) { this->os = os; } inline Communicator(Communicator &from) : Phone(from) { setOS(from.os); } ~Communicator() {} inline string getOS() { return this->os; } void show(); friend Communicator random_communcator(); }; #endif ////////////////////////////////////////////////////////////////////////// //--- Файл communicator.cpp #include "communicator.h" void Communicator::show() { this->Phone::show(); cout << "," << this->getOS(); } Communicator random_communcator() { //функция-друг - вернет коммуникатор со случайным производителем string marks[3] = { string("Nokia"), string("Philips"), string("LG") }; Communicator c(marks[rand() % 3], string(""), 0, 0, 0, 0, string("")); return c; }