运算符重载及组合与继承

news2024/10/6 8:25:40

目录

一、运算符重载

1.1普通运算符重载

1.2特殊运算符重载

二、标准输入输出流

三、组合与继承

3.1组合

3.2继承

1) public继承方式

2) protected继承方式

3) private继承方式

小作业:模仿c++的string类,自己实现string类


一、运算符重载

百度百科-验证

1.1普通运算符重载

//C++准许以运算符命名函数!!!
string  a = “hello”;
a += “ world”;// +(a, “world”);

cout<<“hello”;  //  <<(cout, “hello”);

可重载的运算符

 

不可重载的运算符

 

下面用一个例子来展示重载的妙处:

 

#include <stdio.h>
#include <unistd.h>
class Timer{
public:
    Timer()
    {
        hour = 0;
        min = 0;
        sec = 0;
    }
    ~Timer()
    {
        
    }
    void addtimer(int sec = 1)
    {
        this->min += (this->sec+sec)/60;
        this->sec = (this->sec + sec) % 60;
    }

    void show()
    {
        printf("%2d:%2d:%2d\n", hour, min, sec);
    }
private:
    int hour;
    int min;
    int sec;
};

int main()
{
    Timer t;
    while(1)
    {
        t.addtimer(1);
        t.show();
        sleep(1);
    }
}

 

如果我想向下面这样 

#include <stdio.h>
#include <unistd.h>
class Timer{
public:
    Timer()
    {
        hour = 0;
        min = 0;
        sec = 0;
    }
    ~Timer()
    {
        
    }
    void addtimer(int sec = 1)
    {
        this->min += (this->sec+sec)/60;
        this->sec = (this->sec + sec) % 60;
    }

    void show()
    {
        printf("%2d:%2d:%2d\n", hour, min, sec);
    }
private:
    int hour;
    int min;
    int sec;
};

int main()
{
    Timer t;
    t.addtimer(3);

    Timer t1;
    t1.addtimer(5);

    Timer t2;
    t2 = t + t1;
    t2.show();

#if 0
    while(1)
    {
        t.addtimer(1);
        t.show();
        sleep(1);
    }
#endif
}

 

 结果就是报错,但是他提醒我们可以重载

#include <stdio.h>
#include <unistd.h>
class Timer{
public:
    Timer()
    {
        hour = 0;
        min = 0;
        sec = 0;
    }
    ~Timer()
    {
        
    }
    void addtimer(int sec = 1)
    {
        this->min += (this->sec+sec)/60;
        this->sec = (this->sec + sec) % 60;
    }

    void show()
    {
        printf("%2d:%2d:%2d\n", hour, min, sec);
    }
    Timer operator+(Timer &x)
    {
        Timer tem;
        tem.sec = sec + x.sec;
        tem.min = min + x.min;
        tem.hour = hour + x.hour;
        return tem;
    }
private:
    int hour;
    int min;
    int sec;
};

int main()
{
    Timer t;
    t.addtimer(3);

    Timer t1;
    t1.addtimer(5);

    Timer t2;
    t2 = t + t1;
    t2.show();

#if 0
    while(1)
    {
        t.addtimer(1);
        t.show();
        sleep(1);
    }
#endif
}

 

#include <stdio.h>
#include <unistd.h>
class Timer{
public:
    Timer()
    {
        hour = 0;
        min = 0;
        sec = 0;
    }
    ~Timer()
    {
        
    }
    void addtimer(int sec = 1)
    {
        this->min += (this->sec+sec)/60;
        this->sec = (this->sec + sec) % 60;
    }

    void show()
    {
        printf("%2d:%2d:%2d\n", hour, min, sec);
    }
    Timer operator+(Timer &x)
    {
        Timer tem;
        tem.sec = sec + x.sec;
        tem.min = min + x.min;
        tem.hour = hour + x.hour;
        return tem;
    }
private:
    int hour;
    int min;
    int sec;
};

int main()
{
    Timer t;
    t.addtimer(3);

    Timer t1;
    t1.addtimer(5);

    Timer t2;
    t2 = t + t1;
    t2.show();
    t2 = t2 + 5;
    t2.show();

#if 0
    while(1)
    {
        t.addtimer(1);
        t.show();
        sleep(1);
    }
#endif
}

 

 这次又不行了,那就再重载一次。

