//상속
/*
#include<iostream>
#include<string>
using namespace std;
class Point
{
int x, y;
public:
void set(int x, int y);
void showPoint();
};
void Point::set(int x, int y)
{
this->x = x;
this->y = y;
}
void Point::showPoint()
{
cout << "(" << x << "," << y << ")" << endl;
}
class ColorPoint :public Point
{
string color;
public:
void setColor(string color);
void showColorPoint();
};
void ColorPoint::setColor(string color)
{
this->color = color;
}
void ColorPoint::showColorPoint()
{
cout << color << ":";
showPoint();
}
int main()
{
Point p;
ColorPoint cp;
cp.set(3, 4);
cp.setColor("Red");
cp.showColorPoint();
}*/
//업캐스팅
/*
#include<iostream>
#include<string>
using namespace std;
class Point
{
int x, y;
public:
void set(int x, int y);
void showPoint();
};
void Point::set(int x, int y)
{
this->x = x;
this->y = y;
}
void Point::showPoint()
{
cout << "(" << x << "," << y << ")" << endl;
}
class ColorPoint :public Point
{
string color;
public:
void setColor(string color);
void showColorPoint();
};
void ColorPoint::setColor(string color)
{
this->color = color;
}
void ColorPoint::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();
}*/
//다운캐스팅
/*
#include<iostream>
#include<string>
using namespace std;
class Point
{
int x, y;
public:
void set(int x, int y);
void showPoint();
};
void Point::set(int x, int y)
{
this->x = x;
this->y = y;
}
void Point::showPoint()
{
cout << "(" << x << "," << y << ")" << endl;
}
class ColorPoint :public Point
{
string color;
public:
void setColor(string color);
void showColorPoint();
};
void ColorPoint::setColor(string color)
{
this->color = color;
}
void ColorPoint::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();
}*/
//상속 관계와 생성자 매개 변수 전달
/*
#include<iostream>
#include<string>
using namespace std;
class TV
{
int size;
public:
TV();
TV(int size);
int getSize();
};
TV::TV()
{
size = 20;
}
TV::TV(int size)
{
this->size = size;
}
int TV::getSize()
{
return size;
}
class WideTV :public TV
{
bool videoIn;
public:
WideTV(int size, bool videoIn);
bool getVideoIn();
};
WideTV::WideTV(int size, bool videoIn) :TV(size)
{
this->videoIn = videoIn;
}
bool WideTV::getVideoIn()
{
return videoIn;
}
class SmartTV :public WideTV
{
string ipAddr;
public:
SmartTV(string ipAddr, int size);
string getIpAddr();
};
SmartTV::SmartTV(string ipAddr, int size) :WideTV(size, true)
{
this->ipAddr = ipAddr;
}
string SmartTV::getIpAddr()
{
return ipAddr;
}
int main()
{
SmartTV htv("192.0.0.1", 32);
cout << "size=" << htv.getSize() << endl;
cout << "videoIn=" << htv.getVideoIn() << endl;
cout << "IP=" << htv.getIpAddr() << endl;
}*/
//private 상속 사례
/*
#include<iostream>
using namespace std;
class Base
{
int a;
protected:
void setA(int a);
public:
void showA();
};
void Base::setA(int a)
{
this->a = a;
}
void Base::showA()
{
cout << a;
}
class Derived :private Base
{
int b;
protected:
void setB(int b);
public:
void showB();
};
void Derived::setB(int b)
{
this->b = b;
}
void Derived::showB()
{
cout << b;
}
int main()
{
Derived x;
x.a = 5; //a는 Derived 클래스에 상속되지만 private 멤버이므로 접근 불가
x.setA(10); //setA(),showA()는 private 멤버로 변경되어 Derived 클래스에 상속되기 때문에 Derived 클래스 외부에서 접근불가
x.showA(); //setA(),showA()는 private 멤버로 변경되어 Derived 클래스에 상속되기 때문에 Derived 클래스 외부에서 접근불가
x.b = 10; //b는 private 멤버이기 때문에 Derived 클래스 외부에서 접근 불가
x.setB(10); //setB()는 protected멤버이기 때문에 main()에서 접근 불가
x.showB(); //정상
}
*/
// protected 상속 사례
/*
#include<iostream>
using namespace std;
class Base
{
int a;
protected:
void setA(int a);
public:
void showA();
};
void Base::setA(int a)
{
this->a = a;
}
void Base::showA()
{
cout << a;
}
class Derived :protected Base
{
int b;
protected:
void setB(int b);
public:
void showB();
};
void Derived::setB(int b)
{
this->b = b;
}
void Derived::showB()
{
cout << b;
}
int main()
{
Derived x;
x.a = 5; //a는 Derived 클래스에 상속되지만 private 멤버이므로 접근 불가
x.setA(10); //setA(),showA()는 protected 멤버로 변경되어 Derived 클래스에 상속되기 때문에 Derived 클래스 외부에서 접근불가
x.showA(); //setA(),showA()는 protected 멤버로 변경되어 Derived 클래스에 상속되기 때문에 Derived 클래스 외부에서 접근불가
x.b = 10; //b는 private 멤버이기 때문에 Derived 클래스 외부에서 접근 불가
x.setB(10); //setB()는 protected멤버이기 때문에 main()에서 접근 불가
x.showB(); //정상
}*/
//상속이 중첩될 때 접근 지정 사례
/*
#include<iostream>
using namespace std;
class Base
{
int a;
protected:
void setA(int a);
public:
void showA();
};
void Base::setA(int a)
{
this->a = a;
}
void Base::showA()
{
cout << a;
}
class Derived :private Base
{
int b;
protected:
void setB(int b);
public:
void showB();
};
void Derived::setB(int b)
{
this->b = b;
}
void Derived::showB()
{
setA(5);
showA();
cout << b;
}
class GrandDerived :private Derived
{
int c;
protected:
void setAB(int x);
};
void GrandDerived::setAB(int x)
{
setA(x); //setA()는 private 속성으로 변경되어 Derived 클래스에 상속되기 때문에 GrandDerived 클래스에서 접근 불가
showA(); //showA()는 private 속성으로 변경되어 Derived 클래스에 상속되기 때문에 GrandDerived 클래스에서 접근 불가
setB(x);
}*/
//다중 상속받는 클래스 작성
/*
#include<iostream>
using namespace std;
class Adder
{
protected:
int add(int a, int b);
};
int Adder::add(int a, int b)
{
return a + b;
}
class Subtractor
{
protected:
int minus(int a, int b);
};
int Subtractor::minus(int a, int b)
{
return a - b;
}
class Calculator :public Adder, public Subtractor
{
public:
int calc(char op, int a, int b);
};
int Calculator::calc(char op, int a, int b)
{
int res = 0;
switch (op)
{
case'+':
res = add(a, b);
break;
case '-':
res = minus(a, b);
}
return res;
}
int main()
{
Calculator handCalculator;
cout << "2 + 4 = " << handCalculator.calc('+', 2, 4) << endl;
cout << "100 - 8 = " << handCalculator.calc('-', 100, 8) << endl;
}*/
//범위 지정 연산자를 이용한 기본 클래스의 가상 함수 호출
/*
#include<iostream>
using namespace std;
class Shape
{
public:
virtual void draw();
};
void Shape::draw()
{
cout << "--Shape--";
}
class Circle : public Shape
{
public:
int x;
virtual void draw();
};
void Circle::draw()
{
Shape::draw();
cout << "--Circle--" << endl;
}
int main()
{
Circle circle;
Shape* pShape = &circle;
pShape->draw();
pShape->Shape::draw();
}*/
#include<iostream>
#include"Circle.h"
#include"Line.h"
#include"Rect.h"
#include"Shape.h"
using namespace std;
int main()
{
Shape* pStart = NULL;
Shape* pLast;
pStart = new Circle();
pLast = pStart;
pLast = pLast->add(new Rect());
pLast = pLast->add(new Circle());
pLast = pLast->add(new Line());
pLast = pLast->add(new Rect());
//pLast = pLast->add(new Shape());
Shape* p = pStart;
while (p != NULL)
{
p->paint();
p = p->getNext();
}
p = pStart;
while (p != NULL)
{
Shape* q = p->getNext();
delete p;
p = q;
}
}