C++复制构造函数的实现
2024-08-24 19:06:21
复制构造函数是一种特殊的构造函数,有一般构造函数的特性。它的功能是用一个已知的对象来初始化一个被创建的同类对象。复制构造函数的参数传递方式必须按引用来进行传递,请看实例:
#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
程序结束
程序结束
最新文章
- tomcat7 日志设置为log4j
- 崽崽帮www.zaizaibang.com精选14
- lua 代码风格
- Zookeeper源码编译为Eclipse工程(转)
- [汇编] 002基础知识-CPU和寄存器
- [ZZ]良好的编码习惯
- VM虚拟机上 实现CentOS 6.X下部署LVS(NAT)+keepalived实现高性能高可用负载均衡
- jQuery json数据处理
- centos下安装与配置Apache方法
- 团队作业2:需求分析&;原型设计
- go网络编程应用
- Span<;T>;和ValueTuple<;T>;性能是.Net Core非常关键的特性
- dojo.js --dojo Quick Start/dojo入门手册1
- JavaScript编写风格指南 (一)
- Swift:playground
- Python面向对象之成员修饰符
- 删除排序数组中的重复数字 II
- 【Spring学习笔记-MVC-1.1--】@PathVariable与@RequestParam、@CookieValue等比较
- PHP CURL库学习
- python 查看与更换工作目录
热门文章
- [Python] Send emails to the recepients specified in Message[";CC";]
- SSIS利用Microsoft Connector for Oracle by Attunity组件进行ETL!
- Java分布式:JWT(JSON Web Tokens)
- tomcat 启动报错(tomcat org.apache.catalina.core.StandardContext startInternal)
- Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) A - Neverending competitions
- Java中的静态方法是什么?
- Nginx 自定义404、500错误页面跳转
- ASP.NET MVC Select无限级分类选择下拉框
- Jquery 动态生成的元素绑定事件
- 初入Spring-boot(三)