코딩 및 기타

c++ 복습 3-1~3-5

정지홍 2023. 2. 17. 12:24

3-1

#include <iostream>
using namespace std;
class Tower {
int height;
public:
Tower() { this->height = 1; };
Tower(int x) { this->height = x; };
~Tower() { cout << "소멸! height=" << this->height << endl; };
double getHeight();
};

double Tower::getHeight() {
return this->height;
}

int main(void) {
Tower myTower;
Tower sTower(100);
cout << "myTower height is " << myTower.getHeight() << endl;
cout << "sTower height is " << sTower.getHeight() << endl;
return 0;
}


3-2

#include <iostream>
using namespace std;
 
class Date {
string dependence;
int year, month, day;
public:
Date(int y, int m, int d);
Date(string dpD);
void show() {
cout << dependence << endl;
};
int getYear();
int getMonth();
int getDay();
};

int Date::getYear() {
return year;
}
int Date::getMonth() {
return month;
}
int Date::getDay() {
return day;
}

Date::Date(int y, int m, int d) {
year = y, month = m, day = d;
}

Date::Date(string dpD) {
dependence = dpD;

}
int main() {
Date birth(2014, 3, 20);
Date independenceDay("1945/8/15");
independenceDay.show();
cout << birth.getYear() << ',' << birth.getMonth() << ',' << birth.getDay() << endl;
}


3-3

#include <iostream>
#include <string>
using namespace std;
 
class Account {
string name;
int id=0;
int money=0;
public:
Account(string n, int id, int money) {
name = n;
id = id;
money = money;
};
int deposit(int x) {
money += x;
return money;
}
int withdraw(int y) {
money -= y;
return money;
}
string getOwner() {
return name;
}
int inquiry() {
return money;
}

};


int main() {
Account a("kitae", 1, 5000);
a.deposit(50000);
cout << a.getOwner() << "의 잔액 " << a.inquiry() << endl;
int money = a.withdraw(20000);
cout << a.getOwner() << "의 잔액 " << a.inquiry() << endl;
}


3-4

#include <iostream>
using namespace std;
class CoffeeMachin {
int cof;
int water;
int sugar;
public:
 
CoffeeMachin(int x, int y, int z) {
cof = x;
water = y;
sugar = z;
};
void show();
int drinkEsspresso();
int drinkAmericano();
int drinkSugarCoffee();
int fill();
};

void CoffeeMachin::show() {
cout << "커피 머신 상태, " << "커피:" << cof << "\t" << "물:" << water << "\t 설탕:" << sugar << " \n";
}

int CoffeeMachin::drinkEsspresso() {
return cof--,water--;
}

int CoffeeMachin::drinkAmericano() {
return cof--, water -= 2;
}

int CoffeeMachin::drinkSugarCoffee() {
return cof--, water -= 2; sugar--;
}

int CoffeeMachin::fill() {
return cof = 10, water = 10, sugar = 10;
}

int main() {
CoffeeMachin java(5, 10, 3);
java.drinkEsspresso();
java.show();
java.drinkAmericano();
java.show();
java.drinkSugarCoffee();
java.show();
java.fill();
java.show();
}


3-5

#include <iostream>
#include <cstdlib>
using namespace std;

class Random {

public:
int next();
int nextInRange(int x, int y);
};

int Random::next() {
return rand();
}

int Random::nextInRange(int x, int y) {
return rand() % (y-1) + x;
}

int main() {
srand(time(NULL));
Random r;
cout << "--0에서" << RAND_MAX << "까지의 랜덤 정수 10개--" << endl;
for (int i = 0; i < 10; i++) {
int n = r.next();
cout << n << ' ';
}
cout << endl << endl << "--2에서 " << "4까지의 랜덤 정수 10개--" << endl;
for (int i = 0; i < 10; i++) {
int n = r.nextInRange(2, 4);
cout << n << ' ';
}

}

'코딩 및 기타' 카테고리의 다른 글

c++ 복습 4-1~4-5  (1) 2023.02.22
c++ 복습 3-6~3-9  (0) 2023.02.21
c++ 클래스 복습  (0) 2023.02.16
c++복습 2-9~2-15  (0) 2023.02.15
c++복습 2-6~2-8  (0) 2023.02.14