#include <stdio.h>
#include <unistd.h>
class Timer{
public:
    Timer()
    {
        hour = 0;
        min = 0;
        sec = 0;
    }
    ~Timer()
    {
        
    }
    void addtimer(int sec = 1)
    {
        this->min += (this->sec+sec)/60;
        this->sec = (this->sec + sec) % 60;
    }

    void show()
    {
        printf("%2d:%2d:%2d\n", hour, min, sec);
    }
    Timer operator+(int sec)
    {
        Timer tem;
        tem.sec = this->sec + sec;
        return tem;
    }
    Timer operator+(Timer &x)
    {
        Timer tem;
        tem.sec = sec + x.sec;
        tem.min = min + x.min;
        tem.hour = hour + x.hour;
        return tem;
    }
private:
    int hour;
    int min;
    int sec;
};

int main()
{
    Timer t;
    t.addtimer(3);

    Timer t1;
    t1.addtimer(5);

    Timer t2;
    t2 = t + t1;
    t2.show();
    t2 = t2 + 5;
    t2.show();

#if 0
    while(1)
    {
        t.addtimer(1);
        t.show();
        sleep(1);
    }
#endif
}

 剩下的就都一样了,加减乘除对应换就行了

 

前++和后++不一样 

#include <stdio.h>
#include <unistd.h>
class Timer{
public:
    Timer()
    {
        hour = 0;
        min = 0;
        sec = 0;
    }
    ~Timer()
    {
        
    }
    void addtimer(int sec = 1)
    {
        this->min += (this->sec+sec)/60;
        this->sec = (this->sec + sec) % 60;
    }

    void show()
    {
        printf("%2d:%2d:%2d\n", hour, min, sec);
    }
    Timer operator+(int sec)
    {
        Timer tem;
        tem.sec = this->sec + sec;
        return tem;
    }
    Timer operator+(Timer &x)
    {
        Timer tem;
        tem.sec = sec + x.sec;
        tem.min = min + x.min;
        tem.hour = hour + x.hour;
        return tem;
    }

    Timer operator++(int)
    {
        Timer tem = *this;
        sec++;
        return tem;
    }
    Timer operator++()
    {
        sec++;
        return *this;
    }
private:
    int hour;
    int min;
    int sec;
};

int main()
{
    Timer t;
    t.addtimer(3);

    Timer t1;
    t1.addtimer(5);

    Timer t2;
    t2 = t + t1;
    t2.show();

    t2 = t2 + 5;
    t2.show();

    Timer t3 = t2++;
    t3.show();
    ++t3;
    t3.show();
#if 0
    while(1)
    {
        t.addtimer(1);
        t.show();
        sleep(1);
    }
#endif
}

 

 C++要求我们最好使用前++

因为后++需要先存一次,如果内容太大会很慢。

#include <stdio.h>
#include <unistd.h>
class Timer{
public:
    Timer()
    {
        hour = 0;
        min = 0;
        sec = 0;
    }
    ~Timer()
    {
        
    }
    void addtimer(int sec = 1)
    {
        this->min += (this->sec+sec)/60;
        this->sec = (this->sec + sec) % 60;
    }

    void show()
    {
        printf("%2d:%2d:%2d\n", hour, min, sec);
    }
    Timer operator+(int sec)
    {
        Timer tem;
        tem.sec = this->sec + sec;
        return tem;
    }
    Timer operator+(Timer &x)
    {
        Timer tem;
        tem.sec = sec + x.sec;
        tem.min = min + x.min;
        tem.hour = hour + x.hour;
        return tem;
    }

    Timer operator++(int)
    {
        Timer tem = *this;
        sec++;
        return tem;
    }
    Timer operator++()
    {
        sec++;
        return *this;
    }
    bool operator==(Timer &x)
    {
        if(sec == x.sec && min == x.min && hour == x.hour)
            return true;
        return false;
    }
private:
    int hour;
    int min;
    int sec;
};

int main()
{
    Timer t;
    t.addtimer(3);

    Timer t1;
    t1.addtimer(5);

    Timer t2;
    t2 = t + t1;
    t2.show();

    t2 = t2 + 5;
    t2.show();

    Timer t3 = t2++;
    t3.show();
    ++t3;
    t3.show();

    if(t2 == t3)
        printf("OK..time out!\n");
#if 0
    while(1)
    {
        t.addtimer(1);
        t.show();
        sleep(1);
    }
#endif
}

 

