سلام!
جواب های سری قبلی تمرین ها را می توانید در ادامه ی مطلب مشاهده نمایید.
- #include <iostream>
- using namespace std;
- class Adam{
- private:
- int _weight, _height, _x; //dalile vojoode "_" ghabl az esme moteghayyer ha in ast ke badan betavanim be jaye getWeight() benevisim weight(), khana tar ast.
- public:
- Adam(int = 0, int = 0, int = 0);
- void setWeight(int);
- void setHeight(int);
- void setX(int);
- int weight();
- int height();
- int x();
- void walk(int = 1);
- };
- Adam::Adam(int weight, int height, int x){
- this->setWeight(weight);
- this->setHeight(height);
- this->setX(x);
- }
- void Adam::setWeight(int weight){
- this->_weight = weight;
- }
- void Adam::setHeight(int height){
- this->_height = height;
- }
- void Adam::setX(int x){
- this->_x = x;
- }
- int Adam::weight(){
- return this->_weight;
- }
- int Adam::height(){
- return this->_height;
- }
- int Adam::x(){
- return this->_x;
- }
- void Adam::walk(int value){
- this->setX(this->x() + value);
- }
- #include <iostream>
- #include <cmath>
- using namespace std;
- class Car{
- private:
- int _width, _height, _maxSpeed, _maxAcceleration, _currentSpeed, _currentAcceleration;
- public:
- Car(int = 100, int = 200, int = 120, int = 30, int = 0, int = 0);
- void setWidth(int);
- void setHeight(int);
- void setMaxSpeed(int);
- void setMaxAcceleration(int);
- void setCurrentSpeed(int);
- void setCurrentAcceleration(int);
- int width();
- int height();
- int maxSpeed();
- int maxAcceleration();
- int currentSpeed();
- int currentAcceleration();
- };
- Car::Car(int width, int height, int maxSpeed, int maxAcceleration, int currentSpeed, int currentAcceleration){
- this->setWidth(width);
- this->setHeight(height);
- this->setMaxSpeed(maxSpeed);
- this->setMaxAcceleration(maxAcceleration);
- this->setCurrentSpeed(currentSpeed);
- this->setCurrentAcceleration(currentAcceleration);
- }
- void Car::setWidth(int width){
- this->_width = width;
- }
- void Car::setHeight(int height){
- this->_height = height;
- }
- void Car::setMaxSpeed(int maxSpeed){
- this->_maxSpeed = maxSpeed;
- }
- void Car::setMaxAcceleration(int maxAcceleration){
- this->_maxAcceleration = maxAcceleration;
- }
- void Car::setCurrentSpeed(int currentSpeed){
- if (abs(currentSpeed) <= this->maxSpeed())
- this->_currentSpeed = currentSpeed;
- }
- void Car::setCurrentAcceleration(int currentAcceleration){
- if (abs(currentAcceleration) <= this->maxAcceleration())
- this->_currentAcceleration = currentAcceleration;
- }
- int Car::width(){
- return this->_width;
- }
- int Car::height(){
- return this->_height;
- }
- int Car::maxSpeed(){
- return this->_maxSpeed;
- }
- int Car::maxAcceleration(){
- return this->_maxAcceleration;
- }
- int Car::currentSpeed(){
- return this->_currentSpeed;
- }
- int Car::currentAcceleration(){
- return this->_currentAcceleration;
- }
- int main(){
- Car *car = new Car(200, 100, 120, 50, 100, 30);
- cout << "Width: " << car->width() << "\nHeight: " << car->height() << "\nmaxSpeed: " << car->maxSpeed() << "\nmaxAcceleration: " << car->maxAcceleration() << "\ncurrentSpeed: " << car->currentSpeed() << "\ncurrentAcceleration: " << car->currentAcceleration() << endl;
- delete car;
- }
سوال سوم: (برای آشنایی با دستور printf می توانید به این لینک مراجعه نمایید. اگر باز هم سوالی ماند در قسمت نظرات مطرح کنید.).
- #include <iostream>
- #include <iomanip> //setprecision
- #include <cstdio> //printf
- using namespace std;
- class Cal{
- private:
- double _firstNum, _secondNum;
- public:
- 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
- double firstNum();
- double secondNum();
- void setFirstNum(double);
- void setSecondNum(double);
- double average();
- double sum();
- double multiply();
- double division();
- };
- double Cal::firstNum(){
- return this->_firstNum;
- }
- double Cal::secondNum(){
- return this->_secondNum;
- }
- void Cal::setFirstNum(double firstNum){
- this->_firstNum = firstNum;
- }
- void Cal::setSecondNum(double secondNum){
- this->_secondNum = secondNum;
- }
- double Cal::average(){
- return sum() / 2;
- }
- double Cal::sum(){
- return this->firstNum() + this->secondNum();
- }
- double Cal::multiply(){
- return this->firstNum() * this->secondNum();
- }
- double Cal::division(){
- return this->firstNum() / this->secondNum();
- }
- int main(){
- cout << fixed << setprecision(3);
- Cal* calculator = new Cal(1324.523251, 5465.2534);
- 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());
- return 0;
- }
- #include <iostream>
- using namespace std;
- class Array{
- private:
- int* ar;
- public:
- Array();
- void set(int, int);
- int get(int);
- void sort();
- };
- Array::Array(){
- ar = new int[20];
- }
- void Array::set(int pos, int value){
- if (pos >= 0 || pos <= 20)
- ar[pos] = value;
- }
- int Array::get(int pos){
- if (pos >= 0 || pos <= 20)
- 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 = new Array;
- for (int i = 0; i < 20; i++){
- int value;
- cin >> value;
- ar->set(i, value);
- }
- ar->sort();
- for (int i = 0; i < 20; i++)
- cout << ar->get(i) << " ";
- cout << endl;
- delete ar;
- return 0;
- }
هر اشکالی داشتید، آن را در قسمت نظرات مطرح کنید.
موفق باشید!