class 파생클래스명 : 상속접근지정 기본클래스명{
...
};
class Student : public Person {
// Person을 상속받는 Student 선언
..... };
class StudentWorker : public Student {
// Student를 상속받는 StudentWorker 선언
..... };
#include <iostream>
#include <string>
using namespace std;
// 2차원 평면에서 한 점을 표현하는 클래스 Point 선언
class Point {
int x, y; //한 점 (x,y) 좌표값
public:
void set(int x, int y) {
this->x = x;
this->y = y;
}
void showPoint() {
cout << "(" << x << "," << y << ")" << endl;
}
};
class ColorPoint : public Point { // 2차원 평면에서 컬러 점을 표현하는 클래스 ColorPoint. Point를 상속받음
string color;// 점의 색 표현 public:
void setColor(string color) {
this->color = color;
}
void showColorPoint();
};
void ColorPoint::showColorPoint() {
cout << color << ":";
showPoint(); // Point의 showPoint() 호출
}
int main() {
Point p; // 기본 클래스의 객체 생성
ColorPoint cp; // 파생 클래스의 객체 생성
cp.set(3,4); // 기본 클래스의 멤버 호출
cp.setColor("Red"); // 파생 클래스의 멤버 호출
cp.showColorPoint(); // 파생 클래스의 멤버 호출
}
✅ **출력결과
—————————————————————————————————————————————**
Red:(3,4)
—————————————————————————————————————————————
#include <iostream>
using namespace std;
class Parent {
public:
int mValue;
void func1() {
mValue = 1;
}
};
class Child : public Parent {
public:
int mValue2;
void func2() {
mValue2 = 2;
}
};
int main()
{
Child c;
c.func1(); //부모 클래스의 멤버 함수도 접근 가능
c.func2();
c.Parent::func1(); //부모 클래스의 멤버 함수도 접근 가능
cout << c.Parent::mValue << endl;
cout << c.mValue << endl;
cout << c.mValue2 << endl;
return 0;
}
✅ **출력결과
—————————————————————————————————————————————**
1
1
2
—————————————————————————————————————————————
#include <iostream>
using namespace std;
class Car {
string model;
int speed;
public:
Car() {
speed = 0;
}
void setModel(string model) {
this->model = model;
}
void setSpeed(int speed) {
this->speed = speed;
}
string getModel() {
return model;
}
int getSpeed() {
return speed;
}
};
class SportsCar : public Car {
bool turbo;
public:
SportsCar() {
turbo = true;
}
bool isTurbo() {
return turbo;
}
};
int main()
{
Car myCar;
myCar.setModel("audi");
myCar.setSpeed(50);
cout << "myCar : " << myCar.getModel() << ", speed = " << myCar.getSpeed() << endl;
SportsCar yourCar;
yourCar.setModel("bmw");
yourCar.setSpeed(100);
cout << "yourCar : " << yourCar.getModel() << ", speed = " << yourCar.getSpeed() << endl;
if (yourCar.isTurbo()) {
cout << "yourCar = turbo engine" << endl;
}
else
{
cout << "youtCar = not turbo engine" << endl;
}
return 0;
}
✅ **출력결과
—————————————————————————————————————————————**
myCar : audi, speed = 50
yourCar : bmw, speed = 100
yourCar = turbo engine
—————————————————————————————————————————————
int main() {
ColorPoint cp;
ColorPoint *pDer = &cp;
pDer->set(3,4);
pDer->setColor("Red");
pDer->showColorPoint();
}
#include <iostream>
using namespace std;
//2차원 평면에서 한 점을 표현하는 클래스
class Point {
int x, y;
public:
void set(int x, int y) {
this->x = x;
this->y = y;
}
void showPoint() {
cout << "(" << x << ", " << y << ")" << endl;
}
};
//컬러값이 추가된 파생 클래스
class ColorPoint : public Point {
string color;
public:
void setColor(string color) {
this->color = color;
}
void showColorPoint() {
cout << color << "." ;
showPoint();
}
};
int main()
{
Point p;
p.set(1, 2);
p.showPoint();
//Point 객체의 주소를 저장하는 포인터변수
//부모 클래스 객체
Point* pBase = &p;
p.set(100, 200);
p.showPoint();
ColorPoint cp;
cp.set(10, 20);
cp.setColor("Blue");
cp.showColorPoint();
//ColorPoint 객체의 주소를 저장하는 포인터 변수
//자식 클래스 객체
ColorPoint* pDer;
pDer = &cp;
pDer->set(3, 4);
pDer->setColor("Yellow");
pDer->showColorPoint();
reteurn 0;
}
✅ **출력결과
—————————————————————————————————————————————**
(1, 2)
(100, 200)
Blue.(10, 20)
Yellow.(3, 4)
—————————————————————————————————————————————
#include <iostream>
using namespace std;
//2차원 평면에서 한 점을 표현하는 클래스
class Point {
int x, y;
public:
void set(int x, int y) {
this->x = x;
this->y = y;
}
void showPoint() {
cout << "(" << x << ", " << y << ")" << endl;
}
};
//컬러값이 추가된 파생 클래스
class ColorPoint : public Point {
string color;
public:
void setColor(string color) {
this->color = color;
}
void showColorPoint() {
cout << color << "." ;
showPoint();
}
};
int main()
{
Point p;
p.set(1, 2);
p.showPoint();
//Point 객체의 주소를 저장하는 포인터변수
//부모 클래스 객체
Point* pBase = &p;
p.set(100, 200);
p.showPoint();
ColorPoint cp;
cp.set(10, 20);
cp.setColor("Blue");
cp.showColorPoint();
//ColorPoint 객체의 주소를 저장하는 포인터 변수
//자식 클래스 객체
ColorPoint* pDer;
pDer = &cp;
pDer->set(3, 4);
pDer->setColor("Yellow");
pDer->showColorPoint();
//부모 객체 포인터 <= 자식객체 포인터를 대입
//업캐스팅
pBase = pDer;
pBase->set(1, 2);
pBase->showPoint();
//pBase->setColor("Red"); // 안됨
//pBase->showColorPoint(); // 안됨
//자식 개체 포인터 <= 부모 객체 포인터 대입
//다운 캐스팅
//업캐스팅이 되어야 다운캐스팅이 됨
pDer = (ColorPoint*)pBase;
pDer->setColor("green");
pDer->showColorPoint();
pDer->set(8, 8);
pDer->showPoint();
cout << endl;
Point g;
Point* pp = &g;
//업캐스팅
pp = pDer;
//다운 캐스팅
pDer = (ColorPoint*)pp;
pDer->set(7, 8);
pDer->setColor("Black");
pDer->showPoint();
pDer->showColorPoint();
return 0;
}
✅ **출력결과
—————————————————————————————————————————————**
(1, 2)
(100, 200)
Blue.(10, 20)
Yellow.(3, 4)
(1, 2)
green.(1, 2)
(8, 8)
(7, 8)
Black.(7, 8)
—————————————————————————————————————————————
#include <iostream>
using namespace std;
//2차원 평면에서 한 점을 표현하는 클래스
class Point {
int x, y;
public:
void set(int x, int y) {
this->x = x;
this->y = y;
}
void showPoint() {
cout << "(" << x << ", " << y << ")" << endl;
}
};
//컬러값이 추가된 파생 클래스
class ColorPoint : public Point {
string color;
public:
void setColor(string color) {
this->color = color;
}
void showColorPoint() {
cout << color << "." ;
showPoint();
}
};
int main()
{
ColorPoint cp;
ColorPoint* pDer = &cp;
Point *pBase = pDer;//업캐스팅
pDer->set(3,4);
pBase->showPoint();
pDer->setColor("Red");
pDer->showColorPoint();
//pBase->showColorPoint();//컴파일오류
return 0;
}
✅ **출력결과
—————————————————————————————————————————————**
(3, 4)
Red.(3, 4)
—————————————————————————————————————————————
#include <iostream>
using namespace std;
//2차원 평면에서 한 점을 표현하는 클래스
class Point {
int x, y;
public:
void set(int x, int y) {
this->x = x;
this->y = y;
}
void showPoint() {
cout << "(" << x << ", " << y << ")" << endl;
}
};
//컬러값이 추가된 파생 클래스
class ColorPoint : public Point {
string color;
public:
void setColor(string color) {
this->color = color;
}
void showColorPoint() {
cout << color << "." ;
showPoint();
}
};
int main()
{
ColorPoint cp;
ColorPoint *pDer;
Point *pBase = &cp;//업캐스팅
pBase->set(3,4);
pBase->showPoint();
pDer = (ColorPoint*)pBase;//다운캐스팅
pDer->setColor("Red");//정상컴파일
pDer->showColorPoint();//정상컴파일
return 0;
}
✅ **출력결과
—————————————————————————————————————————————**
(3, 4)
Red.(3, 4)
—————————————————————————————————————————————
#include <iostream>
using namespace std;
class A{
public:
A()
{
cout << "생성자 A" << endl;
}
~A()
{
cout << "소멸자 A" << endl;
}
};
class B : public A
{
public:
B()
{
cout << "생성자 B" << endl;
}
~B()
{
cout << "소멸자 B" << endl;
}
};
class C : public B
{
public:
C()
{
cout << "생성자 C" << endl;
}
~C()
{
cout << "소멸자 C" << endl;
}
};
int main(){
C c;
return 0;
}
✅ **출력결과
—————————————————————————————————————————————**
생성자 A
생성자 B
생성자 C
소멸자 C
소멸자 B
소멸자 A
—————————————————————————————————————————————
#include <iostream>
using namespace std;
class A{
public:
A()
{
cout << "생성자 A" << endl;
}
A(int x)
{
cout << "매개변수 생성자 A" << endl;
}
};
class B : public A
{
public:
B()
{
cout << "생성자 B" << endl;
}
};
int main(){
B b;
return 0;
}
✅ **출력결과
—————————————————————————————————————————————**
생성자 A
생성자 B
—————————————————————————————————————————————
#include <iostream>
using namespace std;
class A{
public:
A(int x)
{
cout << "매개변수 생성자 A" << endl;
}
};
class B : public A
{
public:
B()
{
cout << "생성자 B" << endl;
}
};
int main(){
B b;
return 0;
}
✅ **출력결과
—————————————————————————————————————————————**
Constructor for 'B' must explicitly initialize the base class 'A' which does not have a default constructor
—————————————————————————————————————————————
#include <iostream>
using namespace std;
class A{
public:
A()
{
cout << "생성자 A" << endl;
}
A(int x)
{
cout << "매개변수 생성자 A" << endl;
}
};
class B : public A
{
public:
B()
{
cout << "생성자 B" << endl;
}
B(int x)
{
cout << "매개변수 생성자 B(" << x << ")" << endl;
}
};
int main(){
B b(7);
return 0;
}
✅ **출력결과
—————————————————————————————————————————————**
생성자 A
매개변수 생성자 B(7)
—————————————————————————————————————————————
#include <iostream>
using namespace std;
class A{
public:
A()
{
cout << "생성자 A" << endl;
}
A(int x)
{
cout << "매개변수 생성자 A(" << x << ")" << endl;
}
};
class B : public A
{
public:
B()
{
cout << "생성자 B" << endl;
}
B(int x) : A(x+6)
{
cout << "매개변수 생성자 B(" << x << ")" << endl;
}
};
int main(){
B b(7);
return 0;
}
✅ **출력결과
—————————————————————————————————————————————**
매개변수 생성자 A(13)
매개변수 생성자 B(7)
—————————————————————————————————————————————
#include <iostream>
using namespace std;
class TV {
int size;
public:
TV()
{
size = 20;
}
TV(int size)
{
this->size = size;
}
int getSize()
{
return size;
}
};
class WideTV : public TV {
bool videoIn;
public:
WideTV(int size, bool videoIn) : TV(size) {
this->videoIn = videoIn;
//int h = getSize(); // 가능한거 같음 확인 다시 한번
//cout << h << endl;
}
bool getVideoIn() {
return videoIn;
}
};
class SmartTV : public WideTV {
string ipAddr;
public:
SmartTV(string ipAddr, int size) : WideTV(size, true) {
this->ipAddr = ipAddr;
}
string getIpAddr() {
return ipAddr;
}
};
int main()
{
SmartTV htv("192.0.0.1", 75);
cout << "size = " << htv.getSize() << endl;
cout << "VideoIn = " << htv.getVideoIn() << endl;
cout << "IP = " << htv.getIpAddr() << endl;
return 0;
}
✅ **출력결과
—————————————————————————————————————————————**
size = 75
VideoIn = 1
IP = 192.0.0.1
—————————————————————————————————————————————
#include <iostream>
using namespace std;
class Base
{
int a;
protected:
void setA(int a)
{
this->a = a;
}
public:
void showA()
{
cout << a;
}
};
class Derived : private Base
{
int b;
protected:
void setB(int b)
{
this->b = b;
}
public:
void showB()
{
cout << b;
}
};
int main()
{
Derived x;
x.a = 5; //에러
x.setA(10); //에러
x.showA(); //에러
x.b = 10; //에러
x.setB(10); //에러
x.showB();
return 0;
}
#include <iostream>
using namespace std;
class Base
{
int a;
protected:
void setA(int a)
{
this->a = a;
}
public:
void showA()
{
cout << "멋진 ";
}
};
class Derived : private Base
{
int b;
protected:
void setB(int b)
{
this->b = b;
}
public:
void showB()
{
showA();
cout << "개발자되기" << endl;
}
};
int main()
{
Derived x;
x.showB();
return 0;
}
✅ **출력결과
—————————————————————————————————————————————**
멋진 개발자되기
—————————————————————————————————————————————
#include <iostream>
using namespace std;
class Base {
int a;
protected:
void setA(int a)
{
this->a = a;
}
public:
void showA()
{
cout << a;
}
};
class Derived : protected Base
{
int b;
protected:
void setB(int b)
{
this->b = b;
}
public:
void showB()
{
cout << b;
}
};
int main()
{
Derived x;
x.a = 5; //에러
x.setA(10); //에러
x.showA(); //에러
x.b = 10; //에러
x.setB(10); //에러
x.showB();
return 0;
}
#include <iostream>
using namespace std;
class Base {
int a;
protected:
void setA(int a)
{
this->a = a;
}
public:
void showA()
{
cout << a << endl;
}
};
class Derived : protected Base
{
int b;
protected:
void setB(int b)
{
this->b = b;
}
public:
void showB()
{
cout << b << endl;
}
};
class test : public Derived
{
int c;
public:
void showTest()
{
setA(999);
showA();
setB(888);
showB();
}
};
int main()
{
test t;
t.showB();
t.showTest();
return 0;
}
✅ **출력결과
—————————————————————————————————————————————**
1
999
888
—————————————————————————————————————————————
#include <iostream>
using namespace std;
class Adder {
protected:
int add(int a, int b) {
return a + b;
}
};
class Substractor {
protected:
int minus(int a, int b) {
return a - b;
}
};
class Calculator : public Adder, public Substractor {
public:
int calc(char op, int a, int b) {
int res;
if (op == '+')
{
res = add(a, b);
}
else if (op == '-')
{
res = minus(a, b);
}
return res;
}
};
int main()
{
Calculator hCal;
cout << "2 + 4 = " << hCal.calc('+', 2, 4) << endl;
cout << "2 - 4 = " << hCal.calc('+', 2, 4) << endl;
return 0;
}
✅ **출력결과
—————————————————————————————————————————————**
2 + 4 = 6
2 - 4 = 6
—————————————————————————————————————————————
class Animal {
public:
void eat() { /* ... */ }
};
class Dog : public Animal {
public:
void bark() { /* ... */ }
};
class Cat : public Animal {
public:
void meow() { /* ... */ }
};
class CatDog : public Dog, public Cat {
public:
void beAwesome() { /* ... */ }
};
class Animal {
public:
void eat() { /* ... */ }
};
class Dog : virtual public Animal {
public:
void bark() { /* ... */ }
};
class Cat : virtual public Animal {
public:
void meow() { /* ... */ }
};
class CatDog : public Dog, public Cat {
public:
void beAwesome() { /* ... */ }
};