클래스, 상속, virtual, 선언등 기본적인 샘플 파일이다.

#include <iostream>
#include <string>

using namespace std;

class Test 
{
private:  
int  Age;
string Name;
int KeyNumber;

public:
virtual void showData();
void SetAge(int age) { Age = age; }
void SetName(string name) {Name = name; }
virtual void SetKeyNumber(int key) {KeyNumber = key; }
int GetAge() { return Age; }
string GetName() { return Name; }
int GetKeyNumber() { return KeyNumber; }
Test(int age = 0, string name = "누구", int key = 0)
{
Age = age;
Name = name;
KeyNumber = key;
}
};

void Test::showData()
{
cout << "-------------------------------------" << endl;
cout << " Age : " << Age << endl;
cout << " Name : " << Name << endl;
cout << " KeyNumber : " << KeyNumber << endl;
cout << "-------------------------------------" << endl;
}

class Friend:public Test
{
private:
int friends;
int grade;

public:
virtual void SetKeyNumber(int key) { grade = key * 2; }
void showData();
Friend(int age, string name, int key):Test(age, name, key){};
};

void Friend::showData()
{
cout << "-------------------------------------" << endl;
cout << " Age : " << GetAge() << endl;
cout << " Name : " << GetName() << endl;
cout << " Grade : " << grade << endl;
cout << "-------------------------------------" << endl;
}

int main(int argc, char **argv)
{
Test T1(24, "홍길동", 1);
Test *T2 = new Test(24, "고길동", 2);
Test T3;
T1.showData();
T2->showData();
T3.showData();

Test *T4 = new Friend(25, "백만원", 4);
T4->showData();
T4->SetKeyNumber(4);
T4->showData();
return 0;
}

'공부중 > C/C++' 카테고리의 다른 글

이 카테고리는 C++에 관련된 내용이다.  (0) 2009.08.19

+ Recent posts