Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- using namespace std;
- // + (Складывает площади и возвращает полученный квадрат)
- // & (Умножает стороны и возвращает квадрат с полученной стороной)
- class Square{
- double length;
- public:
- explicit Square(double = 0);
- double getPerimeter() const;
- double getArea() const;
- double getLength() const;
- void setLength(double);
- void setLength(int);
- void printAll() const;
- void printAll(int) const;
- Square operator + (Square) const;
- Square& operator = (Square);
- Square operator & (Square) const;
- };
- Square::Square(double length){
- if (length < 0){
- cout << "Невозможно создать объект с отрицательной длиной стороны"
- "\nСоздан нулевой объект" << endl;
- length = 0;
- }
- this -> length = length;
- }
- double Square::getPerimeter() const{
- return length * 4;
- }
- double Square::getArea() const{
- return length * length;
- }
- double Square::getLength() const{
- return length;
- }
- void Square::setLength(double len){
- if (len < 0) len = 0;
- this -> length = len;
- }
- void Square::setLength(int len){
- if (len < 0) len = 0;
- this -> length = len;
- }
- void Square::printAll() const{
- cout << "Длина: " << getLength() << endl;
- cout << "Периметр: " << getPerimeter() << endl;
- cout << "Площадь: " << getArea() << endl;
- }
- void Square::printAll(int param) const{
- if (!(param == 1 || param == 2)){
- param = 3;
- }
- char* ans[3] = {(char*)"Длина: ", (char*)"Периметр: ", (char*)"Площадь: "};
- double val[3] = {length, getPerimeter(), getArea()};
- for (int i = 0; i < param; i++) cout << ans[i] << val[i] << endl;
- }
- Square Square::operator + (Square sq) const{
- Square sq1;
- sq1.setLength(sqrt(this -> getArea() + sq.getArea()));
- sq1.printAll();
- return sq1;
- }
- Square& Square::operator = (Square sq){
- this -> length = sq.getLength();
- return *this;
- }
- Square Square::operator & (Square sq) const {
- Square sq1;
- sq1.setLength(this -> getLength() * sq.getLength());
- return sq1;
- }
- int main() {
- Square sq(3);
- Square sq1(3);
- Square sq2 = sq1 + sq;
- cout << sq2.getLength() << endl;
- cout << sq2.getArea() << endl;
- Square sq3 = sq & sq1;
- sq3.printAll();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement