Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Triangle N5
//Microsoft (R) C/C++ Optimizing Compiler Version 19.00.23506 for x64 //https://rextester.com/TSIOJ56736 #include <iostream> #include <string> #include <vector> using namespace std; //template <typename Type> typedef int Type; // используем псевдоним для типа координат class Point{ private: vector <Type>_x; string _name; public: static int count; static int dimension; //размерность всех точек void setX(vector <Type> &newX){ _x.assign(newX.begin(),newX.end());// копирование каждого элемента } void setName(string newName){ _name=newName;} const vector<Type>& getX()const{ //возвращает постоянную ссылку на вектор return _x;} string getName(){ return _name;} void print(){ cout<<" Point "<<getName()<<" ("; /*for (Type xi: _x)// все элементы cout<<xi<<" ,";*/ vector<Type>::iterator xi=_x.begin(); cout<<(*xi); xi++; for (; xi<_x.end();xi++) cout<<", "<<(*xi); cout<<"); ";} void askPoint(){ cout<<"? Point name, x, y?"; cin>>_name; Type newX; for(int i=0;i<Point::dimension;i++) { cin>>newX; _x.push_back(newX); } // print();} Point(): _name("A") {//конструктор по умолчанию (без параметров) count++; cout<<"create1 Point "<<_name<<endl;} Point(string name){//конструктор с параметрами ++count; //setXY(x,y); setName(name); cout<<"create2 Point "<<name<<endl;} ~Point (){//деструтор //--count; cout<<"~del Point "<<getName()<<endl;} }; int Point::count=0; int Point::dimension=2; class Figure{ protected: float _perimeter; float _area; string _name; static int _count; public: Figure(){ _name="A"; ++_count; cout<<"create1 Figure A #"<<_count<<"\n" ; } Figure(int i) { ++_count; string name = to_string(i); _name=name; cout<<"create2 Figure "<<name<<" #"<< _count<<endl ; } string getName(){ return _name;} float getArea(){ return _area;} float getPerimeter(){ return _perimeter;} void setArea(float newArea){ _area=newArea;} void setPerimeter(float newPerimeter){ _perimeter=newPerimeter;} ~Figure(){ --_count; cout<<"~del Figure "<<getName()<<endl ;} virtual float calcPerimeter()=0;// чисто виртуальная virtual float calcArea()=0; /*virtual float calcPerimeter(); virtual float calcArea();*/ virtual void print(){// виртулаьная функция cout<< getName()<<" ";} }; int Figure::_count=0; class Triangle: public Figure{ private: Point* _x1, _x2,_x3; public: Triangle(){ _x1=new Point(); _x1->askPoint(); _x2.askPoint(); _x3.askPoint(); cout<<"create Triangle" << getName()<<endl; } Triangle(int i) { //Triangle(); //ошибка, создали треугольник, но никуда не присвоили string name = to_string(i); _name=name; cout<<"create Triangle "<<name<<endl ; } Triangle(Point x1,Point x2, Point x3){ _x1=new Point(x1); _x2=x2; _x3=x3;} ~Triangle(){ cout<<"~del Triandle "<<getName()<<endl ;} float calcPerimeter(){ cout<<"calc P Triangle "; _perimeter=20;// лучше использовать setPerimeter() return _perimeter;} float calcArea(){ cout<<"calcArea Traingle"; setArea(1.5); return _area;} void print(){ Figure::print();//вызов родителя _x1->print(); _x2.print(); _x3.print(); cout<<"\n";} }; void createAnyFigure(Figure* &newFigure){ int choose=1; cout<<"1-Trianfle, 2-Quardrangle,3-Circle,Another- you choose №1"; cin>>choose; cout<<choose<<endl; switch (choose){ case 1:{ newFigure = new Triangle(); break;} case 2:{ newFigure = new Triangle(); break;} case 3:{ newFigure = new Triangle(); break;} default: newFigure = new Triangle(); };//end switch }; int main() { std::cout << "Hello, world!\n"; /*Создание треугольников, четырех угольников и ругов по переменке*/ Figure* a1; createAnyFigure(a1); a1->print(); cout<<"Point = "<<Point::count; cout<<endl<<endl<<typeid(*a1).name(); if (typeid(*a1).name()=="class Triangle") cout<<"Triangle"; }
run
|
edit
|
history
|
help
0
What is the fastest/most efficient way to find the highest set bit (msb) in an integer in C?
Dices by GOOSE
MSVC alias template
#30.1
Chord Note Finder
Computing factorial of an integer with recursion and iteration [EDIT]
variable template not supported
wrong up
#19
C++ standard violation: [templates][explicit instantiation][access checking]