#include <iostream>
#include <string>

using namespace std;

class Human{
 string name;
 int age;
public:
 Human(string n, int a) : name(n), age(a) {}
 string getName(){
  return name;
 }
 int getAge(){
  return age;
 }
 void setName(string n){
  name =n;
 }
 void setAge(int a){
  age =a;
 }
 void print(){
  cout << "이름 : " << name << endl;
  cout << "나이 : " << age << endl;
 }
};

class Student : public Human{
 string major;
public:
 Student(string name, int age, string major) : Human(name,age){
  this ->major = major;
 }
 string getMajor()
 {
  return major;
 }
 void setMajor(string m){
  major = m;
 }
 void print(){
  Human::print();
  cout << "전공 :" << major << endl;
 }
};


int main()
{
 Student h1("명진", 21,"컴퓨터") ,h2("미현",22,"경영"),h3("용준",24,"전자");
 h1.print();
 h2.print();
 h3.print();
 return 0;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////
7. 다음과 같은 생성자를 정의하여 보자. 부모 클래스의 생성자를 호출하여 보라.
Student(string name, int age, string major)
{
              ....// 매개 변수의 값으로 필드를 초기화 한다.
}
이게 중요한 내용이다.

 

'공부하자... > C언어' 카테고리의 다른 글

다형성으로 클래스 구현  (0) 2010.10.04
다형성 정리  (0) 2010.09.29
클래스의 상속성  (0) 2010.09.27
연산자 중복  (0) 2010.09.13
원을 나타내는 클래스 Circle를 가지고 실습하여 보자.  (0) 2010.09.08

+ Recent posts