프로그래밍 공부

명품 C++ Programming CHAPTER 03 OpenChallenge 본문

Programming/명품 C++ Programming

명품 C++ Programming CHAPTER 03 OpenChallenge

khj1999 2021. 2. 22. 17:21

exp.h 파일

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//exp_header
#ifndef EXP_H
#define EXP_H
#include<iostream>
using namespace std;
 
class Exp {
public:
    int x, y;
    Exp();
    Exp(int x);
    Exp(int x, int y);
    //~Exp();
    int getValue();
    int getBase();
    int getExp();
    bool equals(Exp b);
};
#endif
cs

 

exp.cpp 파일

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
32
33
34
35
36
37
38
39
40
//exp_CPP
#include "exp.h"
#include <iostream>
using namespace std;
 
Exp::Exp() {
    x = 1; y = 1;
}
Exp::Exp(int n) {
    x = n; y = 1;
}
Exp::Exp(int n, int m) {
    x = n; y = m;
}
 
/*Exp::~Exp() {
    cout << "객체 소멸\n";
}*/
 
int Exp::getValue() {
    int n = 1;
    for (int i = 0; i < y; i++) {
        n *= x;
    }
    return n;
}
int Exp::getBase() {
    return x;
}
int Exp::getExp() {
    return y;
}
bool Exp::equals(Exp b) {
    if (getValue() == b.getValue()) {
        return true;
    }
    else {
        return false;
    }
}
cs

 

main.cpp 파일

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//Main function_CPP
#include "exp.h"
#include <iostream>
using namespace std;
 
 
int main() {
    Exp a(32);
    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;
 
    return 0;
}
cs

 

실행결과

 

Comments