C++学习笔记3——封装篇(上)

教程:https://www.imooc.com/learn/382

拷贝构造函数

举例:
Teacher.h

#include <iostream>
#include <string>
using namespace std;

class Teacher
{
	public:
		Teacher(string name = "zhang" , int age = 10 ); //默认构造函数 
		Teacher(const Teacher &tea); //拷贝构造函数
		void setName(string _name);
		string getName();
		void setAge(int _age);
		int getAge();

	private:
		string strName;
		int iAge;
};

Teacher.cpp

#include <iostream>
#include <string>
#include "Teacher.h"
using namespace std;

Teacher::Teacher(string _name , int _age):strName(_name),iAge(_age)
{
	//iMax = m; //iMax是const修饰的常量,如果在这里初始化,会报错
	cout << "Teacher(string _name , int _age)" << endl; //体现函数被调用
}

Teacher::Teacher(const Teacher &tea) //拷贝构造函数
{
	cout << "Teacher(const Teacher &tea)" << endl;
}

void Teacher::setName(string _name)
{
	strName = _name;
}
string Teacher::getName()
{
	return strName;
}
void Teacher::setAge(int _age)
{
	iAge = _age;
}
int Teacher::getAge()
{
	return iAge;
}

demo.cpp

#include <iostream>
#include <string>
#include "Teacher.h"
using namespace std;

void test(Teacher t) //参数传递的例子
{

}

int main()
{
	Teacher t1;  //有默认构造函数,如此语句这样定义,则输出的都是默认值
	Teacher t2 = t1; //用t1赋值的方式实例化t2
	Teacher t3(t1); //用t1赋值的方式实例化t3
	test(t1);
	return 0;
}

t2 、t3 、test调用的都是拷贝构造函数

“拷贝构造函数可以重载”(X)
函数重载要求形参有不同的地方,比如个数,比如类型
拷贝构造函数的形参是固定的,所以不能重载

当没有自定义的拷贝构造函数时,系统自动生成一个拷贝构造函数

通过同类型的对象实例化另外的对象时,自动调用拷贝构造函数

拷贝构造函数没有返回值

析构函数

销毁对象,归还系统资源

定义格式: ~类名()

在这里插入图片描述

如果没有自定义的析构函数,则系统自动生成

析构函数在对象销毁时自动调用

析构函数没有返回值、没有参数也不能重载
在这里插入图片描述

析构函数使用举例:
Teacher.h:

#include <iostream>
#include <string>
using namespace std;

class Teacher
{
	public:
		Teacher(string name = "zhang" , int age = 10 ); //默认构造函数 
		Teacher(const Teacher &tea); //拷贝构造函数
		~Teacher(); //声明析构函数,注意没有参数
		void setName(string _name);
		string getName();
		void setAge(int _age);
		int getAge();

	private:
		string strName;
		int iAge;
};

Teacher.cpp:

#include <iostream>
#include <string>
#include "Teacher.h"
using namespace std;

Teacher::Teacher(string _name , int _age):strName(_name),iAge(_age)
{
	//iMax = m; //iMax是const修饰的常量,如果在这里初始化,会报错
	cout << "Teacher(string _name , int _age)" << endl; //体现函数被调用
}

Teacher::Teacher(const Teacher &tea) //拷贝构造函数
{
	cout << "Teacher(const Teacher &tea)" << endl;
}

Teacher::~Teacher()
{
	cout << "~Teacher" << endl; //证明析构函数被调用
}


void Teacher::setName(string _name)
{
	strName = _name;
}
string Teacher::getName()
{
	return strName;
}
void Teacher::setAge(int _age)
{
	iAge = _age;
}
int Teacher::getAge()
{
	return iAge;
}

demo.cpp:

#include <iostream>
#include <string>
#include "Teacher.h"
using namespace std;

void test(Teacher t) //参数传递的例子
{

}

int main()
{
	Teacher t1; //从栈上实例化对象
	Teacher *p = new Teacher(); //从堆中实例化对象
	delete p; //销毁对象
	Teacher t2(t1); //拷贝构造函数
	return 0;
}

综合练习

定义一个Student类,包含名字一个数据成员,定义无参构造函数、有参构造函数、拷贝构造函数、析构函数及对于名字的封装函数,在main函数中实例化Student对象,并访问相关函数,观察运行结果。

#include <iostream>
#include <string>
using namespace std;
/**
 * 定义类:Student
 * 数据成员:m_strName
 * 无参构造函数:Student()
 * 有参构造函数:Student(string _name)
 * 拷贝构造函数:Student(const Student& stu)
 * 析构函数:~Student()
 * 数据成员函数:setName(string _name)、getName()
 */
class Student
{
    public:
        Student();
        Student(string _name);
        Student(const Student& stu);
        ~Student();
        void setName(string _name);
        string getName();
    private:
        string m_strName;
};

Student::Student()
{
    
}

Student::Student(string _name)
{
    
}

Student::Student(const Student& stu)
{
    
}

Student::~Student()
{
    
}



void Student::setName(string _name)
{
    m_strName = _name;
}

string Student::getName()
{
    return m_strName;
}

int main(void)
{
    // 通过new方式实例化对象*stu
    Student *stu = new Student();
    // 更改对象的数据成员为“慕课网”
	stu->setName("慕课网");
    // 打印对象的数据成员
	cout << stu->getName() << endl;
	delete stu;
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值