سلام!

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

سوال اول:

  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class Adam{
  6.     private:
  7.         int _weight, _height, _x; //dalile vojoode "_" ghabl az esme moteghayyer ha in ast ke badan betavanim be jaye getWeight() benevisim weight(), khana tar ast.
  8.     public:
  9.         
  10.         Adam(int = 0, int = 0, int = 0);
  11.  
  12.         void setWeight(int);
  13.         void setHeight(int);
  14.         void setX(int);
  15.  
  16.         int weight();
  17.         int height();
  18.         int x();
  19.  
  20.         void walk(int = 1);
  21. };
  22.  
  23. Adam::Adam(int weight, int height, int x){
  24.     this->setWeight(weight);
  25.     this->setHeight(height);
  26.     this->setX(x);
  27. }
  28.  
  29. void Adam::setWeight(int weight){
  30.     this->_weight = weight;
  31. }
  32.  
  33. void Adam::setHeight(int height){
  34.     this->_height = height;
  35. }
  36.  
  37. void Adam::setX(int x){
  38.     this->_x = x;
  39. }
  40.  
  41. int Adam::weight(){
  42.     return this->_weight;
  43. }
  44.  
  45. int Adam::height(){
  46.     return this->_height;
  47. }
  48.  
  49. int Adam::x(){
  50.     return this->_x;
  51. }
  52.  
  53. void Adam::walk(int value){
  54.     this->setX(this->x() + value);
  55. }
  56.  

سوال دوم:

  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. using namespace std;
  5.  
  6. class Car{
  7.     private:
  8.         int _width, _height, _maxSpeed, _maxAcceleration, _currentSpeed, _currentAcceleration;
  9.     public:
  10.         Car(int = 100, int = 200, int = 120, int = 30, int = 0, int = 0);
  11.  
  12.         void setWidth(int);
  13.         void setHeight(int);
  14.         void setMaxSpeed(int);
  15.         void setMaxAcceleration(int);
  16.         void setCurrentSpeed(int);
  17.         void setCurrentAcceleration(int);
  18.  
  19.         int width();
  20.         int height();
  21.         int maxSpeed();
  22.         int maxAcceleration();
  23.         int currentSpeed();
  24.         int currentAcceleration();
  25. };
  26.  
  27. Car::Car(int width, int height, int maxSpeed, int maxAcceleration, int currentSpeed, int currentAcceleration){
  28.     this->setWidth(width);
  29.     this->setHeight(height);
  30.     this->setMaxSpeed(maxSpeed);
  31.     this->setMaxAcceleration(maxAcceleration);
  32.     this->setCurrentSpeed(currentSpeed);
  33.     this->setCurrentAcceleration(currentAcceleration);
  34. }
  35.  
  36. void Car::setWidth(int width){
  37.     this->_width = width;
  38. }
  39.  
  40. void Car::setHeight(int height){
  41.     this->_height = height;
  42. }
  43.  
  44. void Car::setMaxSpeed(int maxSpeed){
  45.     this->_maxSpeed = maxSpeed;
  46. }
  47.  
  48. void Car::setMaxAcceleration(int maxAcceleration){
  49.     this->_maxAcceleration = maxAcceleration;
  50. }
  51.  
  52. void Car::setCurrentSpeed(int currentSpeed){
  53.     if (abs(currentSpeed) <= this->maxSpeed())
  54.         this->_currentSpeed = currentSpeed;
  55. }
  56.  
  57. void Car::setCurrentAcceleration(int currentAcceleration){
  58.     if (abs(currentAcceleration) <= this->maxAcceleration())
  59.         this->_currentAcceleration = currentAcceleration;
  60. }
  61.  
  62. int Car::width(){
  63.     return this->_width;
  64. }
  65.  
  66. int Car::height(){
  67.     return this->_height;
  68. }
  69.  
  70. int Car::maxSpeed(){
  71.     return this->_maxSpeed;
  72. }
  73.  
  74. int Car::maxAcceleration(){
  75.     return this->_maxAcceleration;
  76. }
  77.  
  78. int Car::currentSpeed(){
  79.     return this->_currentSpeed;
  80. }
  81.  
  82. int Car::currentAcceleration(){
  83.     return this->_currentAcceleration;
  84. }
  85.  
  86. int main(){
  87.     Car *car = new Car(200, 100, 120, 50, 100, 30);
  88.     cout << "Width: " << car->width() << "\nHeight: " << car->height() << "\nmaxSpeed: " << car->maxSpeed() << "\nmaxAcceleration: " << car->maxAcceleration() << "\ncurrentSpeed: " << car->currentSpeed() << "\ncurrentAcceleration: " << car->currentAcceleration() << endl;
  89.     delete car;
  90. }
  91.  

