C++基础一

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/daividtu/article/details/78296989

main.cpp

#include <Teacher.h>
#include <iostream>

using namespace std;

void fun() {
	MyTeacher teacher;

	teacher.setAge(18);
	teacher.setName("小明");

	cout << "teacher name : " << teacher.getName() << endl;
	cout << "teacher age : " << teacher.getAge() << endl;
}
//值传递
void setFunc(MyTeacher teacher) {
	teacher.setAge(16);
	teacher.setName("jhone");
	cout << "teacher name : " << teacher.getName() << endl;
	cout << "teacher age : " << teacher.getAge() << endl;
}
//引用传递
void setFuncY(MyTeacher &teacher) {
	teacher.setAge(16);
	teacher.setName("jhone");
	cout << "teacher name : " << teacher.getName() << endl;
	cout << "teacher age : " << teacher.getAge() << endl;
}

void main() {
	//fun();


	//MyTeacher teacher;
	//teacher.setAge(20);
	//teacher.setName("小明");


	MyTeacher teacher = MyTeacher("xiaohong", 25);//没new关键字的是对象


	MyTeacher *teacher1 = new MyTeacher("xiaohong", 25);//有new关键字是指针 不会走虚构函数 需要自己释放


	delete teacher1;//释放对象 才会走虚构函数
	teacher1 = nullptr;


	cout << "teacher name : " << teacher.getName() << endl;
	cout << "teacher age : " << teacher.getAge() << endl;
	setFuncY(teacher);
	cout << "teacher name : " << teacher.getName() << endl;
	cout << "teacher age : " << teacher.getAge() << endl;
	system("pause");
}
Teacher.h
#pragma once
class MyTeacher
{
public:
	MyTeacher();//构造函数 (malloc)
	~MyTeacher();//虚构函数 释放在构造函数里面动态申请的内存 (free)

	void setAge(int age);
	int getAge();

	void setName(char *name);
	char* getName();
private:
	int age;
	char *name;
};
Teacher.cpp
#include <Teacher.h>
#include<iostream>


using namespace std;

//创建命名空间
namespace NSP_A {
	struct MyStudent
	{
		int age;
	};
}

MyTeacher::MyTeacher()
{
	std::cout << "MyTeacher 构造函数 " << this << std::endl;
}

MyTeacher::~MyTeacher()
{
	//使用了std的命名空间 就可以不要前面的std限定符
	cout << "MyTeacher 虚构函数 " << this << std::endl;
}

void MyTeacher::setAge(int age) {
	this->age = age;

	//命名空间的使用
	NSP_A::MyStudent t;
	t.age = 18;

	using NSP_A::MyStudent;
	MyStudent t2;
	t2.age = 16;
}
int MyTeacher::getAge() {
	return this->age;
}

void MyTeacher::setName(char *name) {
	this->name = name;
}
char* MyTeacher::getName(){
	return this->name;
}

浅拷贝和深拷贝

MyStudent.h

class MyStudent
{
public:
	MyStudent(int age,char*name,char *teacherName);
	~MyStudent();
	//重写默认的拷贝构造函数
	MyStudent(const MyStudent &student);

public:
	int age;
	char *name;
	char *teacherName;

private:

};

MyStudent.cpp

#include <MyStudent.h>
#include <iostream>

using namespace std;

MyStudent::MyStudent(int age, char*name, char *teacherName):age(age),name(name),teacherName(teacherName)
{
	cout << "无参构造函数" << this << endl;
}

MyStudent::~MyStudent()
{
	cout << "虚构函数" << this << endl;
}

//默认拷贝构造函数 浅拷贝
MyStudent::MyStudent(const MyStudent &student) {
	cout << "拷贝构造函数" << this << endl;
	this->age = student.age;
	this->name = student.name;
	this->teacherName = student.teacherName;
}

main.cpp

void copyTest() {
	MyStudent student = MyStudent(21, "jake", "jhone");
	MyStudent stu2 = student;//也会调用拷贝构造函数
}9:58 2017/10/23

void main() {
	copyTest();
	system("pause");
}


注意:浅拷贝可能引出的问题 我在构造函数申请一块内存, 虚构函数去释放这块内存 当我退出时释放内存 由于拷贝的对象是执行的拷贝构造函数 这里没有申请内存 用的是同一块内存 这块内存释放后 执行拷贝对象虚构函数时 会抛出异常 是由野指针造成的 

问题产生

#include <MyStudent.h>
#include <iostream>

using namespace std;

MyStudent::MyStudent(int age, char*name, char *teacherName):age(age)
{
	cout << "无参构造函数" << this << endl;

	int len = strlen(name);
	this->name = (char *)malloc(len + 1);
	strcpy(this->name, name);

	len = strlen(teacherName);
	this->teacherName = (char *)malloc(len + 1);
	strcpy(this->teacherName, teacherName);
}

MyStudent::~MyStudent()
{
	cout << "虚构函数" << this << endl;
	free(this->name);
	free(this->teacherName);

}

//默认拷贝构造函数 浅拷贝
MyStudent::MyStudent(const MyStudent &student) {
	cout << "拷贝构造函数" << this << endl;
	this->age = student.age;
	this->name = student.name;
	this->teacherName = student.teacherName;
}
    


解决浅拷贝可能存在的问题 在拷贝函数重新申请空间 就可以了

MyStudent::MyStudent(const MyStudent &student) {
	cout << "拷贝构造函数" << this << endl;
	this->age = student.age;
	//this->name = student.name;
	int len = strlen(student.name);
	this->name = (char *)malloc(len + 1);
	strcpy(this->name, student.name);
	//this->teacherName = student.teacherName;
	len = strlen(student.teacherName);
	this->name = (char *)malloc(len + 1);
	strcpy(this->name, student.teacherName);
}














猜你喜欢

转载自blog.csdn.net/daividtu/article/details/78296989