반응형
#include <iostream>
using namespace std;
class Fruitseller
{
private:
int APPLE_PRICE;
int numOfApples;
int myMoney;
public:
void InitMembers(int price, int num, int money)
{
APPLE_PRICE = price;
numOfApples = num;
myMoney = money;
}
int SaleApples(int money)
{
if (money < 0)
{
cout << "잘못된 정보가 전달되어 판매를 취소합니다." << endl;
return 0;
}
int num = money / APPLE_PRICE;
numOfApples -= num;
myMoney += money;
return num;
}
void ShowSalesResult()const
{
cout << "남은사과:" << numOfApples << endl;
cout << "판매수익: " << myMoney << endl << endl;
}
};
class FruitBuyer
{
private:
int myMoney;
int numOfApples;
public:
void InitMembers(int money)
{
myMoney = money;
numOfApples = 0;
}
void BuyApples(Fruitseller& seller, int money)
{
if (money < 0)
{
cout << "잘못된 정보가 전달되어 구매를 취소합니다." << endl;
return;
}
numOfApples += seller.SaleApples(money);
myMoney -= money;
}
void ShowBuyResult()const
{
cout << "현재 잔액: " << myMoney << endl;
cout << "사과 개수: " << numOfApples << endl << endl;
}
};
int main()
{
Fruitseller seller;
seller.InitMembers(1000, 20, 0);
FruitBuyer buyer;
buyer.InitMembers(5000);
buyer.BuyApples(seller, 2000);
cout << "과일판매자의 현황" << endl;
seller.ShowSalesResult();
cout << "과일구매자의 현황" << endl;
buyer.ShowBuyResult();
return 0;
}
4-2
#include <iostream>
using namespace std;
class Point
{
private:
int xpos, ypos;
public:
void Init(int x, int y)
{
xpos = x;
ypos = y;
}
void ShowPointInfo()const
{
cout << "[" << xpos << "," << ypos << "]" << endl;
}
};
class Circle
{
private:
int rad;
Point center;
public:
void Init(int x, int y,int r)
{
rad = r;
center.Init(x, y);
}
void ShowCircleInfo()const
{
cout << "radius: " << rad << endl;
center.ShowPointInfo();
}
};
class Ring
{
private:
Circle inCircle;
Circle outCircle;
public:
void Init(int inx, int iny, int inr, int outx, int outy, int outr)
{
inCircle.Init(inx,iny,inr);
outCircle.Init(outx, outy, outr);
}
void ShowRingInfo()const
{
cout << "Inner Circle Info..." << endl;
inCircle.ShowCircleInfo();
cout << "Cuter Circle Info..." << endl;
outCircle.ShowCircleInfo();
}
};
int main()
{
Ring ring;
ring.Init(1, 1, 4, 2, 2, 9);
ring.ShowRingInfo();
return 0;
}
반응형
'Programming > C++' 카테고리의 다른 글
c++ namespace 활용 및 구조체 내의 함수 예시 , 열혈c++ Chapter 03 문제 03-1 (0) | 2021.08.15 |
---|---|
열혈c++ Chapter03 문제03-2 1번 2번 (0) | 2021.08.15 |
열혈c++ Chapter 04 문제04-3 (0) | 2021.08.15 |
열혈c++ 4-3 2번문제 (0) | 2021.08.15 |
c++ winmain (0) | 2021.08.15 |