سلام!

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

سوال اول:

class Adam{
    private:
        int height, weight, x;
    public:
        
        void setHeight(int);
        void setWeight(int);
        void setX(int);

        int getHeight();
        int getWeight();
        int getX();

        void walk(int);
};

void Adam::setHeight(int newHeight){
    height = newHeight;
}

void Adam::setWeight(int newWeight){
    weight = newWeight;
}

void Adam::setX(int newX){
    x = newX;
}

int Adam::getHeight(){
    return height;
}

int Adam::getWeight(){
    return weight;
}

int Adam::getX(){
    return x;
}

void Adam::walk(int value){
    setX( getX() + value); //Say konid be jaye in ke benevisid x += value az in halat estefade konid, behtar ast
}

سوال دوم:

#include <iostream>
#include <cmath>

using namespace std;

class Car{
    private:
        int width, height, maxSpeed, maxAcceleration, currentSpeed, currentAcceleration;
    public:
        
        void setWidth(int);
        void setHeight(int);
        void setMaxSpeed(int);
        void setMaxAcceleration(int);
        void setCurrentSpeed(int);
        void setCurrentAcceleration(int);

        int getWidth();
        int getHeight();
        int getMaxSpeed();
        int getMaxAcceleration();
        int getCurrentSpeed();
        int getCurrentAcceleration();
};

void Car::setWidth(int newWidth){
    width = newWidth;
}

void Car::setHeight(int newHeight){
    height = newHeight;
}

void Car::setMaxSpeed(int newMaxSpeed){
    maxSpeed = newMaxSpeed;
}

void Car::setMaxAcceleration(int newMaxAcceleration){
    maxAcceleration = newMaxAcceleration;
}

void Car::setCurrentSpeed(int newCurrentSpeed){
    if ( abs(newCurrentSpeed) <= maxSpeed) //abs: ghadre motlagh
        currentSpeed = newCurrentSpeed;
    else cout << "ERROR! newCurrentSpeed is " << newCurrentSpeed << " while maxSpeed is " << maxSpeed << endl;
}

void Car::setCurrentAcceleration(int newCurrentAcceleration){
    if ( abs(newCurrentAcceleration) <= maxAcceleration)
        currentAcceleration = newCurrentAcceleration;
    else cout << "ERROR! newCurrentAcceleration is " << newCurrentAcceleration << " while maxAcceleration is " << maxAcceleration << endl;
}

int Car::getWidth(){
    return width;
}

int Car::getHeight(){
    return height;
}

int Car::getMaxSpeed(){
    return maxSpeed;
}

int Car::getMaxAcceleration(){
    return maxAcceleration;
}

int Car::getCurrentSpeed(){
    return currentSpeed;
}

int Car::getCurrentAcceleration(){
    return currentAcceleration;
}

int main(){
    Car car;
    car.setWidth(100);
    car.setHeight(100);
    car.setMaxSpeed(100);
    car.setMaxAcceleration(30);
    car.setCurrentSpeed(-120);
    car.setCurrentSpeed(50);
    car.setCurrentAcceleration(50);
    car.setCurrentAcceleration(20);
}

سوال سوم:

#include <iostream>
#include <iomanip> //baraye estefade az setprecision (dar main)

using namespace std;

class Cal{
    private:
        double a, b;
    public:
        void setNums(double, double);

        double getFirstNum();
        double getSecondNum();

        double average();
        double sum();
        double multiply();
        double division();
};

void Cal::setNums(double newA, double newB){
    a = newA;
    b = newB;
}

double Cal::getFirstNum(){
    return a;
}

double Cal::getSecondNum(){
    return b;
}

double Cal::average(){
    return sum() / 2;
}

double Cal::sum(){
    return a + b;
}

double Cal::multiply(){
    return a * b;
}

double Cal::division(){
    return a / b;
}

int main(){
    cout << fixed << setprecision(5) << endl; //ba in kar adad ha ta 5 raghame ashar cout mishavand
    Cal cal;
    cal.setNums(135.562345, 43.144235);
    cout << "Number1: " << cal.getFirstNum() << "\nNumber2: " << cal.getSecondNum() << "\nAverage: " << cal.average() << "\nSum: " << cal.sum() << "\nMultiply: " << cal.multiply() << "\nDivision: " << cal.division() << endl;
    return 0;
}

سوال چهارم:

#include <iostream>

using namespace std;

class Array{
    private:
        int ar[20];
    public:
        void set(int, int);
        int get(int);
        void sort();
};

void Array::set(int pos, int value){
    if ( pos <= 20 && pos >= 0 )
        ar[pos] = value;
    else cout << "ERROR! pos is " << pos << "!" << endl;
}

int Array::get(int pos){
    if ( pos <= 20 && pos >= 0 )
        return ar[pos];
    else{
        return -1;
    }
}

void Array::sort(){
    for (int i = 0; i < 20; i++)
        for (int j = 0; j < 20; j++)
            if (ar[j] < ar[i])
                swap(ar[i], ar[j]);
}

int main(){
    Array ar;
    for (int i = 0; i < 20; i++){
        int n;
        cin >> n;
        ar.set(i, n);
    }
    cout << ar.get(21) << endl;
    ar.sort();
    for (int i = 0; i < 20; i++)
        cout << ar.get(i) << " ";
    cout << endl;
}

موفق باشید!