Skip to content

coders-school/meetup_modern_cpp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Meetup Nowoczesny C++ - Welcome!

Coders School

Build Status

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++

Użycie:

make

Zadania:

  1. nullptr: Zamień wszystkie NULLe na nullptr
  2. using alias: Zamień typedef na alias using
  3. automatic type deduction: Użyj auto, tam gdzie można go użyć
  4. range based for loop: Użyj pętli for po kolekcji tam, gdzie to możliwe
  5. default: Oznacz konstruktory kopiujące jako default.
  6. delete: Usuń metodę getY() z klasy Square. Usuń domyślne konstruktory (te bez parametrów).
  7. final: Oznacz klasę Square jako final oraz oznacz metodę getX() w klasie Rectangle jako final
  8. override: Oznacz wszystkie wirtualne metody jako override. Czy zauważasz jakiś problem?

Zadania dodatkowe:

  1. uniform initialization: Użyj jednolitej inicjalizacji do zainicjalizowania kolekcji shapes.
  2. smart pointers: Zamień zwykłe wskażniki na shared_ptr
  3. 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;
}

Co dalej?

  1. Porównaj swoje rozwiązania z naszymi na GitHubie
  2. Polub Coders School na Facebooku
  3. Zapisz się na newsletter, aby pobrać dzisiejszą prezentację oraz otrzymywać informacje o przyszłych wydarzeniach i kursach (możesz się wypisać w dowolnej chwili)
  4. Dołącz do Meetupa Kurs programowania C++
  5. Odwiedź stronę Coders School - http://coders.school
  6. Sprawdź swój poziom znajomości C++ wykonując 10-minutowy test
  7. Zapoznaj się z naszą ofertą kursu C++
  8. Zgłoś się na darmowe konsultacje z C++
  9. W razie jakichkolwiek pytań napisz mi maila - lukasz(at)coders.school

Hello job!

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •