Добавил:
Studfiles2
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:Лабораторная работа 11 / lab1_1 / lab1_1
.cpp// lab1_1.cpp : Defines the entry point for the console application.
//
#include <conio.h>
#include <stdio.h>
#include <process.h>
#include <ctime>
#include "stdafx.h"
#include "TextInTriangle.h"
#include "utils.h"
#include <string>
#include <iostream>
#include <map>
using namespace std;
void clrscr(){
system("cls");
}
void printShapeObjectStats(){
cout << "-----" << endl;
cout << "Shape: " << Shape::getNumberOfObjects() << endl;
cout << "Point: " << Point::getNumberOfObjects() << endl;
cout << "Line: " << Line::getNumberOfObjects() << endl;
cout << "Triangle: " << Triangle::getNumberOfObjects() << endl;
cout << "Text: " << Text::getNumberOfObjects() << endl;
cout << "TextInTriangle: " << TextInTriangle::getNumberOfObjects() << endl;
cout << "-----" << endl;
}
Shape* createRandomShape(){
int random = (rand()%4)+1;
switch(random){
case 1:
return new Point();
case 2:
return new Line();
case 3:
return new Triangle();
case 4:
return new TextInTriangle();
default:
return new Point();
}
}
int main(int argc, char* argv[])
{
clrscr();
Point* p7 = new Point(8,7);
Point* p8 = new Point(0,3);
Point* p9 = new Point(-1,0);
TextInTriangle* textInTriangle = new TextInTriangle(p7, p8, p9, "sample msg");
delete p7;
delete p8;
delete p9;
printShapeObjectStats();
getch();
cout << endl << endl;
getch();
delete textInTriangle;
cout << endl << endl;
printShapeObjectStats();
getch();
/*
cout << "Test Shape hierarchy of classes." << endl;
getch();
cout << "Initial objects' statistics:" << endl;
printShapeObjectStats();
getch();
// test constructors and destructors
cout << endl;
cout << "==========================================" << endl;
cout << "Testing objects' creation and destruction:" << endl;
cout << "==========================================" << endl;
getch();
cout << "Create point:" << endl;
Point* p1 = new Point(1,2);
cout << p1 << endl << endl;
getch();
cout << "Create line:" << endl;
Point* p2 = new Point(5,2);
Point* p3 = new Point(-1,3);
Line* line = new Line(p2, p3);
delete p2;
delete p3;
cout << line << endl << endl;
getch();
cout << "Create text:" << endl;
Text* text = new Text("text message");
cout << text << endl << endl;
getch();
cout << "Create triangle:" << endl;
Point* p4 = new Point(8,7);
Point* p5 = new Point(0,3);
Point* p6 = new Point(-1,0);
Triangle* triangle = new Triangle(p4, p5, p6);
delete p4;
delete p5;
delete p6;
cout << triangle << endl << endl;
getch();
cout << "Create textInTriangle:" << endl;
Point* p7 = new Point(8,7);
Point* p8 = new Point(0,3);
Point* p9 = new Point(-1,0);
TextInTriangle* textInTriangle = new TextInTriangle(p7, p8, p9, "sample msg");
delete p7;
delete p8;
delete p9;
cout << textInTriangle << endl << endl;
getch();
cout << "All objects were created. Check statistics:" << endl;
getch();
printShapeObjectStats();
getch();
cout << endl << "Now delete all these objects:" << endl;
getch();
delete p1;
delete line;
delete text;
delete triangle;
delete textInTriangle;
cout << endl << "All created objects were destroyed. Check statistics:" << endl;
getch();
printShapeObjectStats();
getch();
// Test polymorphic calls to moveBy function of different shape objects
cout << endl;
cout << "========================================================================" << endl;
cout << "Testing polymorphic calls to moveBy function of different shape objects." << endl;
cout << "========================================================================" << endl;
getch();
cout << "Create objects of different types and call this method for em" << endl;
getch();
srand((unsigned)time(0));
for(int i = 0; i < 3; i++){
cout << endl << "-------------" << endl;
cout << "create random shape: " << endl;
getch();
Shape* shape = createRandomShape();
cout << shape;
getch();
cout << endl << "Now move it" << endl;
getch();
shape->moveBy(1, 2);
cout << shape;
getch();
cout << endl << "Now destroy it" << endl;
getch();
delete shape;
getch();
}
cout << endl << "finished" << endl;
getch();
// Test operators and methods of TextInTriangle class
cout << endl;
cout << "==================================================" << endl;
cout << "Test operators and methods of TextInTriangle class" << endl;
cout << "==================================================" << endl;
cout << "Create 2 TextInTriangle objects" << endl << endl;
getch();
Point* tp1 = new Point(1,2);
Point* tp2 = new Point(3,5);
Point* tp3 = new Point(0,-2);
TextInTriangle* txtInTriangle1 = new TextInTriangle(tp1, tp2, tp3, "test1");
delete tp1;
delete tp2;
delete tp3;
Point* tp4 = new Point(-5,0);
Point* tp5 = new Point(1,2);
Point* tp6 = new Point(-4,-7);
TextInTriangle* txtInTriangle2 = new TextInTriangle(tp4, tp5, tp6, "test2");
delete tp4;
delete tp5;
delete tp6;
cout << endl << "2 TextInTriangle objects were created" << endl << endl;
getch();
cout << "First: " << txtInTriangle1 << endl;
cout << "Second: " << txtInTriangle2 << endl << endl;
getch();
cout << "First == Second ? " << (*txtInTriangle1 == *txtInTriangle2) << endl << endl;
getch();
cout << "Test assignment operator: First = Second" << endl;
*txtInTriangle1 = *txtInTriangle2;
cout << "First: " << txtInTriangle1 << endl;
cout << "Second: " << txtInTriangle2 << endl << endl;
getch();
cout << "First == Second ? " << (*txtInTriangle1 == *txtInTriangle2) << endl << endl;
getch();
cout << "Change text of the first object:" << endl;
txtInTriangle1->setText("111");
cout << "First: " << txtInTriangle1 << endl;
cout << "Second: " << txtInTriangle2 << endl << endl;
getch();
cout << "First == Second ? " << (*txtInTriangle1 == *txtInTriangle2) << endl << endl;
getch();
delete txtInTriangle1;
delete txtInTriangle2;
cout << endl;
cout << "=============" << endl;
cout << "Test finished" << endl;
cout << "=============" << endl;
getch();
cout << endl << "Check statistics one more time:" << endl;
getch();
printShapeObjectStats();
getch();
return 0;
*/
}