Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

ООП / ООП / oop-ekz-tasks / Наследование 4 семинар

.doc
Скачиваний:
11
Добавлен:
18.02.2017
Размер:
57.86 Кб
Скачать

Семинар 4 «Наследование и виртуальный механизм»

Написать работающую программу рисования графических объектов с сохранением и чтением из файла (как было на лекции): треугольник, квадрат и окружность.

//---------------------------------------------------------------------------

#include <vcl.h>

#pragma hdrstop

//---------------------------------------------------------------------------

USEFORM("Unit1.cpp", Form1);

//---------------------------------------------------------------------------

WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)

{

try

{

Application->Initialize();

SetApplicationMainFormOnTaskBar(Application, true);

Application->CreateForm(__classid(TForm1), &Form1);

Application->Run();

}

catch (Exception &exception)

{

Application->ShowException(&exception);

}

catch (...)

{

try

{

throw Exception("");

}

catch (Exception &exception)

{

Application->ShowException(&exception);

}

}

return 0;

}

//---------------------------------------------------------------------------

//---------------------------------------------------------------------------

#include <vcl.h>

#pragma hdrstop

#include "Unit1.h"

//---------------------------------------------------------------------------

#pragma package(smart_init)

#pragma resource "*.dfm"

TForm1 *Form1;

//---------------------------------------------------------------------------

__fastcall TForm1::TForm1(TComponent* Owner)

: TForm(Owner)

{Edit3->Text="Processing";}

//---------------------------------------------------------------------------

void __fastcall TForm1::Button3Click(TObject *Sender)

{

Shape* p=new Circle ("C:\Users\fw21sr\Documents\МИЭТ\ООП\Семинар 5\Parametr.txt");

try{fs = StrToFloat (Edit3->Text);

p->GetRad(fs);

Edit3->Text="Use";}

catch(...)

{Edit3->Text="Again";}

p->Drow(Image1);

delete p;

}

//---------------------------------------------------------------------------

void __fastcall TForm1::Button2Click(TObject *Sender)

{

Shape* p=new Square ("C:\Users\fw21sr\Documents\МИЭТ\ООПСеминар 5\Parametr.txt");

try{fs = StrToFloat (Edit3->Text);

p->GetRad(fs);

Edit3->Text="Use";}

catch(...)

{Edit3->Text="Again";}

p->Drow(Image1);

delete p;

}

//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)

{

Shape* p=new Triangle ("C:\Users\fw21sr\Documents\МИЭТ\ООП\Семинар 5\Parametr.txt");

try{fs = StrToFloat (Edit3->Text);

p->GetRad(fs);

Edit3->Text="Use";}

catch(...)

{Edit3->Text="Again";}

p->Drow(Image1);

delete p;

}

//---------------------------------------------------------------------------

//---------------------------------------------------------------------------

#ifndef Unit1H

#define Unit1H

//---------------------------------------------------------------------------

#include <Classes.hpp>

#include <Controls.hpp>

#include <StdCtrls.hpp>

#include <Forms.hpp>

#include <ExtCtrls.hpp>

#include "Unit2.h"

#include <Dialogs.hpp>

//---------------------------------------------------------------------------

class TForm1 : public TForm{

__published: // IDE-managed Components

TImage *Image1;

TButton *Button1;

TButton *Button2;

TButton *Button3;

TEdit *Edit3;

void __fastcall Button3Click(TObject *Sender);

void __fastcall Button2Click(TObject *Sender);

void __fastcall Button1Click(TObject *Sender);

private: // User declarations

public: // User declarations

float fs;

Shape* T;

__fastcall TForm1(TComponent* Owner);

};

//---------------------------------------------------------------------------

extern PACKAGE TForm1 *Form1;

//---------------------------------------------------------------------------

#endif

//---------------------------------------------------------------------------

#ifndef Unit2H

#define Unit2H

//---------------------------------------------------------------------------

#include <Classes.hpp>

#include <Controls.hpp>

#include <StdCtrls.hpp>

#include <Forms.hpp>

#include <ExtCtrls.hpp>

#include "fstream.h"

class Shape

{public:

int i;

float par[3];

float t1,t2;

TImage* Img;

char* path;

Shape (char* _path){

path=_path;

ifstream ifs;

ifs.open(path);

if(ifs){

ifs>>par[0];

ifs.close();

}

t1=1.4*par[0];

t2=1.7*par[0];

};

virtual void Drow(TImage* _Img)=0;

void GetRad(float fs){

par[0]=fs;

t1=1.4*par[0];

t2=1.7*par[0];

ofstream oFile;

oFile.open(path);

if (oFile)oFile<<par[0];

oFile.close();

}

};

class Circle: public Shape

{public:

Circle(char* path):Shape(path){};

void Drow(TImage* _Img){

Img=_Img;

par[2]=Img->Width/2;

par[1]=Img->Height/2;

Img->Canvas->Brush->Color = clWhite;

Img->Canvas->FillRect(Rect(0, 0, Img->Width, Img->Height));

Img->Canvas->Pen->Color = clBlue;

Img->Canvas->Brush->Color = clBlue;

Img->Canvas->Ellipse(par[2]-par[0],par[1]-par[0],par[2]+par[0],par[1]+par[0]);

Img->Canvas->Pen->Color = clWhite;

Img->Canvas->Brush->Color = clWhite;

Img->Canvas->Ellipse(par[2]-par[0]+2,par[1]-par[0]+2,par[2]+par[0]-2,par[1]+par[0]-2);

};

};

class Square: public Shape

{public:

Square(char* path):Shape(path){};

void Drow(TImage* _Img){

Img=_Img;

par[2]=Img->Width/2;

par[1]=Img->Height/2;

Img->Canvas->Brush->Color = clWhite;

Img->Canvas->FillRect(Rect(0, 0, Img->Width, Img->Height));

Img->Canvas->Pen->Color = clBlue;

Img->Canvas->Brush->Color = clBlue;

Img->Canvas->MoveTo(par[2]-t1/2,par[1]+t1/2);

Img->Canvas->LineTo(par[2]-t1/2,par[1]-t1/2);

Img->Canvas->MoveTo(par[2]-t1/2,par[1]-t1/2);

Img->Canvas->LineTo(par[2]+t1/2,par[1]-t1/2);

Img->Canvas->MoveTo(par[2]+t1/2,par[1]-t1/2);

Img->Canvas->LineTo(par[2]+t1/2,par[1]+t1/2);

Img->Canvas->MoveTo(par[2]+t1/2,par[1]+t1/2);

Img->Canvas->LineTo(par[2]-t1/2,par[1]+t1/2);

};

};

class Triangle: public Shape

{public:

Triangle(char* path):Shape(path){};

void Drow(TImage* _Img){

Img=_Img;

par[2]=Img->Width/2;

par[1]=Img->Height/2;

Img->Canvas->Brush->Color = clWhite;

Img->Canvas->FillRect(Rect(0, 0, Img->Width, Img->Height));

Img->Canvas->Pen->Color = clBlue;

Img->Canvas->Brush->Color = clBlue;

Img->Canvas->MoveTo(par[2],par[1]-t1/2);

Img->Canvas->LineTo(par[2]-t2/2,par[1]+t1/2);

Img->Canvas->MoveTo(par[2],par[1]-t1/2);

Img->Canvas->LineTo(par[2]+t2/2,par[1]+t1/2);

Img->Canvas->MoveTo(par[2]-t2/2,par[1]+t1/2);

Img->Canvas->LineTo(par[2]+t2/2,par[1]+t1/2);

};

};

//---------------------------------------------------------------------------

#endif

Соседние файлы в папке oop-ekz-tasks