void PositiveInteger :: Clear() { SetValue(0); } // -------------------------------------------- ----------------------- bool PositiveInteger :: SetValue(int value) { if(value< 0) { m_value = 0; //如果是否定的,则设为0 返回false; } { m_value = value; 返回true; } } / / ------------------------------------------------- ------------------ PositiveInteger& PositiveInteger :: operator =(const PositiveInteger& posInt) { if(this!=& posInt) SetValue(posInt.m_value); return *这个; } // ------------------------- ------------------------------------------ bool PositiveInteger :: operator< (const PositiveInteger& posInt) { if(m_value< posInt.m_value) 返回true; else 返回false; } // ------------- -------------------------------------------------- ---- bool PositiveInteger :: operator> (const PositiveInteger& posInt) { if(m_value> posInt.m_value) 返回true; else 返回false; } // ------------- -------------------------------------------------- ---- bool PositiveInteger :: operator ==(const PositiveInteger& posInt) { if(m_value == posInt.m_value) 返回true; else 返回false; } // ---------------------------------------- --------------------------- ostream&运算符<< (ostream& ostr,const PositiveInteger& posInt) { ostr<< 价值: << posInt.m_value; 返回ostr; } // ------------ -------------------------------------------------- ----- istream&运算符<< (istream& istr,PositiveInteger& posInt) { int value; do { cout<< 输入一个正整数:; istr>>价值; } while(!posInt.SetValue(value)); 返回istr; } // ---------------------------------------- ---------------------------// file: PositiveInteger.cpp#include "PositiveInteger.h"//-------------------------------------------------------------------PositiveInteger::PositiveInteger(){Clear();}//-------------------------------------------------------------------PositiveInteger::PositiveInteger(const PositiveInteger &posInt){SetValue(posInt.m_value);}//-------------------------------------------------------------------/*PositiveInteger::~PositiveInteger(){// do nothing}*///-------------------------------------------------------------------void PositiveInteger::Clear(){SetValue(0);}//-------------------------------------------------------------------bool PositiveInteger::SetValue(int value){if (value < 0){m_value = 0; // if negative, set to 0return false;}else{m_value = value;return true;}}//-------------------------------------------------------------------PositiveInteger &PositiveInteger::operator = (const PositiveInteger &posInt){if (this != &posInt)SetValue(posInt.m_value);return *this;}//-------------------------------------------------------------------bool PositiveInteger::operator < (const PositiveInteger &posInt){if (m_value < posInt.m_value)return true;elsereturn false;}//-------------------------------------------------------------------bool PositiveInteger::operator > (const PositiveInteger &posInt){if (m_value > posInt.m_value)return true;elsereturn false;}//-------------------------------------------------------------------bool PositiveInteger::operator == (const PositiveInteger &posInt){if (m_value == posInt.m_value)return true;elsereturn false;}//-------------------------------------------------------------------ostream& operator << (ostream &ostr, const PositiveInteger &posInt){ostr << "Value: " << posInt.m_value;return ostr;}//-------------------------------------------------------------------istream& operator << (istream &istr, PositiveInteger &posInt){int value;do{cout << "Enter a positive integer: ";istr >> value;}while(!posInt.SetValue(value));return istr;}//------------------------------------------------------------------- //文件:Circle.h #ifndef CIRCLE_H #define CIRCLE_H // ----------- -------------------------------------------------- ------ #include< iostream.h> #include" PositiveInteger.h" // -------------------------------------------- ----------------------- class Circle { public: / * constructors * / Circle(); //默认 Circle(int radius,int x,int y); Circle(const Circle& circle); //复制 ~Circle(){}; //析构函数 void Clear(); //清除圆圈为0 .....不确定它是如何工作的? / *设置方法* / bool SetRadius(int radius); bool SetX(int); bool SetY(int); / * get methods * / int GetRadius(){return m_radius.GetValue();} int GetX()const {return m_x;} int GetY()const {return m_y;} double GetPerimeter(); double GetArea(); //如何处理这些? / *输入/输出方法* / //朋友ostream&运算符<< (ostream& ostr,const Circle& circle); //朋友istream&运算符>> (istream& istr,Circle& circle); private: PositiveInteger m_radius; int m_x; int m_y; } #endif// file: Circle.h#ifndef CIRCLE_H#define CIRCLE_H//-------------------------------------------------------------------#include <iostream.h>#include "PositiveInteger.h"//-------------------------------------------------------------------class Circle{public:/* constructors */Circle(); // defaultCircle(int radius, int x, int y);Circle(const Circle &circle); //copy~Circle() {}; // destructorvoid Clear(); // clear circle to 0.....not sure how it works?/* set methods */bool SetRadius(int radius);bool SetX(int);bool SetY(int);/* get methods */int GetRadius() {return m_radius.GetValue();}int GetX() const {return m_x;}int GetY() const {return m_y;}double GetPerimeter();double GetArea();//what to do for these?/* input/output methods *///friend ostream& operator << (ostream &ostr, const Circle &circle);//friend istream& operator >> (istream &istr, Circle &circle);private:PositiveInteger m_radius;int m_x;int m_y;}#endif 这篇关于帮助我的代码?类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 06-11 19:35