실수의 지수 표현을 클래스 Exp로 작성하라. Exp를 이용하는 main() 함수의 실행 결과는 다음과 같다. 클래스 Exp를 Exp.h 헤더 파일과 Exp.cpp 파일로 분리하여 작성하라.

Exp.h
#ifndef Exp_H
#define Exp_H
class Exp {
int base, exp;
public:
Exp();
Exp(int a);
Exp(int a, int b);
int getValue();
int getExp();
int getBase();
bool equals(Exp b);
};
#endif
Exp.cpp
#include "Exp.h"
Exp::Exp() {
base = exp = 1;
}
Exp::Exp(int a) {
base = a;
exp = 1;
}
Exp::Exp(int a, int b) {
base = a;
exp = b;
}
int Exp::getBase() {
return base;
}
int Exp::getExp() {
return exp;
}
int Exp::getValue() {
int result = 1;
for (int i = 0; i < exp; i++) {
result *= base;
}
return result;
}
bool Exp::equals(Exp b) {
return getValue() == b.getValue();
}
main.cpp
#include <iostream>
using namespace std;
#include "Exp.h"
int main() {
Exp a(3, 2);
Exp b(9);
Exp c;
cout << a.getValue() << ' ' << b.getValue() << ' ' << c.getValue() << endl;
cout << "a의 베이스 " << a.getBase() << ',' << "지수 " << a.getExp() << endl;
if (a.equals(b))
cout << "same" << endl;
else
cout << "not same" << endl;
}'C++' 카테고리의 다른 글
| [명품 C++ Programming] 5장 실습 문제 (0) | 2025.04.27 |
|---|---|
| [명품 C++ Programming] 3장 실습 문제 (1) | 2025.04.26 |
| [명품 C++ Programming] 4장 실습 문제 (0) | 2025.04.08 |
| [명품 C++ Programming] 2장 실습 문제 (2) | 2025.01.14 |
| [명품 C++ Programming] 2장 Open Challenge (6) | 2025.01.13 |