1.2特殊运算符重载

#include <stdio.h>
#include <unistd.h>
class Timer{
public:
    Timer()
    {
        hour = 0;
        min = 0;
        sec = 0;
    }
    ~Timer()
    {
        
    }
    void addtimer(int sec = 1)
    {
        this->min += (this->sec+sec)/60;
        this->sec = (this->sec + sec) % 60;
    }

    void show()
    {
        printf("%2d:%2d:%2d\n", hour, min, sec);
    }
    Timer operator+(int sec)
    {
        Timer tem;
        tem.sec = this->sec + sec;
        return tem;
    }
    Timer operator+(Timer &x)
    {
        Timer tem;
        tem.sec = sec + x.sec;
        tem.min = min + x.min;
        tem.hour = hour + x.hour;
        return tem;
    }

    Timer operator++(int)
    {
        Timer tem = *this;
        sec++;
        return tem;
    }
    Timer operator++()
    {
        sec++;
        return *this;
    }
    bool operator==(Timer &x)
    {
        if(sec == x.sec && min == x.min && hour == x.hour)
            return true;
        return false;
    }
    int operator[](int i)
    {
        switch(i)
        {
            case 0:
                return hour;
            case 1:
                return min;
            case 2:
                return sec;
        }
    }
private:
    int hour;
    int min;
    int sec;
};

int main()
{
    Timer t;
    t.addtimer(3);

    Timer t1;
    t1.addtimer(5);

    Timer t2;
    t2 = t + t1;
    t2.show();

    t2 = t2 + 5;
    t2.show();

    Timer t3 = t2++;
    t3.show();
    ++t3;
    t3.show();

    if(t2 == t3)
        printf("OK..time out!\n");
    printf("hour: %d\n", t2[0]);
    printf("min : %d\n", t2[1]);
    printf("sec : %d\n", t2[2]);

#if 0
    while(1)
    {
        t.addtimer(1);
        t.show();
        sleep(1);
    }
#endif
}

 如果返回的是对象就失去了左值的权利,(可以修改的值放到左边,所以左值就是修改值)

#include <stdio.h>
#include <unistd.h>
class Timer{
public:
    Timer()
    {
        hour = 0;
        min = 0;
        sec = 0;
    }
    ~Timer()
    {
        
    }
    void addtimer(int sec = 1)
    {
        this->min += (this->sec+sec)/60;
        this->sec = (this->sec + sec) % 60;
    }

    void show()
    {
        printf("%2d:%2d:%2d\n", hour, min, sec);
    }
    Timer operator+(int sec)
    {
        Timer tem;
        tem.sec = this->sec + sec;
        return tem;
    }
    Timer operator+(Timer &x)
    {
        Timer tem;
        tem.sec = sec + x.sec;
        tem.min = min + x.min;
        tem.hour = hour + x.hour;
        return tem;
    }

    Timer operator++(int)
    {
        Timer tem = *this;
        sec++;
        return tem;
    }
    Timer operator++()
    {
        sec++;
        return *this;
    }
    bool operator==(Timer &x)
    {
        if(sec == x.sec && min == x.min && hour == x.hour)
            return true;
        return false;
    }
    int &operator[](int i)
    {
        switch(i)
        {
            case 0:
                return hour;
            case 1:
                return min;
            case 2:
                return sec;
        }
    }
private:
    int hour;
    int min;
    int sec;
};

int main()
{
    Timer t;
    t.addtimer(3);

    Timer t1;
    t1.addtimer(5);

    Timer t2;
    t2 = t + t1;
    t2.show();

    t2 = t2 + 5;
    t2.show();

    Timer t3 = t2++;
    t3.show();
    ++t3;
    t3.show();

    if(t2 == t3)
        printf("OK..time out!\n");
    printf("hour: %d\n", t2[0]);
    printf("min : %d\n", t2[1]);
    printf("sec : %d\n", t2[2]);
    t2[1] = 30;
    printf("hour: %d\n", t2[0]);
	printf("min : %d\n", t2[1]);
	printf("sec : %d\n", t2[2]);
#if 0
    while(1)
    {
        t.addtimer(1);
        t.show();
        sleep(1);
    }
#endif
}

 接下来我们看看拷贝中运算符重载的妙用

 

#include <stdio.h>
#include <string.h>

