C++ 中的 '+' 运算符重载
'+' Operator overloading in C++
我刚开始学习运算符重载,并且只是在研究代码以了解其工作原理。所以,我写了一个添加两个字符的代码。例如:'#' + '%' = 'H' 因为 ASCII 值增加。
这是我的代码:
#include <iostream>
using namespace std;
class Strings {
//everything is public
public:
char str;
//empty constructor
Strings();
Strings(char);
//operator takes in an object of Strings
Strings operator+(Strings);
};
Strings::Strings()
{}
//constructor
Strings::Strings(char a) {
str = a;
}
//aso is "another string object"
//makes new empty object "brandNew"
//brandNew is the two characters added together
//returns object brandNew
Strings Strings::operator+(Strings aso) {
Strings brandNew;
brandNew.str = str + aso.str;
return brandNew;
}
int main() {
Strings a('#');
Strings b('%');
Strings c;
//now, we can use + operator to add characters
c = a + b;
cout << c.str << endl;
return 0;
}
如果我想添加两个字符串怎么办?如果我输入
Strings a("###");
Strings b("%%%");
我希望输出为
HHH
我将如何更改我的代码以添加这两个字符串?我开始将所有 char 类型声明更改为 string。我想我必须在运算符函数内创建一个 for 循环,以便在添加它们时迭代两个输入的每个字符。但是,我对它的语法感到困惑,也对如何实现它感到困惑。任何帮助和解释将不胜感激!
我会给你一些帮助来声明 class。
class Strings {
private:
char* str;
unsigned int length;
unsigned int size;
public:
//constructor
Strings();
~Strings();
Strings(const char*);
Strings(const Strings&);
//operator
Strings operator+(const Strings&);
Strings operator+(const char*);
Strings operator=(const Strings&);
Strings operator=(const char*);
Strings operator+=(const Strings&);
Strings operator+=(const char*);
///Accessors
const char* GetStr()const;
unsigned int GetLength()const;
unsigned int GetSize()const;
};
我刚开始学习运算符重载,并且只是在研究代码以了解其工作原理。所以,我写了一个添加两个字符的代码。例如:'#' + '%' = 'H' 因为 ASCII 值增加。
这是我的代码:
#include <iostream>
using namespace std;
class Strings {
//everything is public
public:
char str;
//empty constructor
Strings();
Strings(char);
//operator takes in an object of Strings
Strings operator+(Strings);
};
Strings::Strings()
{}
//constructor
Strings::Strings(char a) {
str = a;
}
//aso is "another string object"
//makes new empty object "brandNew"
//brandNew is the two characters added together
//returns object brandNew
Strings Strings::operator+(Strings aso) {
Strings brandNew;
brandNew.str = str + aso.str;
return brandNew;
}
int main() {
Strings a('#');
Strings b('%');
Strings c;
//now, we can use + operator to add characters
c = a + b;
cout << c.str << endl;
return 0;
}
如果我想添加两个字符串怎么办?如果我输入
Strings a("###");
Strings b("%%%");
我希望输出为
HHH
我将如何更改我的代码以添加这两个字符串?我开始将所有 char 类型声明更改为 string。我想我必须在运算符函数内创建一个 for 循环,以便在添加它们时迭代两个输入的每个字符。但是,我对它的语法感到困惑,也对如何实现它感到困惑。任何帮助和解释将不胜感激!
我会给你一些帮助来声明 class。
class Strings {
private:
char* str;
unsigned int length;
unsigned int size;
public:
//constructor
Strings();
~Strings();
Strings(const char*);
Strings(const Strings&);
//operator
Strings operator+(const Strings&);
Strings operator+(const char*);
Strings operator=(const Strings&);
Strings operator=(const char*);
Strings operator+=(const Strings&);
Strings operator+=(const char*);
///Accessors
const char* GetStr()const;
unsigned int GetLength()const;
unsigned int GetSize()const;
};