سلام!
می توانید پاسخ سری قبلی تمرین ها را در ادامه ی مطلب مشاهده نمایید. سوال های خود را در قسمت نظرات مطرح کنید.
#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() const;
int height() const;
int x() const;
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() const{
return this->_weight;
}
int Adam::height() const{
return this->_height;
}
int Adam::x() const{
return this->_x;
}
void Adam::walk(int value){
this->setX(this->x() + value);
}
int main(){
const Adam *adam = new Adam(70, 180);
// adam->walk(); functione walk nemitavanad const bashad chon darad meghdari ra taghyir midahad, pas nemitavanad baraye yek objecte const estefade shavad
cout << adam->x() << endl;
delete adam;
return 0;
}
#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() const;
int height() const;
int maxSpeed() const;
int maxAcceleration() const;
int currentSpeed() const;
int currentAcceleration() const;
};
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() const{
return this->_width;
}
int Car::height() const{
return this->_height;
}
int Car::maxSpeed() const{
return this->_maxSpeed;
}
int Car::maxAcceleration() const{
return this->_maxAcceleration;
}
int Car::currentSpeed() const{
return this->_currentSpeed;
}
int Car::currentAcceleration() const{
return this->_currentAcceleration;
}
int main(){
const 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;
}
#include <iostream>
#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() const;
double secondNum() const;
void setFirstNum(double);
void setSecondNum(double);
double average() const;
double sum() const;
double multiply() const;
double division() const;
};
double Cal::firstNum() const{
return this->_firstNum;
}
double Cal::secondNum() const{
return this->_secondNum;
}
void Cal::setFirstNum(double firstNum){
this->_firstNum = firstNum;
}
void Cal::setSecondNum(double secondNum){
this->_secondNum = secondNum;
}
double Cal::average() const{
return sum() / 2;
}
double Cal::sum() const{
return this->firstNum() + this->secondNum();
}
double Cal::multiply() const{
return this->firstNum() * this->secondNum();
}
double Cal::division() const{
return this->firstNum() / this->secondNum();
}
int main(){
const 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());
delete calculator;
return 0;
}
#include <iostream>
using namespace std;
class Array{
private:
int* ar;
public:
Array();
void set(int, int);
int get(int) const;
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) const{
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(){
const 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;
}
موفق باشید!