Wersja online w serwisie Tech.io
Poniżej jest kod napisany w starym C++03. Reprezentuje on prostą hierarchię figur geometrycznych. Klasa bazowa Shape definiuje interfejs z 3 publicznymi metodami: getArea(), getPerimeter() i print(). Po Shape dziedziczy klasa Rectangle reprezentująca prostokąt, a po niej dziedziczy klasa Square reprezentująca kwadrat. Klasy te odpowienio implementują wspomniane metody. W funkcji main() jest użycie figur, które są przechowywane w kolekcji. Twoim zadaniem jest unowocześnienie tego kodu, korzystając z możliwości jakie daje C++11 i C++14.
Jeśli ściągniesz kod lokalnie, to dodatkowo możesz sprawdzać postęp za pomocą dodanych skryptów sprawdzających. Repozytorium do ściągnięcia na GitHubie
Potrzebne programy: make, g++
make
nullptr: Zamień wszystkie NULLe nanullptrusingalias: Zamień typedef na alias using- automatic type deduction:
Użyj
auto, tam gdzie można go użyć - range based for loop: Użyj pętli for po kolekcji tam, gdzie to możliwe
default: Oznacz konstruktory kopiujące jakodefault.delete: Usuń metodęgetY()z klasySquare. Usuń domyślne konstruktory (te bez parametrów).final: Oznacz klasęSquarejakofinaloraz oznacz metodęgetX()w klasieRectanglejakofinaloverride: Oznacz wszystkie wirtualne metody jakooverride. Czy zauważasz jakiś problem?
- uniform initialization:
Użyj jednolitej inicjalizacji do zainicjalizowania kolekcji
shapes. - smart pointers:
Zamień zwykłe wskażniki na
shared_ptr - lambda functions:
Zamień funkcję
sortByArea()na funkcję lambda
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class Shape
{
public:
virtual ~Shape() {}
virtual double getArea() const = 0;
virtual double getPerimeter() const = 0;
virtual void print() const { cout << "Unknown Shape" << endl; }
};
class Rectangle : public Shape
{
public:
Rectangle(double x, double y) : x_(x), y_(y) {}
Rectangle(const Rectangle & other) { x_ = other.getX(); y_ = other.getY(); }
double getArea() const { return x_ * y_; }
double getPerimeter() const { return 2 * (x_ + y_); }
double getX() const { return x_; }
double getY() const { return y_; }
void print() const {
cout << "Rectangle: x: " << getX() << endl
<< " y: " << getY() << endl
<< " area: " << getArea() << endl
<< " perimeter: " << getPerimeter() << endl;
}
private:
Rectangle();
double x_;
double y_;
};
class Square : public Rectangle
{
public:
Square(double x) : Rectangle(x, x) {}
Square(const Square & other) : Rectangle(other.getX(), other.getX()) {}
double getArea() { return getX() * getX(); }
double getPerimeter() { return 4 * getX(); }
void print() {
cout << "Square: x: " << getX() << endl
<< " area: " << getArea() << endl
<< " perimeter: " << getPerimeter() << endl;
}
private:
double getY(); // should not have Y dimension
Square();
};
bool sortByArea(Shape* first, Shape* second)
{
if(first == NULL || second == NULL)
{
return false;
}
return (first->getArea() < second->getArea());
}
typedef vector<Shape*> Collection;
void printCollectionElements(const Collection& collection)
{
for(Collection::const_iterator it = collection.begin(); it != collection.end(); ++it)
if(*it != NULL)
(*it)->print();
}
void printAreas(const Collection& collection)
{
for(Collection::const_iterator it = collection.begin(); it != collection.end(); ++it)
if(*it != NULL)
cout << (*it)->getArea() << endl;
}
int main() {
Collection shapes;
shapes.push_back(new Rectangle(4.0, 2.0));
shapes.push_back(new Rectangle(10.0, 5.0));
shapes.push_back(new Square(3.0));
shapes.push_back(new Square(4.0));
printCollectionElements(shapes);
cout << "Areas before sort: " << endl;
printAreas(shapes);
sort(shapes.begin(), shapes.end(), sortByArea);
cout << "Areas after sort: " << endl;
printAreas(shapes);
return 0;
}- Porównaj swoje rozwiązania z naszymi na GitHubie
- Polub Coders School na Facebooku
- Zapisz się na newsletter, aby pobrać dzisiejszą prezentację oraz otrzymywać informacje o przyszłych wydarzeniach i kursach (możesz się wypisać w dowolnej chwili)
- Dołącz do Meetupa Kurs programowania C++
- Odwiedź stronę Coders School - http://coders.school
- Sprawdź swój poziom znajomości C++ wykonując 10-minutowy test
- Zapoznaj się z naszą ofertą kursu C++
- Zgłoś się na darmowe konsultacje z C++
- W razie jakichkolwiek pytań napisz mi maila - lukasz(at)coders.school

