复制构造函数是一种特殊的构造函数,有一般构造函数的特性。它的功能是用一个已知的对象来初始化一个被创建的同类对象。复制构造函数的参数传递方式必须按引用来进行传递,请看实例:

#include <iostream>
#include <cstring>
using namespace std ;
class Student
{
	private :
		char name[8];
		int age ;
		char sex ;
		int score ;
	public :
		void disp(); //打印信息的函数声明
		Student(char name[],int age , char sex ,int score); //构造函数声明
		Student(Student &dx);	//复制构造函数的声明
		~Student(); //析构函数的声明
};
//打印信息函数的实现
void Student::disp()
{
	cout << this->name << endl ;
	cout << this->age << endl ;
	cout << this->sex << endl ;
	cout << this->score << endl ;
}
//构造函数的实现
Student::Student(char name[],int age , char sex ,int score)
{
	strcpy(this->name,name);
	this->age = age ;
	this->sex = sex ;
	this->score = score ;
}
//复制构造函数的实现
Student::Student(Student &dx)
{
	strcpy(this->name , dx.name);
	this->age = dx.age ;
	this->sex = dx.sex ;
	this->score = dx.score ;
}
//析构函数的实现
Student::~Student()
{
	cout << "程序结束" << endl ;
} 

int main(void)
{
	Student stu1("YYX",23,'N',86);
	Student stu2(stu1);
	stu1.disp() ;
	stu2.disp() ;
	return 0 ;
}

运行结果:

YYX

23

N

86

YYX

23

N

86

程序结束

程序结束

最新文章

  1. tomcat7 日志设置为log4j
  2. 崽崽帮www.zaizaibang.com精选14
  3. lua 代码风格
  4. Zookeeper源码编译为Eclipse工程(转)
  5. [汇编] 002基础知识-CPU和寄存器
  6. [ZZ]良好的编码习惯
  7. VM虚拟机上 实现CentOS 6.X下部署LVS(NAT)+keepalived实现高性能高可用负载均衡
  8. jQuery json数据处理
  9. centos下安装与配置Apache方法
  10. 团队作业2:需求分析&amp;原型设计
  11. go网络编程应用
  12. Span&lt;T&gt;和ValueTuple&lt;T&gt;性能是.Net Core非常关键的特性
  13. dojo.js --dojo Quick Start/dojo入门手册1
  14. JavaScript编写风格指南 (一)
  15. Swift:playground
  16. Python面向对象之成员修饰符
  17. 删除排序数组中的重复数字 II
  18. 【Spring学习笔记-MVC-1.1--】@PathVariable与@RequestParam、@CookieValue等比较
  19. PHP CURL库学习
  20. python 查看与更换工作目录

热门文章

  1. [Python] Send emails to the recepients specified in Message[&quot;CC&quot;]
  2. SSIS利用Microsoft Connector for Oracle by Attunity组件进行ETL!
  3. Java分布式:JWT(JSON Web Tokens)
  4. tomcat 启动报错(tomcat org.apache.catalina.core.StandardContext startInternal)
  5. Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) A - Neverending competitions
  6. Java中的静态方法是什么?
  7. Nginx 自定义404、500错误页面跳转
  8. ASP.NET MVC Select无限级分类选择下拉框
  9. Jquery 动态生成的元素绑定事件
  10. 初入Spring-boot(三)