본문 바로가기
문제풀이/명품 C++ Programming

[명품 C++ Programming] 3장 Open Challenge

by 민됴리 2020. 12. 11.
반응형

2018년에 3월 5일에 발행된

황기태 저자의

명품 C++ Programming 개정판

3장 Open Challenge 문제입니다.

 

저작권을 준수하기 위해서

책에 나와있는 문제는 적지 않고

정답만 적었습니다.

 

궁금한 점은 댓글로 남겨주세요.

 


 

Exp.h

//Exp.h
#ifndef EXP_H
#define EXP_H

class Exp {
	int base;
	int exp;
	int value;
public:
	Exp();
	Exp(int base);
	Exp(int base, int exp);
	int getValue();
	int getBase();
	int getExp();
	bool Equal(Exp other);
};

#endif

 

Exp.cpp

//Exp.cpp
#include <cmath>
#include "Exp.h"

Exp::Exp() {

	base = 1;
	exp = 1;
	value = pow(1, 1);
}

Exp::Exp(int base) {
	this->base = base;
	exp = 1;
	value = pow(base, 1);
}

Exp::Exp(int base, int exp) {
	this->base = base;
	this->exp = exp;
	value = pow(base, exp);
}

int Exp::getValue() {
	return value;
}

int Exp::getBase() {
	return base;
}

int Exp::getExp() {
	return exp;
}

bool Exp::Equal(Exp other) {
	if (this->value == other.value)
		return true;
	else
		return false;
}

 

main.cpp

//main.cpp
#include <iostream>
using namespace std;

#include "Exp.h"

int main(void) {
	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.Equal(b))
		cout << "same" << endl;
	else
		cout << "not same" << endl;

	return 0;
}

 

반응형