[[연습문제 1]]
#include <iostream>
using std::cout;
using std::endl;
using std::cin;
class T
{
public:
T();
T(int n);
T(int a1,double a2, char b);
T(char a);
T(char *p);
T(int a1, int a2, char *p);
};
int main()
{
T ob, ob2, ob3; //객체 생성
T ob4(100); //객체 생성 시 인수전달
T ob5(10, 20.34, 'B');
T ob6('a');
T ob7;
T ob8("Hello");
T ob9(50, 60, "Happy Birthday");
return 0;
}
T::T()
{
cout << "생성자\n";
}
T::T(int n)
{
cout << n << "을 받는 생성자\n";
}
T::T(int a1, double a2, char b)
{
cout << a1<< ", " << a2<< ", " << b << "를 받는 생성자\n";
}
T::T(char *p)
{
cout << p << "를 받는 생성자\n";
}
T::T(int a1, int a2, char *p)
{
cout << a1<< ", " << a2<< ", " << p << "를 받는 생성자\n";
}
T::T(char a)
{
cout << a << "를 받는 생성자\n";
}
[[연습문제 2]]
#include <iostream>
#include <string.h>
using std::cout;
using std::cin;
using std::endl;
class ACC
{
private:
int accId; //계좌번호
char pw[5]; //비밀번호
char name[20]; //이름
int bal; //잔액
public:
ACC(int accid, char *Pp, char *Pn, int bal);
void deposit(); //입금
void withdraw(); //출금
void showData(); //잔액조회
};
///////////////////////////////////////////main
int main()
{
ACC ac(1111, "1234", "김수현", 5000); //객체 생성 후, 초기화
ac.showData(); //잔액조회
ac.deposit(); //입금
ac.withdraw(); //출금
ac.showData(); //잔액조회
return 0;
}
///////////////////////////////////////////ACC 멤버함수 외부정의
ACC::ACC(int accid, char *Pp, char *Pn, int bal2)
{
accId = accid;
strcpy(pw, Pp);
strcpy(name, Pn);
bal = bal2;
}
void ACC::deposit()
{
int m;
cout << "\n입금금액 입력 : ";
cin >> m;
bal += m;
cout << "\t\t" << m << "원 입금완료~!!\n";
}
void ACC::withdraw()
{
int m;
cout << "\n출금금액 입력 : ";
cin >> m;
bal -= m;
cout << "\t\t" << m << "원 출금완료~!!\n";
}
void ACC::showData()
{
cout << "\n계좌번호 : " << accId << endl;
cout << name << "회원님의 잔액은 " << bal << "입니다.\n";
}
[[연습문제 3]]
>>Triangle.h<<
class Triangle
{
private:
int base, height;
public:
Triangle(int b, int h);
double GetArea();
};
>>main.cpp<<
#include <iostream>
#include "Triangle.h"
using std::cin; using std::cout; using std::endl;
int main()
{
int base, height;
cout << "\n *** 삼각형 넓이 구하기 ***\n\n";
cout << "밑변 : ";
cin >> base;
cout << "높이 : ";
cin >> height;
Triangle tri(base, height);
cout << "삼각형의 넓이는 " << tri.GetArea() << "입니다\n";
return 0;
}
>>Triangle.cpp<<
#include <iostream>
#include "Triangle.h"
Triangle::Triangle(int b, int h) : base(b), height(h)
{}
double Triangle::GetArea()
{
return (base*height)/2.0;
}