프로그래밍 언어 3장 실습문제
1.
은행에서 사용하는 프로그램을 작성하기 위해, 은행 계좌 하나를 표현하는 클래스 Account 가 필요하다. 계좌 정보는 계좌의 주인, 계좌번호, 잔액을 나타내는 3개의 변수로 이루어진다. main() 함수의 실행결과가 다음과 같도록 Account 클래스를 작성하라.
pr.h
#include <iostream>
#include <string>
using namespace std;
class Account {
public:
string name;
int id;
int balance;
Account(string name, int id, int balance);
void deposit(int money);
int withdraw(int money);
int inquiry();
string getOwner();
};
C++
복사
pr.cpp
#include <iostream>
#include "pr.h"
using namespace std;
Account::Account(string name, int id, int balance) {
this->name = name;
this->id = id;
this->balance = balance;
}
void Account::deposit(int money) {
balance += money;
}
int Account::withdraw(int money) {
balance -= money;
return balance;
}
int Account::inquiry() {
return balance;
}
string Account::getOwner() {
return name;
}
C++
복사
main.cpp
#include <iostream>
#include "pr.h"
using namespace std;
int main() {
Account a("youn", 1, 5000);
a.deposit(50000);
cout << a.getOwner() << "의 잔액은 " << a.inquiry() << endl;
int money = a.withdraw(20000);
cout << a.getOwner() << "의 잔액은 " << a.inquiry() << endl;
}
C++
복사
2.
랜덤 수를 발생시키는 Random 클래스를 만들자. Random 클래스를 이용해 랜덤한 정수 10개를 출력하는 사례는 아래와 같다. Random 클래스가 생성자, next(), nextInRange() 의 3개의 멤버함수를 가지도록 작성하라.
main.cpp
#include <iostream>
#include <cstdlib>
#include <ctime>
#include "pr.h"
using namespace std;
int main() {
Random r;
cout << "0에서 " << RAND_MAX << "까지의 랜덤 정수 10개 --" << endl;
for (int i = 0; i < 10; i++) {
int n = r.next();
cout << n << endl;
}
cout << endl << endl << "2에서 4까지의 랜덤 정수 10개" << endl;
for (int i = 0; i < 10; i++) {
int n = r.nextInRange(2, 4);
cout << n << endl;
}
cout << endl;
}
C++
복사
pr.h
#include <iostream>
#include <string>
using namespace std;
class Random {
int seed = 0;
}
public:
int next();
int nextInRange(int s, int e);
};
C++
복사
pr.cpp
#include <iostream>
#include "pr.h"
using namespace std;
int Random::next() {
int n = rand();
return n;
}
int Random::nextInRange(int s, int e) {
// srand((unsigned int)time(0));
int n = rand() % (e - s + 1) + s;
return n;
}
C++
복사
a.
Tower 클래스 작성
#include <iostream>
#include <cstring>
using namespace std;
class Tower {
public:
int height;
Tower();
Tower(int height);
int getHeight();
};
Tower::Tower() {
height = 1;
}
Tower::Tower(int height) {
this->height = height;
}
int Tower::getHeight() {
return height;
}
int main() {
Tower myTower;
Tower seoulTower(100);
cout << "Height " << myTower.getHeight() << " meter" << endl;
cout << "Height " << seoulTower.getHeight() << " meter" << endl;
}
C++
복사
b.
날짜 다루는 Date 클래스 작성
#include <iostream>
#include <string>
using namespace std;
class Date {
public:
int year;
int month;
int day;
Date(int year, int month, int day);
Date(string date);
void show();
int getYear();
int getMonth();
int getDay();
};
Date::Date(int year, int month, int day) {
this->year = year;
this->month = month;
this->day = day;
}
Date::Date(string date) {
int ind;
this->year = stoi(date);
ind = date.find('/');
this->month = stoi(date.substr(ind + 1));
ind = date.find('/' + 1);
this->day = stoi(date.substr(ind + 1));
}
void Date::show() {
cout << year << "년" << month << "월" << day << "일" << endl;
}
int Date::getYear() {
return year;
}
int Date::getMonth() {
return month;
}
int Date::getDay() {
return day;
}
int main() {
Date birth(2014, 3, 20);
Date independenceDay("1945/8/15");
independenceDay.show();
cout << birth.getYear() << ',' << birth.getMonth() << ',' << birth.getDay() << endl;
}
C++
복사
ㅇ