سلام!

پاسخ سوال های تمرین سری قبل را می توانید در ادامه ی مطلب مشاهده نمایید.

سوال اول:

#include <iostream>
#include <cstdio>

using namespace std;

class Rectangle{

    friend void set(Rectangle*, int, int);
    
    private:

        int _width, _height;

        void setWidth(int);
        void setHeight(int);
        
    public:
    
        Rectangle(int = 0, int = 0);

        int width() const;
        int height() const;
};

void set(Rectangle* Rectangle, int width, int height){
    Rectangle->setWidth(width);
    Rectangle->setHeight(height);
}

Rectangle::Rectangle(int width, int height){
    set(this, width, height);
}

void Rectangle::setWidth(int width){
    this->_width = width;
}

void Rectangle::setHeight(int height){
    this->_height = height;
}

int Rectangle::width() const{
    return this->_width;
}

int Rectangle::height() const{
    return this->_height;
}

int main(){
    int a, b;
    cin >> a >> b;
    Rectangle rectangle(a, b);
    printf("Width: %d\nHeight: %d\n", rectangle.width(), rectangle.height());
    return 0;
}

سوال دوم:

#include <iostream>
#include <cstdio>

using namespace std;

class Point{
    
    friend void set(Point*, int = 0, int = 0);

    private:

        int _x, _y;
        
        void setX(int);
        void setY(int);
    
    public:

        Point(int = 0, int = 0);

        int x() const;
        int y() const;

};

Point::Point(int x, int y){
    set(this, x, y);
}

void Point::setX(int x){
    this->_x = x;
}

void Point::setY(int y){
    this->_y = y;
}

int Point::x() const{
    return this->_x;
}

int Point::y() const{
    return this->_y;
}

void set(Point *point, int x, int y){
    point->setX(x);
    point->setY(y);
}

int main(){
    int x, y;
    scanf("%d %d", &x, &y);
    Point point(x, y);
    printf("X: %d\nY: %d\n", point.x(), point.y());
    return 0;
}

موفق باشید!