class A{
public:
	A()
	{
		printf("A()\n");
		p = new char[10];
		strcpy(p, "hello");


		printf("p: %s\n", p);
		printf("p: %s\n", this->p);
	}

	A(const A &x)
	{
		printf("A(const A &x)\n");
		p = new char[10];
		strcpy(p, x.p);	
	}

	~A()
	{
		printf("~A()\n");
		delete [] p;
	}
#if 0
	A &     operator=(A &x)
	{
		printf("operator=\n");
		p = new char[10];
		strcpy(p, x.p);	
		return *this;
	}
#endif
private:
	char *p;
};

int main()
{
	A x;

	A y = x;


	y = x;
}

去掉注释部分

先学点快捷方式     3yy复制三行

接下来是()这里我们引出仿函数

#include <iostream>
using namespace std;

int main()
{
//    std::cout<<"hello"<<std::endl;
    cout<<"hello"<<endl;
}

 

 

#include <iostream>

using namespace std;

class Converter{
public:
	Converter(double rate)
	{
		this->rate = rate;
	}

	double operator()(double rmb)
	{
		return rmb*rate;
	}
private:
	double rate;
};


double RMBto(double rmb, double rate)
{
	return rmb*rate;
}

int main()
{
//	std::cout << "hello"<<std::endl;
//	cout << "hello"<<endl;

	Converter RMBtoUS(6.4);
	cout << RMBtoUS(10) << endl;
	cout << RMBtoUS(10) << endl;
	cout << RMBtoUS(10) << endl;
	cout << RMBtoUS(10) << endl;
	cout << RMBtoUS(10) << endl;
	cout << RMBtoUS(10) << endl;
	cout << RMBtoUS(10) << endl;
	cout << RMBtoUS(10) << endl;

	Converter RMBtoE(8.4);
	cout << RMBtoE(100) << endl;
	cout << RMBtoE(100) << endl;
	cout << RMBtoE(100) << endl;
	cout << RMBtoE(100) << endl;
	cout << RMBtoE(100) << endl;
	cout << RMBtoE(100) << endl;
#if 0
	cout << RMBto(10, 6.4) << endl;
	cout << RMBto(10, 6.4) << endl;
	cout << RMBto(10, 6.4) << endl;
	cout << RMBto(10, 6.4) << endl;
	cout << RMBto(10, 6.4) << endl;
	cout << RMBto(10, 6.4) << endl;
	cout << RMBto(10, 6.4) << endl;
	cout << RMBto(10, 6.4) << endl;
	cout << RMBto(10, 6.4) << endl;
	cout << RMBto(10, 6.4) << endl;
	cout << RMBto(10, 6.4) << endl;
	cout << RMBto(10, 6.4) << endl;
	cout << RMBto(10, 6.4) << endl;
	cout << RMBto(10, 8.4) << endl;
	cout << RMBto(10, 8.4) << endl;
	cout << RMBto(10, 8.4) << endl;
	cout << RMBto(10, 8.4) << endl;
	cout << RMBto(10, 8.4) << endl;
	cout << RMBto(10, 8.4) << endl;
	cout << RMBto(10, 8.4) << endl;
	cout << RMBto(10, 8.4) << endl;
	cout << RMBto(10, 8.4) << endl;
	cout << RMBto(10, 8.4) << endl;
#endif
}

 百度百科-验证

接下来是对输出运算符的重载

比如直接输出一个对象

 

#include <stdio.h>
#include <unistd.h>
#include <iostream>

using namespace std;
class Timer{
public:
    Timer()
    {
        hour = 0;
        min = 0;
        sec = 0;
    }
    ~Timer()
    {
        
    }
    void addtimer(int sec = 1)
    {
        this->min += (this->sec+sec)/60;
        this->sec = (this->sec + sec) % 60;
    }

    void show()
    {
        printf("%2d:%2d:%2d\n", hour, min, sec);
    }
    Timer operator+(int sec)
    {
        Timer tem;
        tem.sec = this->sec + sec;
        return tem;
    }
    Timer operator+(Timer &x)
    {
        Timer tem;
        tem.sec = sec + x.sec;
        tem.min = min + x.min;
        tem.hour = hour + x.hour;
        return tem;
    }

