Notice
Recent Posts
Recent Comments
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 코딩테스트
- 개발자
- java정리
- 자바정리
- 사물인터넷
- 풀스택프레임워크
- C++
- 백준
- Reflex
- 코테
- SNS
- 코스닥
- 프로그래머스
- 모의 주식
- 자바
- 주가예측
- 자바독학
- backend
- Project
- Python
- 빅데이터
- 프로그램
- 개발
- 코딩
- 이웃한 칸
- java
- frontend
- 코딩독학
- 개인프로젝트
- 프로그래밍
Archives
- Today
- Total
Creative Code
shape(도형 상속) 본문
※main.cpp파일
#include <iostream>
#include "shape.h"
#include "rectangle.h"
#include "circle.h"
#include <typeinfo>
void printArea(Shape *ps) {
std::cout << "area : " << ps->area() << std::endl;
}
void printShape(const Shape *ps) {
if (typeid(*ps) == typeid(Rectangle)) {
std::cout << "rectangle area : " << ps->area() << '\t';
const Rectangle *p = (const Rectangle *)ps;
std::cout << "diagonal : " <<
p->getDiagonal() << std::endl;
// RTTI : RunTime Type Indentification 실행시간에 부모클래스 타입의 포인터가 가리키는 객체의 타입을 알기 위할 때
} else if (typeid(*ps) == typeid(Circle)) {
std::cout << "circle area : " << ps->area() << '\t';
const Circle *p = (const Circle *)ps;
std::cout << "circumference : " <<
p->getCircumference() << std::endl;
}
}
int main() {
//Shape s(100,100); --> 추상클래스의 객체는 만들수 없다.
Shape *pShapes[5];
pShapes[0] = new Rectangle(10,10,20,5);
pShapes[1] = new Circle(50,50,5);
pShapes[2] = new Rectangle(0,0,100,20);
pShapes[3] = new Rectangle(100,100,100,100);
pShapes[4] = new Circle(100,100,50);
for (int i = 0; i<5; ++i) {
printArea(pShapes[i]);
}
for (int i = 0; i<5; ++i) {
printShape(pShapes[i]);
}
for (int i = 0; i<5; ++i) {
delete pShapes[i];
}
return 0;
}
※shape.h파일
#ifndef SHAPE_H
#define SHAPE_H
class Shape { //Abstract Base Class (추상기본 클래스) 순수가상함수를 1개이상 가지는 클래스
private:
protected:
int x_;
int y_;
Shape(const Shape& rhs);
Shape& operator=(const Shape& rhs);
public:
Shape(int x, int y);
virtual ~Shape() {}
void move(int offsetX, int offsetY);
virtual double area() const = 0;
//구현할수 없는 함수라는 뜻으로 =0을 붙여준다.(pure virtual function)
//내용은 없고 인터페이스만 존재하는 함수(자식 클래스에서 area함수를 강제로 오버라이딩 시키기 위해 사용)
};
#endif
//C++에서는 부모클래스를 Base class, 자식클래스를 derived class라고 부른다.
//추상클래스 타입의 객체는 만들 수 없다. -> 상속하기 위한 용도
※shape.cpp파일
#include "shape.h"
Shape::Shape(int x,int y)
:x_(x),y_(y)
{
}
void Shape::move(int offsetX, int offsetY)
{
x_ += offsetX;
y_ += offsetY;
}
/*
double Shape::area() const
{
//모든 도형에 대한 넓이를 구현할 수 없음
}
*/
※rectangle.h파일
#ifndef RECTANGLE_H
#define RECTANGLE_H
#include "shape.h"
class Rectangle : public Shape {
private:
int width_;
int height_;
Rectangle(const Rectangle& );
Rectangle& operator=(const Rectangle& );
protected:
public:
Rectangle(int x, int y, int width, int height);
virtual ~Rectangle() { }
virtual double area() const;
double getDiagonal() const;
};
#endif
※rectangle.cpp파일
#include "rectangle.h"
#include <cmath>
Rectangle::Rectangle(int x, int y, int width, int height)
:Shape(x,y),width_(width),height_(height)
{
}
double Rectangle::area() const {
return width_ * height_;
}
double Rectangle::getDiagonal() const {
return sqrt(width_ * width_ + height_ * height_);
}
※circle.h파일
#ifndef CIRCLE_H
#define CIRCLE_H
#include "shape.h"
class Circle : public Shape {
private:
int radius_;
Circle(const Circle& );
Circle& operator=(const Circle& );
protected:
public:
Circle(int x, int y, int radius);
virtual ~Circle() {}
virtual double area() const;
double getCircumference() const;
};
#endif
※circle.cpp파일
#include "circle.h"
Circle::Circle(int x, int y, int radius)
:Shape(x,y),radius_(radius)
{
}
double Circle::area() const {
return radius_ * radius_ * 3.141592;
}
double Circle::getCircumference() const {
return 2 * radius_ * 3.141592;
}
'C++ Programming' 카테고리의 다른 글
array2(template사용) (0) | 2023.09.08 |
---|---|
boundArray(index지정) (0) | 2023.09.07 |
safeArray2(상속) (0) | 2023.09.07 |
queue3(array클래스와 has -a관계) (0) | 2023.09.06 |
queue2(생성자 초기화 리스트, static member, explicit) (0) | 2023.09.06 |