project 1 重载()运算符
#include <iostream>
using namespace std;
class Clock {
private:
int hour, minute, second;
public:
Clock(int h, int m, int s) : hour(h), minute(m), second(s) {}
// 重载()运算符
void operator()(int h, int m, int s) {
hour = h;
minute = m;
second = s;
}
void ShowTime() {
cout << hour << ":" << minute << ":" << second << endl;
}
};
int main(int argc, char const *argv[])
{
Clock t1(12, 34, 45);
t1.ShowTime();
t1.operator()(10, 21, 32);
t1.ShowTime();
return 0;
}
project 2 重载()和[]运算符
#include<iostream>
using namespace std;
class X
{
public:
int operator() (int i = 0) {
cout << "X operator (" << i << ")" << endl;
return i;
}
int operator[] (int j) {
cout << "X operator [" << j << "]" << endl;
return j;
}
};
int main (void)
{
X obj;
int i = obj(1);
cout << i << endl;
int j = obj[234];
cout << j << endl;
return 0;
}
X operator (1)
1
X operator [234]
234
sublime
的代码复制到VScode
之后会出现空格不一致的问题,解决方法是在sublime
中把tab
替换为空格Perference->setting
添加
"tab_size": 4,
"translate_tabs_to_spaces": true
project 3 员工工资系统
struct person {
double salary;
char* name;
};
class SalaryManage {
private:
person* employee;
int max; // 可容纳员工数
int count; // 当前员工数
public:
SalaryManage(int Max = 0) {
max = Max;
count = 0;
employee = new person[max];
}
double &operator [] (const char* Name) {
person* ptr;
for (ptr = employee; ptr <= employee + count; ptr++) {
if (strcmp(ptr->name, Name) == 0) {
return ptr->salary;
}
ptr = employee + count; // 跳到最后一个
count++; // 员工数++
ptr->name = new char[strlen(Name) + 1]; // 新建一个员工
strcpy(ptr->name, Name);
ptr->salary = 0;
return ptr->salary;
}
}
void display() {
for (int i = 0; i < count; i++) {
cout << employee[i].name << ": " << employee[i].salary << endl;
}
}
};
int main(int argc, char const *argv[])
{
SalaryManage s(2);
const char* ch1 = "kavin";
s[ch1] = 3.2;
const char* ch2 = "jack";
s[ch2] = 123.2;
cout << s[ch1] << endl;
s.display();
return 0;
}
3.2
kavin: 3.2
jack: 123.2
C++11
不能直接从string
字符串转为char*
,所以用一个指向字符串常量的指针指向他
project 4 重载二元运算符
#include <iostream>
using namespace std;
class Complex
{
public:
Complex(int _r = 0, int _i = 0) : r(_r), i(_i) {}
Complex operator + (Complex input);
Complex operator - (Complex input);
void info();
private:
int r, i;
};
Complex Complex::operator + (Complex input) {
return Complex(this->r + input.r, this->i + input.i);
}
Complex Complex::operator - (Complex input) {
return Complex(this->r - input.r, this->i - input.i);
}
void Complex::info() {
cout << this->r;
if (this->i > 0) cout << "+";
if (this->i != 0) cout << this->i << "i" << endl;
}
int main(int argc, char const *argv[])
{
Complex a(2, -3);
a.info();
Complex b(3, 4);
b.info();
Complex c = a + b;
c.info();
Complex d = a - b;
d.info();
return 0;
}
2-3i
3+4i
5+1i
-1-7i
project5 自定义String类
#include <iostream>
#include <cstring>
using namespace std;
class String
{
public:
String(const char* = "");
~String(){}
friend ostream& operator << (ostream& os, const String& s) {
return os << s.sPtr;
}
// 声明为友元函数,参数要与实际需要的参数相同
friend String operator + (const String& a, const String& b);
const String& operator = (const String&);
const String& operator += (const String&);
bool operator == (const String&);
bool operator != (const String&);
bool operator < (const String&);
char& operator [] (int index);
int getLength() { return this->length; }
private:
char* sPtr;
int length;
};
String::String(const char* str) {
this->sPtr = new char[strlen(str) + 1];
strcpy(this->sPtr, str);
this->length = strlen(str);
}
// 友元就不需要作用域String::了
String operator + (const String& a, const String& b) {
String res;
res.sPtr = new char[a.length + b.length + 1];
strcpy(res.sPtr, a.sPtr);
strcat(res.sPtr, b.sPtr);
return String(res.sPtr);
}
const String& String::operator = (const String& s) {
cout << "copy assignment" << endl;
this->length = s.length;
strcpy(this->sPtr, s.sPtr);
return *this;
}
const String& String::operator += (const String& s) {
char* temp = this->sPtr;
this->length += s.length;
this->sPtr = new char[this->length + 1];
strcpy(this->sPtr, temp);
strcat(this->sPtr, s.sPtr);
delete[] temp;
return *this;
}
bool String::operator == (const String& s) {
return strcmp(this->sPtr, s.sPtr) == 0;
}
bool String::operator != (const String& s) {
return !(*this == s);
}
bool String::operator < (const String& s) {
return strcmp(this->sPtr, s.sPtr) < 0;
}
char& String::operator [] (int index) {
return this->sPtr[index];
}
int main(int argc, char const *argv[])
{
String s1("hello");
cout << "s1=" << s1 << endl;
String s2;
s2.operator=(s1);
cout << "s2=" << s2 << endl;
s2 += " world";
cout << "s2=" << s2 << endl;
for (int i = 0; i < s2.getLength(); i++) {
cout << s2[i] << " ";
}
cout << endl;
String s3 = s1 + s2;
cout << "s3=" << s3 << endl;
String s4("hello world");
if (s2 != s4) {
cout << "s2!=s4" << endl;
} else {
cout << "s2==s4" << endl;
}
String str1("abd");
String str2("abc");
if (str1 < str2) {
cout << "str1 < str2" << endl;
} else {
cout << "str1 >= str2" << endl;
}
return 0;
}
s1=hello
copy assignment
s2=hello
s2=hello world
h e l l o w o r l d
s3=hellohello world
s2==s4
str1 >= str2