区别无参、带参、拷贝构造函数、赋值函数
来源:互联网 发布:nginx 防止ddos攻击 编辑:程序博客网 时间:2024/06/01 10:40
#include <iostream>
#include <string>
using namespace std;
struct Student//在结构体中
{
string name;
int age;
Student() //无参构造函数
{
cout << "No-args Constructor" << endl;
age = -1;
name = "Unknown";
}
Student(string name, int age)//带参构造函数
{
cout << "Argumenting Constructor" << endl;
this->name = name;
this->age = age;
}
Student(const Student&other)//拷贝构造函数
{
cout << "Copy Constructor" << endl;
this->name = other.name;
this->age = other.age;
}
Student& operator=(const Student&other)//赋值函数
{
cout << "Assigning" << endl;
this->name = other.name;
this->age = other.age;
}
~Student()
{
cout << "Destructor" << endl;
}
friend ostream& operator<<(ostream & out, const Student&student)
{
cout << "Student[" << student.name << "]:" << "age=" << student.age;
return out;
}
};
int main()
{
Student student01;//调用了无参构造函数
cout << student01 << endl;
system("pause");
Student student02;//构造函数是被实例化过程中调用的,无论重载多少个构造函数,实际的只有一个
cout << student02 << endl;
system("pause");
Student student03 = Student();//不要误以为:等号右边调用了构造函数,然后赋值给左边又调用一次
cout << student03 << endl;//拷贝构造函数。其实只调用了构造函数,和Student student03;的效果一样
system("pause");
Student student04("LiMing", 1);//带参构造函数
cout << student04 << endl;
system("pause");
Student student05 = Student("LiMing", 1);//如上所说也是只调用一次带参构造函数
cout << student05 << endl;
system("pause");
Student student06 = student04;//拷贝构造函数,声明和赋值在同一行
cout << student06 << endl;
system("pause");
Student student07;//赋值函数,声明和赋值分开为两行
student07 = student04;
cout << student07 << endl;
system("pause");
return 0;
}
#include <string>
using namespace std;
struct Student//在结构体中
{
string name;
int age;
Student() //无参构造函数
{
cout << "No-args Constructor" << endl;
age = -1;
name = "Unknown";
}
Student(string name, int age)//带参构造函数
{
cout << "Argumenting Constructor" << endl;
this->name = name;
this->age = age;
}
Student(const Student&other)//拷贝构造函数
{
cout << "Copy Constructor" << endl;
this->name = other.name;
this->age = other.age;
}
Student& operator=(const Student&other)//赋值函数
{
cout << "Assigning" << endl;
this->name = other.name;
this->age = other.age;
}
~Student()
{
cout << "Destructor" << endl;
}
friend ostream& operator<<(ostream & out, const Student&student)
{
cout << "Student[" << student.name << "]:" << "age=" << student.age;
return out;
}
};
int main()
{
Student student01;//调用了无参构造函数
cout << student01 << endl;
system("pause");
Student student02;//构造函数是被实例化过程中调用的,无论重载多少个构造函数,实际的只有一个
cout << student02 << endl;
system("pause");
Student student03 = Student();//不要误以为:等号右边调用了构造函数,然后赋值给左边又调用一次
cout << student03 << endl;//拷贝构造函数。其实只调用了构造函数,和Student student03;的效果一样
system("pause");
Student student04("LiMing", 1);//带参构造函数
cout << student04 << endl;
system("pause");
Student student05 = Student("LiMing", 1);//如上所说也是只调用一次带参构造函数
cout << student05 << endl;
system("pause");
Student student06 = student04;//拷贝构造函数,声明和赋值在同一行
cout << student06 << endl;
system("pause");
Student student07;//赋值函数,声明和赋值分开为两行
student07 = student04;
cout << student07 << endl;
system("pause");
return 0;
}
阅读全文
0 0
- 区别无参、带参、拷贝构造函数、赋值函数
- 拷贝构造函数和无参构造函数,深浅拷贝
- 区别拷贝构造函数和赋值函数
- 拷贝,赋值构造函数赋值
- 拷贝构造函数与赋值构造函数的区别
- 拷贝构造函数与赋值构造函数的区别
- 拷贝构造函数和赋值构造函数的区别
- 拷贝构造函数和赋值构造函数的区别
- 赋值构造函数和拷贝构造函数的区别
- 赋值构造函数和拷贝构造函数的区别
- 拷贝构造函数和赋值构造函数的区别
- 赋值函数、拷贝构造函数
- 拷贝构造函数赋值函数
- 拷贝构造函数,赋值函数
- 构造函数 拷贝函数 赋值构造函数
- 构造函数 拷贝函数 赋值构造函数
- 构造函数、拷贝构造函数、赋值函数
- 拷贝构造函数和赋值函数的区别
- 第八周项目4-广义表算法库及应用(1)
- 异常行为检测论文学习综述
- H5大番薯牛牛源码下载
- 卷积神经网络实战(可视化部分)——使用keras识别猫咪
- 视频分辨率全表(101种)
- 区别无参、带参、拷贝构造函数、赋值函数
- IT运维软件的发展趋势
- win10下命令提示符窗口的路径切换与安装opencv
- 报错信息解决情况汇总
- 听说你把Word用成了记事本?难怪你月薪不过万!
- SQL语法基础
- 阿里巴巴Java开发手册MyEclipse插件使用指南
- C语言提高-第20讲: 经典:查找的艺术(有几个)
- 餐饮界有福了,为海底捞服务的SaaS产品开始推向全行业