문제 1
#include <iostream>
using namespace std;
class Point
{
private:
int xpos;
int ypos;
public:
Point(int x, int y)
:xpos(x),ypos(y)
{
}
void ShowPointInfo() const
{
cout << "[" << xpos << "," << ypos << "]" << endl;
}
};
class Circle
{
private:
Point center;
int radius;
public:
Circle(int x, int y, int r)
:center(x,y),radius(r)
{
}
void ShowCircleInfo() const
{
cout << "center: ";
center.ShowPointInfo();
cout << "radius: " << radius << endl;
}
};
class Ring
{
private:
Circle incircle;
Circle outcircle;
public:
Ring(int x1, int y1, int radius1, int x2, int y2, int radius2)
:incircle(x1,y1,radius1),outcircle(x2,y2,radius2)
{
}
void ShowRingInfo()
{
cout << "Inner Circle Info..." << endl;
incircle.ShowCircleInfo();
cout << "Outter Circle Info..." << endl;
outcircle.ShowCircleInfo();
}
};
int main()
{
Ring ring(1, 1, 4, 2, 2, 9);
ring.ShowRingInfo();
return 0;
}
2024.08.05 - [C++] - 문제 04-2 [다양한 클래스의 정의]
문제 04-2 [다양한 클래스의 정의]
#include using namespace std;class Point{private: int xpos; int ypos;public: void Init(int x, int y) { xpos = x; ypos = y; } void ShowPointInfo() const { cout const 선언 잊지말기캡슐화 신경쓰기 다른버전#include using namespace std;class
kwone.tistory.com
생성자와 이니셜라이즈를 이용하여 초기화 함수 Init의 별도 정의 대신 생성과 동시에 초기화 할 수 있도록 하였다.
문제 2
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstring>
using namespace std;
namespace COMP_POS
{
enum rank
{
CLERK=1,
SENIOR,
ASSIST,
MANAGER
};
}
class NameCard
{
private:
char* name;
char* cpname;
char* tpnumber;
int rank;
public:
NameCard(const char* myname, const char* mycpname, const char* mytpnumber, int myrank)
{
int len1 = strlen(myname) + 1;
name = new char[len1];
strcpy(name, myname);
int len2 = strlen(mycpname) + 1;
cpname = new char[len2];
strcpy(cpname, mycpname);
int len3 = strlen(mytpnumber) + 1;
tpnumber = new char[len3];
strcpy(tpnumber, mytpnumber);
rank = myrank;
}
void ShowNameCardInfo() const
{
cout << "이름: " << name << endl;
cout << "회사: " << cpname << endl;
cout << "전화번호: " << tpnumber << endl;
cout << "직급: ";
switch (rank)
{
case COMP_POS::CLERK:
cout << "사원";
break;
case COMP_POS::SENIOR:
cout << "주임";
break;
case COMP_POS::ASSIST:
cout << "대리";
break;
case COMP_POS::MANAGER:
cout << "과장";
break;
}
cout << endl << endl;
}
~NameCard()
{
delete[]name;
delete[]cpname;
delete[]tpnumber;
}
};
int main(void)
{
NameCard manClerk("Lee", "ABCEng", "010-1111-2222", COMP_POS::CLERK);
NameCard manSenior("Hong", "A232455", "010-1123-1252", COMP_POS::SENIOR);
NameCard manAssist("Kim", "53dfssdf", "010-1235-7234", COMP_POS::ASSIST);
manClerk.ShowNameCardInfo();
manSenior.ShowNameCardInfo();
manAssist.ShowNameCardInfo();
return 0;
}
'C++' 카테고리의 다른 글
OOP (Object-Oriented-Project) (1) | 2024.08.07 |
---|---|
문제 04-2 [다양한 클래스의 정의] (0) | 2024.08.05 |
문제 04-1 [정보은닉과 const] (0) | 2024.08.05 |
Class 활용 예제 (0) | 2024.08.05 |
문제 03-2 [클래스의 정의] (0) | 2024.08.02 |