[[연습문제 1]]
#include <iostream>
using std::cin; using std::cout; using std::endl;
class ACC
{
public:
int accId;
char pw[5];
char name[20];
int bal;
void showdata();
void deposit();
void withdraw();
};
int main()
{
ACC ac = {1111, "1234", "오세혁", 5000};
ac.showdata();
ac.deposit();
ac.withdraw();
ac.showdata();
return 0;
}
void ACC::showdata()
{
cout << "\n계좌번호 : " << accId << endl;
cout << name << "회원님의 잔액은 " << bal << "입니다.\n";
}
void ACC::deposit()
{
int inm;
cout << "입금 금액 : ";
cin >> inm;
bal+=inm;
}
void ACC::withdraw()
{
int outm;
cout << "출금 금액 : ";
cin >> outm;
bal -= outm;
}
[[연습문제 2]]
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
class Triangle
{
public:
int base, height;
void SetArea(int baset, int heightt);
double GetArea();
};
int main()
{
int base, height;
cout << "\n *** 삼각형 넓이 구하기 ***\n\n";
cout << "밑변 : ";
cin >> base;
cout << "높이 : ";
cin >> height;
Triangle tri;
tri.SetArea(base, height);
cout << "삼각형의 넓이는 " << tri.GetArea() << "입니다\n";
return 0;
}
void Triangle::SetArea(int baset, int heightt)
{
base = baset;
height = heightt;
}
double Triangle::GetArea()
{
return (base*height)/2.0;
}
[[연습문제 3]]
#include <iostream>
using std::cin; using std::cout; using std::endl;
class Cal
{
public:
int sum;
void Init();
void RangeSum(int num1, int num2);
int GetrangeSum();
};
int main()
{
int su1, su2;
cout << "두 수를 입력 하시오 : ";
cin >> su1 >> su2;
Cal ca;
ca.Init();
ca.RangeSum(su1, su2);
cout << endl << su1 << " ~ " << su2 << "의 합은 ["
<< ca.GetrangeSum() << "]입니다.\n";
return 0;
}
void Cal::Init()
{
sum =0;
}
void Cal::RangeSum(int num1, int num2)
{
if (num1 > num2)
{
int temp;
temp = num1;
num1 = num2;
num2 = temp;
}
for(int i = num1; i<=num2; i++)
{
sum += i;
}
}
int Cal::GetrangeSum()
{
return sum;
}