이름과 나이를 가지는 CPerson 클래스를 설계하세요.
1) void SetName(char *str), void SetAge(int age), char * GetName(), int GetAge() 메소드를 구현하고 3명에 대해서 데이터를 저장하고 출력하는 예제 프로그램을 작성하세요.
#include "stdafx.h"
#include <iostream>
using namespace std;
class CPerson
{
protected:
char str[10];
int age;
public:
CPerson(char* str,int age){ strcpy(this->str, str); this->age = age; }
void SetName(char *str)
{
strcpy(this->str, str);
}
char GetName()
{
return *str;
}
void SetAge(int age)
{
this->age = age;
}
int GetAge()
{
return age;
}
void showData()
{
cout << "이름 : "<< str <<", 나이 : "<< age << endl;
}
};
void main()
{
CPerson p1("슈퍼맨",20);
p1.showData();
CPerson p2("배트맨",21);
p2.showData();
CPerson p3("호빵맨",22);
p3.showData();
}
'프로그래밍 > C++' 카테고리의 다른 글
파일 입출력 예제 (0) | 2012.02.24 |
---|---|
오버라이딩 예제 (0) | 2012.02.24 |
private 변수값 초기화 하기 (0) | 2012.02.24 |
두 수를 매개 변수로 전달받고 큰 수를 찾아 화면에 출력하는 함수 (0) | 2012.02.24 |
클래스 구현 실습 (0) | 2012.02.24 |