C++ 열혈강의 7-2 연습문제

♪C++ 2010. 11. 1. 00:13

#include<iostream>
using std::endl;
using std::cout;

class person
{
protected:
 int age;
 char *name;
public:
 int getage() const
 {
  return age;
 }
 const char * getname() const{
  return name;
 }
 person(int _age=1,char * _name="noname")
 {
  age=_age;
  name=new char [strlen(_name)+1];
  strcpy(name,_name);
 }
 ~person()
 {
 delete []name;
 }
};
class student: public person
{
 char *major;
public:
 student(char* _major,int _age,char* _name) : person(_age,_name)   //이니셜라이져
 {
//  age=_age;
//  strcpy(name,_name);
  major=new char [strlen(_major)+1];
  strcpy(major,_major);
 }
 const char* getmajor() const{
  return major;
 }
 ~student()
 {
 delete []major;
 }
 void showdata() const{
  cout<<"이름 : "<<getname()<<endl;
  cout<<"나이 : "<<getage()<<endl;
  cout<<"전공 : "<<getmajor()<<endl;
 }
};
int main()
{
 student jun("컴퓨터공학과",20,"김준현");
 jun.showdata();

 return 0;
}


 

'♪C++' 카테고리의 다른 글

4996 에러  (0) 2018.08.14
c++ 연습 8-1  (0) 2010.11.14
OOP프로젝트 5단계  (0) 2010.11.02
열혈강의 c++ 연습문제 7-1  (1) 2010.10.31
C++ BasicInheri1  (1) 2010.10.31