ex10_10MyInteger类

头文件ex10.h

#include <string>
 
class MyInteger{
    public:
        MyInteger();
        MyInteger(int);
        int getValue() const;
        bool isEven() const;
        bool isOdd() const;
        bool isPrime() const;
 
        static bool isEven(int);
        static bool isOdd(int);
        static bool isPrime(int);
 
        static bool isEven(const MyInteger&);
        static bool isOdd(const MyInteger&);
        static bool isPrime(const MyInteger&);
 
        bool equals(int) const;
        bool equals(const MyInteger&) const;
 
        static int parseInt(const std::string&);
    private:
        int value;
};#include <string>
 
class MyInteger{
    public:
        MyInteger();
        MyInteger(int);
        int getValue() const;
        bool isEven() const;
        bool isOdd() const;
        bool isPrime() const;
 
        static bool isEven(int);
        static bool isOdd(int);
        static bool isPrime(int);
 
        static bool isEven(const MyInteger&);
        static bool isOdd(const MyInteger&);
        static bool isPrime(const MyInteger&);
 
        bool equals(int) const;
        bool equals(const MyInteger&) const;
 
        static int parseInt(const std::string&);
    private:
        int value;
};

 

类的实现文件ex10.cp

#include "ex10.h"
#include <string>
#include <cstdlib>
 
MyInteger::MyInteger(){
    //nothing
}
 
MyInteger::MyInteger(int value){
    this->value = value;
}
 
int MyInteger::getValue() const{
    return value;
}
 
bool MyInteger::isEven() const{
    return isEven(value);
}
 
bool MyInteger::isOdd() const{
    return isOdd(value);
}
 
bool MyInteger::isPrime() const{
    return isPrime(value);
}
 
bool MyInteger::isEven(int newValue){
    return (newValue % 2) == 0;
}
 
bool MyInteger::isOdd(int newValue){
    return (newValue % 2) != 0;
}
 
bool MyInteger::isPrime(int newValue){
    if(newValue < 2)
        return false;
 
    for(int i = 2; i <= newValue / 2; ++i)
        if(newValue % i == 0)
            return false;
    return true;
}
 
bool MyInteger::isEven(const MyInteger &obj){
    return isEven(obj.getValue());
}
 
bool MyInteger::isOdd(const MyInteger &obj){
    return isOdd(obj.getValue());
}
 
bool MyInteger::isPrime(const MyInteger &obj){
    return isPrime(obj.getValue());
}
 
bool MyInteger::equals(int newValue) const{
    return value == newValue;
}
 
bool MyInteger::equals(const MyInteger &obj) const{
    return value == obj.getValue();
}
 
int MyInteger::parseInt(const std::string &s){
    return atoi(s.c_str());     //使用atoi函数前把字符串转化为C风格
}

 

测试文件main.cpp

#include <iostream>
#include "ex10.h"
using namespace std;
 
int main(){
    MyInteger mi(5);
    cout << "mi.getValue() = " << mi.getValue() << endl;
    cout << "mi.isEven() = " << mi.isEven() << endl;
    cout << "mi.isPrime() = " << mi.isPrime() << endl;
 
    cout << "11 is Prime? " << MyInteger::isPrime(11) << endl;
    cout << "isEven(mi) = " << MyInteger::isEven(mi) << endl;
    return 0;
}

 

转载于:https://www.cnblogs.com/mocuishle/p/7979137.html

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值