سوال سوم: (برای آشنایی با دستور printf می توانید به این لینک مراجعه نمایید. اگر باز هم سوالی ماند در قسمت نظرات مطرح کنید.).

  1. #include <iostream>
  2. #include <iomanip> //setprecision
  3. #include <cstdio> //printf
  4.  
  5. using namespace std;
  6.  
  7. class Cal{
  8.     private:
  9.         double _firstNum, _secondNum;
  10.     public:
  11.         Cal(double firstNum = 0, double secondNum = 0) : _firstNum(firstNum), _secondNum(secondNum) {} //intori neveshtan be tamiztar shodane code komak mikonad, faghat moshkel injast ke digar controli bar rooye meghdarDehi nadarim va inja niazi ham be aan nadarim
  12.         
  13.         double firstNum();
  14.         double secondNum();
  15.  
  16.         void setFirstNum(double);
  17.         void setSecondNum(double);
  18.  
  19.         double average();
  20.         double sum();
  21.         double multiply();
  22.         double division();
  23. };
  24.  
  25. double Cal::firstNum(){
  26.     return this->_firstNum;
  27. }
  28.  
  29. double Cal::secondNum(){
  30.     return this->_secondNum;
  31. }
  32.  
  33. void Cal::setFirstNum(double firstNum){
  34.     this->_firstNum = firstNum;
  35. }
  36.  
  37. void Cal::setSecondNum(double secondNum){
  38.     this->_secondNum = secondNum;
  39. }
  40.  
  41. double Cal::average(){
  42.     return sum() / 2;
  43. }
  44.  
  45. double Cal::sum(){
  46.     return this->firstNum() + this->secondNum();
  47. }
  48.  
  49. double Cal::multiply(){
  50.     return this->firstNum() * this->secondNum();
  51. }
  52.  
  53. double Cal::division(){
  54.     return this->firstNum() / this->secondNum();
  55. }
  56.  
  57. int main(){
  58.     cout << fixed << setprecision(3);
  59.     Cal* calculator = new Cal(1324.523251, 5465.2534);
  60.     printf("First Number is %f And Second Number is %f.\nSum: %f\nAverage: %f\nMultiply: %f\nDivision: %f\n", calculator->firstNum(), calculator->secondNum(), calculator->sum(), calculator->average(), calculator->multiply(), calculator->division());
  61.     return 0;
  62. }
  63.  

سوال چهارم:

  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class Array{
  6.     private:
  7.         int* ar;
  8.     public:
  9.         Array();
  10.  
  11.         void set(int, int);
  12.  
  13.         int get(int);
  14.  
  15.         void sort();
  16. };
  17.  
  18. Array::Array(){
  19.     ar = new int[20];
  20. }
  21.  
  22. void Array::set(int pos, int value){
  23.     if (pos >= 0 || pos <= 20)
  24.         ar[pos] = value;
  25. }
  26.  
  27. int Array::get(int pos){
  28.     if (pos >= 0 || pos <= 20)
  29.         return ar[pos];
  30.     else return -1;
  31. }
  32.  
  33. void Array::sort(){
  34.     for (int i = 0; i < 20; i++)
  35.         for (int j = 0; j < 20; j++)
  36.             if (ar[j] > ar[i])
  37.                 swap(ar[i], ar[j]);
  38. }
  39.  
  40. int main(){
  41.     Array* ar = new Array;
  42.     for (int i = 0; i < 20; i++){
  43.         int value;
  44.         cin >> value;
  45.         ar->set(i, value);
  46.     }
  47.     ar->sort();
  48.     for (int i = 0; i < 20; i++)
  49.         cout << ar->get(i) << " ";
  50.     cout << endl;
  51.     delete ar;
  52.     return 0;
  53. }

هر اشکالی داشتید، آن را در قسمت نظرات مطرح کنید.

موفق باشید!