1. 문제
6. int 타입의 정수를 객체화한 Integer 클래스를 작성하라. Integer의 모든 멤버 함수를 자동 인라인으로 작성하라. Integer 클래스를 활용하는 코드는 다음과 같다.
2. 결과
3. 코드
#include <iostream>
#include <string>
using namespace std;
class Integer {
int num;
public :
Integer(int n) { num = n; }
Integer(string s) { num = stoi(s); }
int get() { return num; }
void set(int n) { num = n ; }
bool isEven() { if( num % 2 == 0) return true; else return false; }
};
int main() {
Integer n(30);
cout << n.get() << ' ';
n.set(50);
cout << n.get() << ' ';
Integer m("300");
cout << m.get() << ' ';
cout << m.isEven();
}
4. 설명
실행결과의 맞게 각 함수들과 생성자를 구현하였습니다.
클래스 내의 인라인 함수는 구현부 길이가 길지 않을 때 사용하시는 것이 좋습니다.
'명품 C++ programming' 카테고리의 다른 글
명품 C++ programming 실습문제 3장 8번 (1) (0) | 2019.04.06 |
---|---|
명품 C++ programming 실습문제 3장 7번 (1) | 2019.04.06 |
명품 C++ programming 실습문제 3장 5번 (0) | 2019.04.06 |
명품 C++ programming 실습문제 3장 4번 (0) | 2019.04.06 |
명품 C++ programming 실습문제 3장 3번 (0) | 2019.04.06 |