【C++Primer】封装_拷贝构造函数
2017-01-30 16:48
155 查看
噫,大概有些了解构造函数了,这大概是一下午反复折腾收到的最好回报了。
#include <stdio.h>
#include <iostream>
#include <string>
#include "Teacher.h"
using namespace std;
class Teacher
{
public:
Teacher(string name="Ludwing",int age=26);
Teacher(const Teacher &tea);//声明拷贝构造函数;
void setName(string name);
string getName();
void setAge(int age);
int getAge();
private:
string m_strName;
int m_iAge;
};
Teacher::Teacher(string name,int age):m_strName(name),m_iAge(age)
{
cout<<"Teacher(string name,int age)"<<endl;
}
Teacher::Teacher(const Teacher &tea)
{
cout<<"Teacher(const Teacher &tea)"<<endl;
}
void Teacher::setName(string name)
{
m_strName=name;
}
string Teacher::getName()
{
return m_strName;
}
void Teacher::setAge(int age)
{
m_iAge=age;
}
int Teacher::getAge()
{
return m_iAge;
}
void test(Teacher t)
{
}
int main()
{
Teacher t1;//调用t1时使用的是正常的构造函数;
test(t1);//调用test时触发了拷贝过程,所以用到了拷贝构造函数;
Teacher t2(t1);//调用t2,t3时使用的是拷贝构造函数;
Teacher t3=t1;
return 0;
}
#include <stdio.h>
#include <iostream>
#include <string>
#include "Teacher.h"
using namespace std;
class Teacher
{
public:
Teacher(string name="Ludwing",int age=26);
Teacher(const Teacher &tea);//声明拷贝构造函数;
void setName(string name);
string getName();
void setAge(int age);
int getAge();
private:
string m_strName;
int m_iAge;
};
Teacher::Teacher(string name,int age):m_strName(name),m_iAge(age)
{
cout<<"Teacher(string name,int age)"<<endl;
}
Teacher::Teacher(const Teacher &tea)
{
cout<<"Teacher(const Teacher &tea)"<<endl;
}
void Teacher::setName(string name)
{
m_strName=name;
}
string Teacher::getName()
{
return m_strName;
}
void Teacher::setAge(int age)
{
m_iAge=age;
}
int Teacher::getAge()
{
return m_iAge;
}
void test(Teacher t)
{
}
int main()
{
Teacher t1;//调用t1时使用的是正常的构造函数;
test(t1);//调用test时触发了拷贝过程,所以用到了拷贝构造函数;
Teacher t2(t1);//调用t2,t3时使用的是拷贝构造函数;
Teacher t3=t1;
return 0;
}
相关文章推荐
- c++远征之封装篇(拷贝构造函数)
- 一个mysql pdo的封装类
- Ajax封装
- 黄聪:C#图片处理封装类(裁剪、缩放、清晰度、加水印、生成缩略图)有示例(转)
- 《C++Primer 5e》学习笔记(5):函数
- asp.net mvc自定义pager封装与优化
- 跨浏览器事件对象封装
- Glide V4的封装使用
- C++Primer 第一章 快速入门 学习
- action中实现对批量文件上传的封装
- 封装C++实现Window和Linux下多线程Socket通信
- 轻松把玩HttpClient之封装HttpClient工具类(五),携带Cookie的请求
- 理解java的三大特性之封装
- 使用mp4v2将aac音频h264视频数据封装成mp4开发心得
- 策略模式:封装变化与面向接口编程
- 读《C++Primer》第二天——变量和基本类型
- 封装两个简单的Jquery组件
- Go语言标准库堆(heap)封装及堆排序实现
- jQuery Ajax通用js封装
- C#调用C++封装的DLL调试方法小结