【C++学习笔记】常见运算符重载
2018-06-17 21:43:10来源:未知 阅读 ()
本文为学习笔记 没有说明c++重载的语法细节。具体参考MOOC 程序设计与算法三 ,
赋值运算符重载:
#include<iostream>
#include<cstring>
using namespace std;
class String{
private:
char *str;
public:
String():str(new char[1]){
str[0]=0;
}
String(const String & s){//重写赋值构造函数
str=new char[strlen(s.str)+1];
strcpy(str,s.str);
}
const char * c_str(){
return str;
}
String & operator =(const char *s);//解决s1=“hello world”问题
~String(){
delete [] str;
}
String & operator =(const String & s);//解决s1=s2问题
};
String & String::operator = (const char * s){
delete [] str;
str=new char[strlen(s)+1];
strcpy(str,s);
return *this;
}
String & String:: operator =(const String & s){
if(this==&s){ //防止出现s=s问题
return *this;
}
delete [] str;
str=new char[strlen(s.str)+1];
strcpy(str,s.str);
return *this;
}
int main(void){
String s;
s="good luck";
cout<<s.c_str()<<endl;
s="welcome to AHPU!";
cout<<s.c_str()<<endl;
return 0;
}
加号运算符重载:
#include<iostream>
using namespace std;
class Complex{
private:
double real,imag;
public:
Complex(){
}
Complex(double r,double i):real(r),imag(i){
}
Complex operator +(double x);
Complex operator +(const Complex & c);
friend Complex operator +(double r,const Complex & c);
double getReal(){
return this->real;
}
double getImag(){
return this->imag;
}
};
Complex Complex::operator +(const Complex & c){
Complex t;
t.real=this->real+c.real;
t.imag=this->imag+c.imag;
return t;
}
Complex Complex::operator +(double x){
return Complex(this->real+x,this->imag);
}
Complex operator +(double r,const Complex & c){
return Complex(c.real+r,c.imag);
}
int main(void){
Complex s1(1,2);
Complex s2(3,4);
Complex s3;
s3=s1+s2;
Complex s4,s5;
s4=s3+4;
s5=4+s1;
cout<<s3.getReal()<<","<<s3.getImag()<<endl;
cout<<s4.getReal()<<","<<s4.getImag()<<endl;
cout<<s5.getReal()<<","<<s5.getImag()<<endl;
return 0;
}
左移运算符重载:
#include<iostream>
using namespace std;
class Cstudent{
public:
int nAge;
};
ostream & operator <<(ostream & o,const Cstudent & s){
o<<s.nAge;
return o;
}
int main (void){
Cstudent s;
s.nAge=18;
cout<<s<<"hello"<<endl; //18hello
return 0;
}
左移和右移运算符重载:
#include<iostream>
using namespace std;
class Complex{
private:
double real,imag;
public:
double getReal(){
return this->real;
}
double getImag(){
return this->imag;
}
Complex(double r=0,double i=0):real(r),imag(i){
}
friend istream & operator >>(istream & is,Complex & c);
};
ostream & operator <<(ostream & os,Complex & c){
os<<c.getReal()<<"+"<<c.getImag()<<"i";
return os;
}
istream & operator >>(istream & is,Complex &c){
double m,n;
is>>m>>n;
c.real=m;
c.imag=n;
return is;
}
int main(void){
int n;
Complex c(3,4);
Complex d;
cin>>d>>n;
cout<<c<<endl;
cout<<d<<endl;
return 0;
}
运算符++ –重载:
#include<iostream>
using namespace std;
class cDemo{
private:
int num;
public:
cDemo(int x=0):num(x){
}
operator int(){
return num;
}
cDemo operator ++(int a){
cDemo tmp(*this);
(this->num)++;
return tmp;
}
cDemo & operator ++(){
++(this->num);
return *this;
}
cDemo operator --(int a){
cDemo tmp(*this);
(this->num)--;
return tmp;
}
cDemo & operator -- (){
--(this->num);
return *this;
}
};
int main(void){
cDemo d(5);
cout<<d++<<",";
cout<<d<<",";
cout<<++d<<",";
cout<<d<<","<<endl;
cout<<d--<<",";
cout<<d<<",";
cout<<--d<<",";
cout<<d<<endl;
return 0;
}
参考MOOC 程序设计与算法三
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
上一篇:P1554 梦中的统计
- C++ 转换函数搭配友元函数 2020-06-10
- C++ 自动转换和强制类型转换(用户自定义类类型) 2020-06-10
- C++ rand函数 2020-06-10
- C++ 友元函数 2020-06-10
- C++ 运算符重载 2020-06-10
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash