티스토리 뷰
클래스 관계
1. has ~a : 클래스의 내용을 가져다 쓴다
- data와 관리 class
- 포함 object
- 제일 많이 쓰는 방식
#include "stdafx.h"
#include <stdio.h>
#include <string>
#include <iostream>
using namespace std;
//has ~a 관계
class A {
string name;
public :
A() { //디폴트 생성자
cout << "A 생성자" << endl;
}
A(string name) { //Class B에서 받은 name
this->name = name;
}
void setName(string name) {
this->name = name;
}
string getName() const {
return name;
}
};
class B {
A aa; //포함 object
int age;
public :
B() {
cout << "B 생성자" << endl;
}
B(string name, int age) : aa(name) { //aa 객체에 name을 준다
this->age = age;
}
void setAge(int age) {
this->age = age;
}
int getAge() const {
return age;
}
//A를 가져다 쓰기
void setName(string name) {
aa.setName(name);
}
string getName() const {
return aa.getName();
}
};
void main() {
B bb;
B bb("Superman", 10);
bb.setName("a"); //이름입력
//bb.aa.setName("a"); //오류 - name이 private으로 되어있기 때문
bb.setAge(10); //나이입력
cout << bb.getName() << endl; //이름출력
cout << bb.getAge() << endl; //나이출력
}
Has ~a 관계 성적 처리
#include "stdafx.h"
#include <string>
#include <iostream>
using namespace std;
//Has ~a 관계 성적 처리
class Name {
string name;
public :
void setName(string name) { this->name = name; }
string getName() { return name; }
};
class Subject {
int score;
public :
void setScore(int score) { this->score = score; }
int getScore() { return score; }
};
class ScoMag { //계산
Name n;
Subject kor;
Subject eng;
Subject math;
int total;
float avg;
public :
void setName(string name) { n.setName(name); }
string getName() { return n.getName(); }
void setKor(int score) { kor.setScore(score); }
int getKor() { return kor.getScore(); }
void setEng(int score) { eng.setScore(score); }
int getEng() { return eng.getScore(); }
void setMath(int score) { math.setScore(score); }
int getMath() { return math.getScore(); }
void setTotal(int total) { this->total = total; }
int getTotal() {
total = kor.getScore() + eng.getScore() + math.getScore();
return total; }
void setAvg(float avg) { this->avg = avg; }
float getAvg() {
avg = getTotal() / 3.f;
return avg; }
};
void main() {
int num = 0;
int pos = 0;
string name = NULL;
int kor = 0, eng = 0, math = 0;
cout << "사람 수 : ";
cin >> num;
ScoMag *sms = new ScoMag[num];
for (int i = 0; i < num; i++) {
cout << "이름 : ";
cin >> name;
sms[i].setName(name);
cout << "국어 : ";
cin >> kor;
sms[i].setKor(kor);
cout << "영어 : ";
cin >> eng;
sms[i].setEng(eng);
cout << "수학 : ";
cin >> math;
sms[i].setMath(math);
pos++;
}
for (int i = 0; i < pos; i++) {
cout << "이름 : " << sms[i].getName() << endl;
cout << "국어 : " << sms[i].getKor() << endl;
cout << "영어 : " << sms[i].getEng() << endl;
cout << "수학 : " << sms[i].getMath() << endl;
cout << "총점 : " << sms[i].getTotal() << endl;
cout << "평균 : " << sms[i].getAvg() << endl;
}
cout << endl;
delete[] sms;
}
2. is ~a : ~는 ~이다
- 상속 : 부모 클래스로부터 물려받음으로써 코드의 확장성과 재사용성을 높힌다
기본 형태
//클래스 B가 A를 상속 받을 때
class A { class B : 접근 지정자 A {
}; };
상속 방식
1. public 상속 //is ~a
- class B : public A { }
- JAVA에서의 상속
- 원래 의미 그대로 상속받음
#include "stdafx.h"
#include <iostream>
using namespace std;
class A {
int a1; //1
protected :
int b1; //2
public :
int c1; //3
void setA1(int a1) { this->a1 = a1; }
void setB1(int b1) { this->b1 = b1; }
int getA1() { return a1; }
int getB1() { return b1; }
};
class B : public A {
int a2; //4
protected:
int b2; //5
public:
int c2; //6
void setA2(int a2) { this->a2 = a2; }
void setB2(int b2) { this->b2 = b2; }
int getA2() { return a2; }
int getB2() { return b2; }
};
class C : public B {
int a3; //7
protected:
int b3; //8
public:
int c3; //9
void setA3(int a3) { this->a3 = a3; }
void setB3(int b3) { this->b3 = b3; }
int getA3() { return a3; }
int getB3() { return b3; }
};
void main() {
C cc; //a1~c3
//직접 접근가능 -> 직접 표현
cc.c1 = 3;
cc.c2 = 6;
cc.c3 = 9;
cout << "직접 접근 : " << cc.c1 << " " << cc.c2 << " " << cc.c3 << endl;
//접근 불가능 -> 함수 표현
cc.setA1(1);
cc.setA2(4);
cc.setA3(7);
cc.setB1(2);
cc.setB2(5);
cc.setB3(8);
cout << "함수 표현해서 접근 : " << cc.getA1() << " " << cc.getA2() << " " << cc.getA3() << endl;
cout << "함수 표현해서 접근 : " << cc.getB1() << " " << cc.getB2() << " " << cc.getB3() << endl;
}
2. protected 상속 //has ~a
- class B : protected A { }
- 부모의 접근 지정자가 바뀜
#include "stdafx.h"
#include <iostream>
using namespace std;
class A {
int a1; //1
protected:
int b1; //2
public:
int c1; //3
void setA1(int a1) { this->a1 = a1; }
void setB1(int b1) { this->b1 = b1; }
void setC1(int c1) { this->c1 = c1; }
int getA1() { return a1; }
int getB1() { return b1; }
int getC1() { return c1; }
};
class B : protected A {
int a2; //4
protected:
int b2; //5
public:
int c2; //6
void setA2(int a2) { this->a2 = a2; }
void setB2(int b2) { this->b2 = b2; }
void setC2(int c2) { this->c2 = c2; }
int getA2() { return a2; }
int getB2() { return b2; }
int getC2() { return c2; }
};
class C : protected B {
int a3; //7
protected:
int b3; //8
public:
int c3; //9
void setA3(int a3) { this->a3 = a3; }
void setB3(int b3) { this->b3 = b3; }
int getA3() { return a3; }
int getB3() { return b3; }
void setA1(int a1) { A::setA1(a1); }
int getA1() { return A::getA1(); }
void setA2(int a2) { B::setA2(a2); }
int getA2() { return B::getA2(); }
void setB1(int b1) { A::setB1(b1); }
int getB1() { return A::getB1(); }
void setB2(int b2) { B::setB2(b2); }
int getB2() { return B::getB2(); }
void setC1(int c1) { A::setC1(c1); }
int getC1() { return A::getC1(); }
void setC2(int c2) { B::setC2(c2); }
int getC2() { return B::getC2(); }
};
void main() {
C cc;
//직접 접근 가능
cc.c3 = 9;
cout << "직접 접근 : " << cc.c3 << endl;
//직접 접근 불가능
cc.setA1(1);
cc.setA2(4);
cc.setA3(7);
cc.setB1(2);
cc.setB2(5);
cc.setB3(8);
cc.setC1(3);
cc.setC2(6);
cout << "함수 표현해서 접근 : " << cc.getA1() << " " << cc.getA2() << " " << cc.getA3() << endl;
cout << "함수 표현해서 접근 : " << cc.getB1() << " " << cc.getB2() << " " << cc.getB3() << endl;
cout << "함수 표현해서 접근 : " << cc.getC1() << " " << cc.getC2() << endl;
}
3. private 상속 //has ~a
- class B : private A { }
#include "stdafx.h"
#include <iostream>
using namespace std;
class A {
int a1; //1
protected:
int b1; //2
public:
int c1; //3
void setA1(int a1) { this->a1 = a1; }
void setB1(int b1) { this->b1 = b1; }
void setC1(int c1) { this->c1 = c1; }
int getA1() { return a1; }
int getB1() { return b1; }
int getC1() { return c1; }
};
class B : private A {
int a2; //4
protected:
int b2; //5
public:
int c2; //6
void setA2(int a2) { this->a2 = a2; }
void setB2(int b2) { this->b2 = b2; }
void setC2(int c2) { this->c2 = c2; }
int getA2() { return a2; }
int getB2() { return b2; }
int getC2() { return c2; }
void setA1(int a1) { A::setA1(a1); }
void setB1(int b1) { A::setB1(b1); }
void setC1(int c1) { A::setC1(c1); }
int getA1() { return A::getA1(); }
int getB1() { return A::getB1(); }
int getC1() { return A::getC1(); }
};
class C : private B {
int a3; //7
protected:
int b3; //8
public:
int c3; //9
void setA3(int a3) { this->a3 = a3; }
void setB3(int b3) { this->b3 = b3; }
int getA3() { return a3; }
int getB3() { return b3; }
void setA1(int a1) { B::setA1(a1); }
int getA1() { return B::getA1(); }
void setA2(int a2) { B::setA2(a2); }
int getA2() { return B::getA2(); }
void setB1(int b1) { B::setB1(b1); }
int getB1() { return B::getB1(); }
void setB2(int b2) { B::setB2(b2); }
int getB2() { return B::getB2(); }
void setC1(int c1) { B::setC1(c1); }
int getC1() { return B::getC1(); }
void setC2(int c2) { B::setC2(c2); }
int getC2() { return B::getC2(); }
};
void main() {
C cc;
//직접 접근 가능
cc.c3 = 9;
cout << "직접 접근 : " << cc.c3 << endl;
//직접 접근 불가능
cc.setA1(1);
cc.setA2(4);
cc.setA3(7);
cc.setB1(2);
cc.setB2(5);
cc.setB3(8);
cc.setC1(3);
cc.setC2(6);
cout << "함수 표현해서 접근 : " << cc.getA1() << " " << cc.getA2() << " " << cc.getA3() << endl;
cout << "함수 표현해서 접근 : " << cc.getB1() << " " << cc.getB2() << " " << cc.getB3() << endl;
cout << "함수 표현해서 접근 : " << cc.getC1() << " " << cc.getC2() << endl;
}
#접근 지정자를 지정하지 않으면 private으로 상속
상속 |
부모 |
자식 |
private |
1. private 2. proteced 3. public |
모두 private |
protected |
1. private 2. proteced 3. public |
1 -> private 2 -> protected 3 -> protected |
public |
1. private 2. proteced 3. public |
있는 그대로 상속 |
'LANGUAGE > C++' 카테고리의 다른 글
[C++] MULTIPLE INHERITANCE - 다중 상속 (0) | 2018.03.13 |
---|---|
[C++] 급여관리 프로그램 (0) | 2018.03.12 |
[C++] CONST (0) | 2018.03.08 |
[C++] STATIC (0) | 2018.03.08 |
[C++] THIS (0) | 2018.03.08 |
댓글