第1关:完整的包装类

编程入门 行业动态 更新时间:2024-10-19 14:26:53

第1关:<a href=https://www.elefans.com/category/jswz/34/1771399.html style=完整的包装类"/>

第1关:完整的包装类

题目: 

根据给定头文件写cpp文件

/********** BEGIN **********/
#include"Int.h"int Int::getValue()const{return value;}
void Int::setValue(int v){value=v;}
Int::Int(){value=0;}
Int::Int(int v){value=v;}
Int::Int(const Int&rhs){this->value=rhs.getValue();}
Int::~Int(){ }Int& Int::operator=(const Int&rhs){this->value=rhs.getValue();return *this;}
Int& Int::operator+=(const Int&rhs){this->value+=rhs.getValue();return *this;}
Int& Int::operator-=(const Int&rhs){this->value-=rhs.getValue();return *this;}
Int& Int::operator*=(const Int&rhs){this->value*=rhs.getValue();return *this;}
Int& Int::operator/=(const Int&rhs){this->value/=rhs.getValue();return *this;}
Int& Int::operator%=(const Int&rhs){this->value%=rhs.getValue();return *this;}Int& Int::operator++(){this->value+=1;return *this;}
Int& Int::operator--(){this->value-=1;return *this;}Int Int::operator++(int tmp){Int temp=*this;this->value+=1;return temp;}
Int Int::operator--(int tmp){Int temp=*this;this->value-=1;return temp;}/********** END **********/

 

 

 

/********** BEGIN **********/
#ifndef _INTOP_H_
#define _INTOP_H_
#include"Int.h"
#include <iostream>const Int operator+(const Int&lhs,const Int&rhs)
{return lhs.getValue()+rhs.getValue();}
const Int operator-(const Int&lhs,const Int&rhs)
{return lhs.getValue()-rhs.getValue();}
const Int operator*(const Int&lhs,const Int&rhs)
{return lhs.getValue()*rhs.getValue();}
const Int operator/(const Int&lhs,const Int&rhs)
{return lhs.getValue()/rhs.getValue();}
const Int operator%(const Int&lhs,const Int&rhs)
{return lhs.getValue()%rhs.getValue();}bool operator==(const Int&lhs,const Int&rhs)
{return lhs.getValue()==rhs.getValue();}
bool operator!=(const Int&lhs,const Int&rhs)
{return lhs.getValue()!=rhs.getValue();}
bool operator<(const Int&lhs,const Int&rhs)
{return lhs.getValue()<rhs.getValue();}
bool operator<=(const Int&lhs,const Int&rhs)
{return lhs.getValue()<=rhs.getValue();}
bool operator>(const Int&lhs,const Int&rhs)
{return lhs.getValue()>rhs.getValue();}
bool operator>=(const Int&lhs,const Int&rhs)
{return lhs.getValue()>=rhs.getValue();}std::ostream& operator<<(std::ostream& os,Int const& rhs)
{os<<rhs.getValue();return os;}
std::istream& operator>>(std::istream& is,Int&  rhs)
{int n;is>>n;rhs.setValue(n);return is;}#endif 
/********** END **********/

 考点:

1.自增自减运算符重载,区分前后!!(后置重载函数会传入形式参数,但这只作为区别于前置重载函数的特征,并不会真的传参,故在函数内部不可以!!!)

2.输入输出运算符重载,注意输入传参无const 

更多推荐

第1关:完整的包装类

本文发布于:2023-11-15 06:11:40,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1595038.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:完整

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!