Я хочу рассчитать площадь и периметр прямоугольников, используя следующий код:
rect a;
a = ( -----
! !
-----a );
std::cout << a.area() << std::endl;
std::cout << a.perimeter() << std::endl;
Для этого я создал следующий класс:
class rect
{
public:
rect():w(0), h(2) {}
rect& operator - () { w += 0.5f; return *this; }
rect& operator - (rect&) { w += 0.5f; return *this; }
rect& operator -- (int a) { w += a; return *this; }
rect& operator -- () { w += 1; return *this; }
rect& operator ! () { h += 0.5f; return *this; }
void clear() { w = 0; h = 2; }
int area() { return w * h; }
int perimeter() { return 2 * w + 2 * h; }
int width() { return w; }
int height() { return h; }
private:
float w;
float h;
};
Вот несколько примеров использования:
#include <iostream>
int main()
{
rect a;
a = ( -----
! !
-----a );
std::cout << a.area() << std::endl;
std::cout << a.perimeter() << std::endl;
std::cout << a.width() << std::endl;
std::cout << a.height() << std::endl;
std::cout << std::endl;
a.clear();
a = ( ----------
! !
! !
! !
! !
---------a );
std::cout << a.area() << std::endl;
std::cout << a.perimeter() << std::endl;
std::cout << a.width() << std::endl;
std::cout << a.height() << std::endl;
return 0;
}
Вот мои вопросы:
Можно ли его обобщить на трехмерный случай? То есть:
cuboid b;
b = ( ---------
/ /!
! -------! !
! ! !
! ! !
! !/
---------b );
std::cout << b.volume() << std::endl;