4.【拷贝构造函数与重载】

简介: 4.【拷贝构造函数与重载】

 

【没有明确提供拷贝构造函数的时候回调用默认的拷贝构造函数】

#include <iostream>
#include <string.h>
using namespace std;
class Student
{
private:
    int number;
    string name;
public:
    Student(int nu, string na) :number(nu), name(na) {}
    Student() {}
    void show()
    {
        cout << "学生的学号是:" << number << "学生的姓名是:" << name << endl;
    }
};
int main()
{
    Student s(21032114, "李威涛");
    Student s1 = s;                   //   利用初始化的方法
    s1.show();
    cout << "***********************" << endl;
    Student s2(s);                      //    利用小括号的方法赋值
    s2.show();
    return 0;
}

=============

【明确提供拷贝构造函数时】

#include <iostream>
#include <string.h>
using namespace std;
class Student
{
private:
 int number;
 string name;
public:
 Student(int nu, string na) :number(nu), name(na) {}
 Student() {}
 Student(const Student& s) //拷贝构造函数要用用引用 ,因为引用值传递形参改变实参也改变
 {
  number = s.number;
  name = s.name;
 }
 void show()
 {
  cout << "学生的学号是:" << number << "学生的姓名是:" << name << endl;
 }
};
int main()
{
 Student s(21032114, "李威涛");
 Student s1 = s; // 利用初始化的方法
 s1.show();
 cout << "***********************" << endl;          
 Student s2(s); // 利用小括号的方法赋值
 s2.show();
 return 0;
}

=============

【拷贝构造函数与重载运算符函数】

(为什么要返回对象名,实际上是在用拷贝构造函数)

#include <iostream>
using namespace std;
class Complex
{
private:
    double real;
    double imag;
public:
    Complex(double r, double i) :real(r), imag(i) { cout << "调用了构造函数" << endl; }
    Complex() {}
    Complex(const Complex& c)
    {
        real = c.real;
        imag = c.imag;
        cout << "*************调用了拷贝构造函数:************" << endl;
    }
    Complex operator+(Complex& c)
    {
        cout << "*************调用了重载运算符**********" << endl;
        Complex c1;
        c1.real = real + c.real;     //    返回对象的实部=原实部+引用实部;
        c1.imag = imag + c.imag;
        return c1;                    //返回对象名,其实是想用拷贝函数
    }
    void show()
    {
        cout << "加到一起是:" << real << "+" << imag << "i" << endl;
    }
};
int main()
{
    Complex c(2, 3), c1(c);
    c1.show();
    Complex c2(3, 4), c3(2, 5),c4;
    c4 = c2.operator+(c3);          //  实际上为: c4=c1(real,imag);
    c4.show();
}

=============

【在重载运算符的时候,如果不调用新的对象,要返回*this】

#include <iostream>
using namespace std;
class Complex
{
private:
 double real;
 double imag;
public:
 Complex(double r, double i) :real(r), imag(i) { cout << "调用了构造函数" << endl; }
 Complex() {}
 Complex(const Complex& c)
 {
  real = c.real;
  imag = c.imag;
  cout << "*************调用了拷贝构造函数:************" << endl;
 }
 Complex operator=(Complex& c)
 {
  cout << "*************调用了重载运算符**********" << endl;
   real = c.real; // 返回对象的实部=原实部+引用实部;
    imag = c.imag;
    return *this; //临时变量后面会消失
 }
 void show()
 {
  cout << "加到一起是:" << real << "+" << imag << "i" << endl;
 }
};
int main()
{
 Complex c(2, 3), c1(c);
 c1.show();
 Complex c2(3, 4), c3(2, 5),c4;
  c2.operator=(c3); //  
  c2.show();
}

=============

【注意事项】


相关文章
|
9月前
|
存储 编译器 C语言
【C++基础】类与对象(中):默认成员函数、构造函数、析构函数、拷贝构造、赋值重载函数……
【C++基础】类与对象(中):默认成员函数、构造函数、析构函数、拷贝构造、赋值重载函数……
68 0
|
18天前
|
C++
C++拷贝构造函数和运算符重载--2
C++拷贝构造函数和运算符重载--2
|
18天前
|
编译器 C++
C++拷贝构造函数和运算符重载--4
C++拷贝构造函数和运算符重载--4
|
18天前
|
编译器 C++
C++拷贝构造函数和运算符重载--1
C++拷贝构造函数和运算符重载--1
|
18天前
|
C++
C++拷贝构造函数和运算符重载--3
C++拷贝构造函数和运算符重载--3
|
18天前
|
C++
C++拷贝构造函数和运算符重载--5
C++拷贝构造函数和运算符重载--5
|
18天前
拷贝构造函数与移动构造函数
拷贝构造函数与移动构造函数
30 0
|
8月前
|
编译器 C++
C++ 重载函数
C++ 重载函数
35 0
|
11月前
|
存储 编译器 C++
拷贝构造函数和运算符重载(上)
拷贝构造函数和运算符重载(上)
|
11月前
|
编译器
拷贝构造函数和运算符重载(下)
拷贝构造函数和运算符重载(下)

热门文章

最新文章