Skip to content

Instantly share code, notes, and snippets.

@Zabaa
Created March 19, 2017 12:29
Show Gist options
  • Save Zabaa/a695ca30786de6d71b5b78fc27a88274 to your computer and use it in GitHub Desktop.
Save Zabaa/a695ca30786de6d71b5b78fc27a88274 to your computer and use it in GitHub Desktop.
// 3.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <ctime>
using namespace std;
class Figura
{
protected:
double s; //pole figury płaskiej
char str[256]; //nazwa figury płaskiej
public:
Figura(char sstr[]) { strcpy_s(str, sizeof(str), sstr); }
virtual void f() = 0;
void disp() { cout << str << endl << "pole " << s << endl; }
};
class Trojkat : public Figura
{
double a, h;
public:
Trojkat(double aa, double hh, char sstr[]) : Figura(sstr), a(aa), h(hh) {}
void f() { s = a*h/2.0; }
void set(double aa, double hh, char sstr[]) { a = aa; h = hh; strcpy_s(str, sizeof(str), sstr); }
};
class Quadrat : public Figura
{
double a;
public:
Quadrat(double aa, char sstr[] ) : Figura(sstr), a(aa) {}
void f() { s = a*a; }
void set(double aa, char sstr[]) { a = aa; strcpy_s(str, sizeof(str), sstr); }
};
int _tmain(int argc, _TCHAR* argv[])
{
//Stworzyc tablicę wskaźników typu Figura o nazwie pfig, które
//by wskazywali do obiektów:
// Trojkat a = 2 h = 1 nazwa - trojkat ABC
// Quadrat a = 4 nazwa - quadrat ABCD
// Trojkat a = 3 h = 1 nazwa - trojkat ECD
// Quadrat a = 3 nazwa - quadrat EFKL
// NULL - na nic nie wskazuje.
Figura *pfig[] =
{
new Trojkat(2, 1, "ABC"),
new Quadrat(4, "ABCD"),
new Trojkat(3, 1, "ECD"),
new Quadrat(3, "EFKL"),
NULL,
};
Trojkat *pTr;
Quadrat *pQ;
srand(time(NULL));
for(size_t it=0; it<10; ++it)
{
size_t i = rand()%5;
//i jest wartyością losową
//Wyznaczyć typ figury, do której wskazuje pfig[i]
//Jeśli to jest trókąt, za pomocą metody set ustalić
// a = 10, h = 5, nazwa - "Triangle MNK";
//Jeśli qwadrat - a = 10, nazwa - "Quadrat OPRS"
if (Trojkat* fig = dynamic_cast<Trojkat*>(pfig[i])) {
fig->set(10,5, "Triangle MNK");
}
if (Quadrat* fig = dynamic_cast<Quadrat*>(pfig[i])) {
fig->set(10, "Quadrat OPRS");
}
if(pfig[i]) {
pfig[i]->f();
pfig[i]->disp();
}
}
system("pause");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment