/* Простой пример реализации лаб. 8 на основе класса string Информация о сотовых телефонах */ //---------------------- файл phone.h #define _CRT_SECURE_NO_WARNINGS #ifndef PHONE_H #define PHONE_H #include #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() { char str[100]; sprintf(str,"%dx%d", this->width, this->height); return string(str); } string Phone::getInfo() { return this->vendor + " " + this->model; } void Phone::show() { cout << endl << this->getInfo() << "," << this->getScreenSize() << "," << this->getWeight() << "," << this->getPrice(); } //---------------------- файл Source.cpp #include #include "phone.h" using namespace std; int main() { Phone ph1("Nokia","C100"); ph1.show(); Phone ph2("Siemens","S75",320,240,150,7000); ph2.show(); Phone ph3 = ph2; ph3.show(); Phone *ph4 = new Phone("LG","X500",640,480); ph4->show(); delete ph4; //т.к. создан в куче! cin.get(); return 0; }