    Timer operator++(int)
    {
        Timer tem = *this;
        sec++;
        return tem;
    }
    Timer operator++()
    {
        sec++;
        return *this;
    }
    bool operator==(Timer &x)
    {
        if(sec == x.sec && min == x.min && hour == x.hour)
            return true;
        return false;
    }
    int &operator[](int i)
    {
        switch(i)
        {
            case 0:
                return hour;
            case 1:
                return min;
            case 2:
                return sec;
        }
    }
    friend ostream &operator<<(ostream &out, const Timer &t);
private:
    int hour;
    int min;
    int sec;
};

ostream &operator<<(ostream &out, const Timer &t)
{
    out << "hour:" << t.hour << "min:" <<t.min<<"sec:" <<t.sec<<endl;
}

int main()
{
    Timer t;
    t.addtimer(3);

    Timer t1;
    t1.addtimer(5);

    Timer t2;
    t2 = t + t1;
    t2.show();

    t2 = t2 + 5;
    t2.show();

    Timer t3 = t2++;
    t3.show();
    ++t3;
    t3.show();

    if(t2 == t3)
    printf("OK..time out!\n");
    printf("hour: %d\n", t2[0]);
    printf("min : %d\n", t2[1]);
    printf("sec : %d\n", t2[2]);
    t2[1] = 30;
    printf("hour: %d\n", t2[0]);
	printf("min : %d\n", t2[1]);
	printf("sec : %d\n", t2[2]);

    cout<<t2;
#if 0
    while(1)
    {
        t.addtimer(1);
        t.show();
        sleep(1);
    }
#endif
}

二、标准输入输出流

 

#include <stdio.h>
#include <iostream>

using namespace std;

int main()
{
    //printf("input:");fflush(stdout);
    cout<<"input:";

    char buf[100];
    //gets(buf);
    cin >> buf;

    //printf("%s\n", buf);
    cout << buf <<endl;
}

它会自动识别,不像C那样%d %s %C 什么的

 

#include <stdio.h>
#include <iostream>

using namespace std;

int main()
{
    //printf("input:");fflush(stdout);
    cout<<"input:";

    char buf[100];
    //gets(buf);
    cin >> buf;

    //printf("%s\n", buf);
    cout << buf <<endl;

    cout<<10<<endl;
    cout <<hex<<10<<endl;

}

 

 

控制符

作 用

dec

设置数值的基数为10

hex

设置数值的基数为16

oct

设置数值的基数为8

setfill(c)

设置填充字符c,c可以是字符常量或字符变量

setprecision(n)

设置浮点数的精度为n位。在以一般十进制小数形式输出时,n代表有效数字。在以fixed(固定小数位数)形式和 scientific(指数)形式输出时,n为小数位数

setw(n)

设置字段宽度为n位

setiosflags( ios::fixed)

设置浮点数以固定的小数位数显示

setiosftags( ios::scientific)

设置浮点数以科学记数法(即指数形式)显示

setiosflags( ios::left)

输出数据左对齐

setiosflags( ios::right)

输出数据右对齐

setiosflags( ios::skipws)

忽略前导的空格

setiosflags( ios::uppercase)

数据以十六进制形式输出时字母以大写表示

setiosflags( ios::lowercase)

数据以十六进制形式输出时宇母以小写表示

setiosflags(ios::showpos)

输出正数时给出“+”号


需要注意的是: 如果使用了控制符,在程序单位的开头除了要加iostream头文件外,还要加iomanip头文件。

 

流成员函数

与之作用相同的控制符

作用

precision(n)

setprecision(n)

设置实数的精度为n

width(n)

setw(n)

设置字段宽度为n

fill(c)

setfill(c)

设置填充宇符c

setf()

setiosflags()

设置输出格式状态,括号中应给出格式状态,内容与控制符setiosflags括号中的内容相同,如表13.5所示

unsetf()

resetioflags()

终止已设置的输出格式状态,在括号中应指定内容

 

格式标志

作用

ios::left

输出数据在本域宽范围内向左对齐

ios::right

输出数据在本域宽范围内向右对齐

ios::internal

数值的符号位在域宽内左对齐,数值右对齐,中间由填充字符填充

ios::dec

设置整数的基数为10

ios::oct

设置整数的基数为8

ios::hex

设置整数的基数为16

ios::showbase

强制输出整数的基数(八进制数以0打头,十六进制数以0x打头)

ios::showpoint

强制输出浮点数的小点和尾数0

ios::uppercase

在以科学记数法格式E和以十六进制输出字母时以大写表示

ios::showpos

对正数显示“+”

