블로그 이미지
SANGHO KIM

calendar

1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31

Notice

Tag

2018. 1. 23. 18:31 C++Language


#include <cstring>

#include <iostream>

using std::cout;

using std::endl;


class AAA

{

int a;

public:

AAA()

{

cout<<"AAA() call!"<<endl;

a=10;

}

AAA(int i)

{

cout<<"AAA(int i) call!"<<endl;

}

};

class BBB:public AAA

{

int b;

public:

BBB(){

cout<<"BBB() call!"<<endl;

b=20;

}

BBB(int j)

{

cout<<"BBB(int j) call!"<<endl;

}

};

int main()

{

BBB b;

return 0;

}


#include <iostream>

#include <cstring>

using std::endl;

using std::cout;


class Person

{

int age;

char name[20];

public:


int GetAge() const {

return age;

}

const char* GetName() const {

return name;

}


Person(int _age=1, char* _name="noname"){

age=_age;

strcpy(name, _name);

}

};


class Student: public Person

{

char major[20]; //전공

public:

Student(char* _major){

strcpy(major, _major);

}

const char* GetMajor() const {

return major;

}

void ShowData() const {

cout<<"이름: "<<GetName()<<endl;

cout<<"나이: "<<GetAge()<<endl;

cout<<"전공: "<<GetMajor()<<endl;

}

};


int main(void)

{

Student Kim("computer");

Kim.ShowData();


return 0;

}; 

아래의 코드가 작동하지 않는 이유를 고찰하시오.

#include <iostream>

#include <cstring>

using std::endl;

using std::cout;


class Person

{

int age;

char name[20];

public:


int GetAge() const {

return age;

}

const char* GetName() const {

return name;

}


Person(int _age=1, char* _name="noname"){

age=_age;

strcpy(name, _name);

}

};


class Student: public Person

{

char major[20]; //전공

public:

Student(int _age, char* _name, char* _major){

age=_age;

strcpy(name, _name);

strcpy(major, _major);

}

const char* GetMajor() const {

return major;

}

void ShowData() const {

cout<<"이름: "<<GetName()<<endl;

cout<<"나이: "<<GetAge()<<endl;

cout<<"전공: "<<GetMajor()<<endl;

}

};


int main(void)

{

Student Kim(20, "Hong Gil Dong", "computer");

Kim.ShowData();


return 0;

};

아래의 코드를 잘 이해하시오.

#include <iostream>

#include <cstring>

using std::endl;

using std::cout;


class Person

{

int age;

char name[20];

public:


int GetAge() const {

return age;

}

const char* GetName() const {

return name;

}


Person(int _age=1, char* _name="noname"){

age=_age;

strcpy(name, _name);

}

};


class Student: public Person

{

char major[20]; //전공

public:

Student(int _age, char* _name, char* _major)

: Person(_age, _name)

{

strcpy(major, _major);

}

const char* GetMajor() const {

return major;

}

void ShowData() const {

cout<<"이름: "<<GetName()<<endl;

cout<<"나이: "<<GetAge()<<endl;

cout<<"전공: "<<GetMajor()<<endl;

}

};


int main(void)

{

Student Kim(20, "Hong Gil Dong", "computer");

Kim.ShowData();


return 0;

};

데이터의 Protected를 사용한 이유를 설명하시오.

Protected를 사용할 경우, 상속관계에 있는 경우 접근 가능하다.

#include <iostream>

#include <cstring>

using std::endl;

using std::cout;


class Person

{

/*데이터*/

protected:

int age;

char name[20];

public:

/*자동함수*/

Person(int _age=1, char* _name="noname"){age=_age; strcpy(name, _name);}

/*수동함수*/

int GetAge() const {return age;}

const char* GetName() const {return name;}

};


class Student: public Person

{

/*데이터*/

char major[20];

public:

/*수동함수*/

Student(int _age, char* _name, char* _major):Person(_age, _name)

{strcpy(major, _major);}

/*수동함수*/

const char* GetMajor() const {return major;}

void ShowData() const {

cout<<"이름: "<<age<<endl;

cout<<"나이: "<<name<<endl;

cout<<"전공: "<<major<<endl;

}

};


int main(void)

{

Student Kim(20, "Hong Gil Dong", "computer");

Kim.ShowData();

return 0;

}; 


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

소유관계  (0) 2018.01.24
3가지 형태의 상속  (0) 2018.01.24
C++언어; 상속(1)  (0) 2018.01.23
C++언어; explicit과 mutable 함수의 사용  (0) 2018.01.23
C++언어; 클래스의 static변수 사용  (0) 2018.01.23
posted by SANGHO KIM