14

C++ Programming - 10th Study

  • Upload
    -

  • View
    457

  • Download
    6

Embed Size (px)

Citation preview

Page 1: C++ Programming - 10th Study
Page 2: C++ Programming - 10th Study
Page 3: C++ Programming - 10th Study

3

Page 4: C++ Programming - 10th Study

4

Page 5: C++ Programming - 10th Study

5

Page 6: C++ Programming - 10th Study

Shape

Circle

Rect

Triangle

Shape.getArea();

만약 도형이

원이라면 사각형이라면 삼각형이라면

Circle.getArea(); Rect.getArea(); Triangle.getArea();

원주율(𝜋𝜋) × (반지름)2 폭 × 높이 (밑변 × 높이) ÷ 2

6

Page 7: C++ Programming - 10th Study

class Shape{protected:

std::string name;int width;int height;

public:Shape(std::string _name, int _width, int _height)

: name(std::move(_name)), width(_width), height(_height) { }void printArea() { std::cout << "The area of " << name.c_str()

<< " is " << getArea() << std::endl; }virtual double getArea() { return 0; };

};

7

Page 8: C++ Programming - 10th Study

class Circle : public Shape{protected:

const double PI = 3.14;public:

Circle(std::string _name, int _width, int _height): Shape(std::move(_name), _width, _height) { }

virtual double getArea() { return PI * std::pow(width * 0.5, 2); };};

class Rect : public Shape{public:

Rect(std::string _name, int _width, int _height): Shape(std::move(_name), _width, _height) { }

virtual double getArea() { return width * height; };};

8

Page 9: C++ Programming - 10th Study

class Triangle : public Shape{public:

Triangle(std::string _name, int _width, int _height): Shape(std::move(_name), _width, _height) { }

virtual double getArea() { return width * height * 0.5; };};

9

Page 10: C++ Programming - 10th Study

int main(){

Shape* s = new Shape("Shape", 5, 5);s->printArea();

s = new Circle("Circle", 5, 5);s->printArea();

s = new Rect("Rectangle", 5, 5);s->printArea();

s = new Triangle("Triangle", 5, 5);s->printArea();

}

10

Page 11: C++ Programming - 10th Study
Page 12: C++ Programming - 10th Study

12

Page 13: C++ Programming - 10th Study

virtual double getArea() = 0;virtual double getArea() { return 0; };

Shape* s = new Shape("Shape", 5, 5);

Shape* s = new Circle("Circle", 5, 5);

13

Page 14: C++ Programming - 10th Study

class Super{public:

virtual void f() { }};

class Super{public:

virtual void f() = 0;};

class Sub : public Super{

};

int main(){

Sub sub;sub.f();

}

class Sub : public Super{

// 구현하지 않을 경우// 오류 발생virtual void f() { }

};

int main(){

Sub sub;sub.f();

}

14