ios::scientific

浮点数以科学记数法格式输出

ios::fixed

浮点数以定点格式(小数形式)输出

ios::unitbuf

每次输出之后刷新所有的流

ios::stdio

每次输出之后清除stdout, stderr

 

三、组合与继承

 

 

 

3.1组合

一个类的功能都是依据另一个类的对象实现的,就是组合。 

3.2继承

 

#include <iostream>

using namespace std;

class A{
public:
    A(){ }
    ~A(){ }
    void showx()
    {
        cout<<"xxxxxxxxxxxxxxxxxxx"<<endl;
    }
};

class AX:public A{
public:
    void showy()
    {
        cout << "yyyyyyyyyyyyyyyyyyyyyyyyy"<<endl;
    }

};

int main()
{
    A a;
    a.showx();

    AX b;
    b.showx();
    b.showy();
}

然后再回到我们想写的学生管理系统,我们想要再加一个求平均值的功能,那么前面那种组合的方式就不能满足了。

#include "arr.h"
#include <iostream>

using namespace std;

class ARRX:public ARR{
public:
	int ever(void)
	{
		int i = 0;
		int sum = 0;
		for(;i<tail; i++)
			sum += data[i]; 
		return sum/tail;
	}
};

class Stuma{
public:
	Stuma(){

	}
	~Stuma() { }
	
	void savescore(int score)
	{
		scorearr.addtail(score);
	}
	
	int everscore(void)
	{
		return scorearr.ever();
	}

	void showscore(void)
	{
		scorearr.show();
	}

private:
	//ARR scorearr;
	ARRX scorearr;
};

int main()
{
	Stuma mmm;

	mmm.savescore(23);
	mmm.savescore(44);
	mmm.savescore(55);
	mmm.savescore(23);

	mmm.showscore();
	cout << mmm.everscore() <<endl;
}

 

1) public继承方式

  • 基类中所有public成员在派生类中为public属性;
  • 基类中所有protected成员在派生类中为protected属性;
  • 基类中所有private成员在派生类中不可访问。


2) protected继承方式

  • 基类中的所有public成员在派生类中为protected属性;
  • 基类中的所有protected成员在派生类中为protected属性;
  • 基类中的所有private成员在派生类中仍然不可访问。


3) private继承方式

  • 基类中的所有public成员在派生类中均为private属性;
  • 基类中的所有protected成员在派生类中均为private属性;
  • 基类中的所有private成员在派生类中均不可访问。

小作业:模仿c++的string类,自己实现string类

百度百科-验证

        能够准确无误地编写出String类的构造函数、拷贝构造函数、赋值函数和析构函数的面试者至少已经具备了C++基本功的60%以上!在这个类中包括了指针类成员变量m_data,当类中包括指针类成员变量时,一定要重载其拷贝构造函数、赋值函数和析构函数,这既是对C++程序员的基本要求,也是《Effective C++》中特别强调的条款。仔细学习这个类,特别注意加注释的得分点和加分点的意义,这样就具备了60%以上的C++基本功!

我暂时不需要会这么多,就按会60%的标准写这四个,看了两个大佬写的比较全,连接放这,需要的自己去看看:

模拟实现C++中的string类(详细解析)_二肥是只大懒蓝猫的博客-CSDN博客

C++ string类模拟实现_全貌的博客-CSDN博客_模仿c++的string类,自己实现string类

#include <stddef.h>
#include <algorithm>
#include <iostream>
#include <string.h>

//using namespace std;

class String{
public:
    String()
    {
        _str = new char[1];
        _size = 0;
        _capacity = 0;
        _str[0] = '\0';
    }

    String(char* str)
    {
        _size = strlen(str);
        _capacity = _size;
        _str = new char[_capacity + 1];
        strcpy(_str, str);
    }
    ~String()
    {
        delete[] _str;
        _size = 0;
        _capacity = 0;
    }
    void swap(String& tmp)
//这是自己定义的交换函数,第一个参数是隐含的this指针,交换两个类的成员变量
	{
		std::swap(_str, tmp._str);//加::操作符表示调用的是全局的swap函数
		//_str和tmp._str都是指针,交换指向的空间的地址
		std::swap(_size, tmp._size);
		std::swap(_capacity, tmp._capacity);
	}
    
