Menu
CoddyTech

복습: 다형적 가격

Coddy의 Luau 개발자를 위한 C++ 코스 레슨. 48개 중 42번째.

challenge icon

챌린지

쉬움

입력: 음수가 아닌 단가, 수량, 0부터 100까지의 정수 할인율입니다. Price는 추상 클래스입니다. FullPrice는 단가와 수량을 곱한 값을 반환하고, DiscountPrice는 할인율만큼 금액을 줄이며 소수 부분은 버립니다. 드라이버는 Price 참조를 통해 두 클래스의 메서드를 호출합니다.

편집 가능한 모듈: Price.cpp, Price.hpp, FullPrice.cpp, FullPrice.hpp, DiscountPrice.cpp, DiscountPrice.hpp. 각 필수 타입은 고유한 이름의 헤더와 구현 파일을 가집니다. 모든 공개 시그니처를 유지하세요. 잠겨 있는 main.cpp는 stdin을 읽고 API를 직접 호출합니다. 또한 명시된 경우 공개 API 동작을 독립적으로 확인합니다. API 내부에서 출력하지 말고 값을 반환하세요. 정수 입력과 결과는 signed int 범위에 들어갑니다. 별도로 명시하지 않는 한 텍스트는 ASCII입니다.

직접 해보기

#include "Price.hpp"
#include "FullPrice.hpp"
#include "DiscountPrice.hpp"
#include <iostream>
int main(){
{FullPrice f;DiscountPrice d(25);const Price& p=d;if(f.cost(3,4)!=12||p.cost(4,4)!=12||p.cost(7,2)!=10)return 21;}
std::vector<std::string> rows; std::string line; while(std::getline(std::cin,line)) rows.push_back(line);
std::istringstream in(rows.empty()?"":rows[0]);int u,q,p;in>>u>>q>>p;FullPrice a;DiscountPrice b(p);const Price& x=a;const Price& y=b;std::cout<<x.cost(u,q)<<" "<<y.cost(u,q)<<"\n";
}

Luau 개발자를 위한 C++의 모든 레슨

직접 연습해 보세요: 온라인 C++ 컴파일러