	//拷贝构造的传统写法
	String(String& s)
    {
		_str = new char[s._capacity + 1];
		_capacity = s._capacity;
		_size = s._size;
		strcpy(_str, s._str);
	}

    String& operator=(String& s)
    //使用传值传参,调用拷贝构造创建临时类s接收需要赋值的string类
	{
        if (this != &s)
        {
            String tmp(s);
		    swap(s);//交换它们
        }
		return *this;//返回this指向的类
	}//结束时临时类s销毁,自动调用析构函数
private:
    char* _str;
    size_t _size;           //long long unsigned int
    size_t _capacity;
};
int main()
{
    
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/168596.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【文件随机读写和文件缓冲区】

1.1fseek函数 1.2ftell函数1.3rewind函数2. 文件读取结束的判定2.1文件缓冲区 1.1fseek函数 根据文件指针的位置和偏移量来定位文件指针。 int fseek ( FILE * stream, long int offset, int origin );看不懂没关系&#xff0c;举个例子你就明白了。 我们首先在text.txt文…

送什么礼物给小学生合适?保护视力的专业护眼台灯

在学生们过节以及生日来临&#xff0c;父母们会精心为孩子准备好礼物的&#xff0c;而最有实际意义的&#xff0c;是对学习有所帮助的&#xff0c;比如学习机、护眼灯、绘画本&#xff0c;能丰富孩子的生活都可以&#xff0c;这几年儿童青少年的近视率迅速上升&#xff0c;有52…

TypeScript中的 | 分隔符、 运算符、类型谓词is

一. | 分隔符 在 TypeScript 中联合类型&#xff08;Union Types&#xff09;表示取值可以为多种类型中的一种&#xff0c;联合类型使用 | 分隔每个类型。联合类型通常与 null 或 undefined 一起使用&#xff1a; const sayHello (name: string | undefined) > { /* ... …

WC!咱平时使用的PDF,原来这么不安全?

早前&#xff0c;在2019年3月初&#xff0c;来自明斯特大学及波鸿鲁尔大学的德国研究人员称&#xff0c;他们已经设法利用新发现的漏洞&#xff0c;并成功地攻破了PDF文件中的数字签名。 随后&#xff0c;2019年10月再次披露&#xff1a; 加密PDF存在PDFex漏洞。 最后&#x…

基于wordpress和Sakura主题插件搭建博客网站

基于wordpress和Sakura主题插件搭建博客网站1.引言2.认清现实3.使用的本地化方法4.分享自己走的一些弯路5.硬刚404的余波6.额外的收获1.引言 最近&#xff0c;本着试试的想法&#xff0c;想着找一个前端方面的工作&#xff0c;遇到一些招聘软件或者网站上面有一栏是个人博客网站…

我用递归写单调栈(?)

前言&#xff1a;嗯,这个题上午有的思路&#xff0c;敲了一中午代码&#xff0c;改了一下午最后超时? 题&#xff1a;D. Boris and His Amazing Haircut 题意&#xff1a;一个理发师可以把一段数组给建成一个高度&#xff0c;他现在每个高度的剪子都有若干个。给一个原始数组和…

STL - Set容器

基本概念 构造和赋值 功能描述&#xff1a;创建set容器以及赋值 #include <algorithm> //算法 #include <iostream> #include <set> #include <string> using namespace std;// set/multiset容器void printSet(set<int>& s) {for (set<i…

Java之节点流和处理流(Buffered字节字符处理流)

文章目录前言基本介绍Buffered字符处理流BufferedReader缓冲字符输入流BufferedWriter缓冲字符输出流文件拷贝Buffered字节处理流文件拷贝&#xff08;二进制文件&#xff09;处理流关闭问题前言 Java中的流按照功能可以分为节点流和处理流。其中节点流是直接用来访问数据源&a…

GO的interface的使用和反射

博客主页&#xff1a;&#x1f3c6;看看是李XX还是李歘歘 &#x1f3c6; &#x1f33a;每天不定期分享一些包括但不限于计算机基础、算法、后端开发相关的知识点&#xff0c;以及职场小菜鸡的生活。&#x1f33a; &#x1f497;点关注不迷路&#xff0c;总有一些&#x1f4d6;知…

自动化测试

一、关于自动化什么是自动化?扫地机器人 自动浇水机 自动洗手液 智能马桶... &#xff0c;能够有效的减少人力的消耗&#xff0c;同时提高生活质量。而自动化测试同样&#xff0c;能够有效减少人力的投入&#xff0c;同时提高了测试的质量和效率。回归测试&#xff0c;版本越来…

23.Isaac教程--Isaac导航

Isaac导航 ISAAC教程合集地址: https://blog.csdn.net/kunhe0512/category_12163211.html 节点和消息 Isaac 应用程序由多个节点创建。 导航堆栈具有以下节点&#xff1a; GlobalLocalization&#xff1a;在没有先验信息的情况下&#xff0c;仅使用当前范围扫描测量来估计地…

【JavaGuide面试总结】MySQL篇·上

【JavaGuide面试总结】MySQL篇上1.SQL语句在MySQL中的执行过程MySQL架构Server 层基本组件介绍查询语句分析更新语句分析总结2.MySQL更新语句为什么要用两个日志模块&#xff0c;用一个日志模块不行吗?3.MySQL 支持哪些存储引擎&#xff1f;默认使用哪个&#xff1f;4.MySQL 存…

虹科分享 | TSN时间敏感网络测试框架

一、时间敏感网络 时间敏感网络&#xff08;TSN&#xff09;允许合并OT和IT世界&#xff0c;并保证确定性以太网网络中所有设备的互操作性和标准化。TSN建立在一个真正成熟的生态系统中&#xff08;如以太网&#xff09;&#xff0c;因此大家认为TSN将是下一代工业网络通信的核…

top命令详解

1. 命令参数 d : 监控内容刷新的时间间隔。 n : 限定监控内容刷新的次数&#xff0c;完成后将会退出 top 视图。 p : 只监控指定PID的进程。 -b : 以非交互非全屏模式运行&#xff0c;一般配合-n指定输出几次统计信息&#xff0c;将输出重定向到指定文件&#xff0c;比如 top …

二分查找----C/C++

目录 1. 二分查找的概念 2. 整数的二分 2.1 二分的模版一 2.2 二分的模版二 2.3. 案例剖析 2.4.整数二分总结 3. 浮点数的二分 1. 二分查找的概念 折半查找(BinarySearch)技术&#xff0c;又称为二分查找。它的前提是线性表中的记录 必须是关键码有序(通常从小到大有序)&a…

mysql 分库分表、 分区(partition)、sharding-sphere 综合整理

引言&#xff1a; 一般情况下&#xff0c;如果单表数据量超过2000w的样子查询速度会很慢&#xff0c;因为内存无法存储其索引&#xff0c;使得之后的 SQL 查询会产生磁盘 IO&#xff0c;从而导致性能下降。解决方案&#xff1a;mysql 分区 、 分表处理 分库分表&#xff1a; 原…

【匠心打造】从0打造uniapp 可视化拖拽设计 c_o 第六篇

1、这个版本的变化是左侧增加了布局设计和包资源管理器 包资源管理器&#xff1a;eclipse的特称&#xff0c;左侧的项目管理。和hbuildx左侧类似 项目的整体设计结构如下: v1.0 普通模式&#xff1a;支持新建前端项目&#xff0c;拖拽&#xff0c;且生成前端项目&#xff08…

基于“遥感+”蓝碳储量估算、红树林信息提取实践技术应用与科研论文写作

目录 “遥感”助推蓝碳生态系统碳储量调查简介 第一章 高光谱遥感数据介绍及预处理 第二章 光谱特征分析与参量提取 第三章 高光谱遥感数据分类与制图 第四章 GEE数据处理介绍 第五章 碳储量时空变化与预测 大气温室气体浓度不断增加&#xff0c;导致气候变暖加剧&#x…

DFS的树上应用

目录 一、前言 二、树上的DFS 1、树的重心 2、树的重心例题 3、树的直径 4、树的直径例题 &#xff08;1&#xff09;做两次DFS 三、拓扑排序与DFS 1、用DFS解拓扑排序 2、欧拉路与DFS 3、用DFS输出一个欧拉回路 一、前言 本文主要讲了树上的DFS、树的重心、树的直…

538. 把二叉搜索树转换为累加树

538. 把二叉搜索树转换为累加树 难度中等 给出二叉 搜索 树的根节点&#xff0c;该树的节点值各不相同&#xff0c;请你将其转换为累加树&#xff08;Greater Sum Tree&#xff09;&#xff0c;使每个节点 node 的新值等于原树中大于或等于 node.val 的值之和。 提醒一下&am…