一.拷贝构造函数
当将一个对象赋值给另外一个对象的时候,只会调用一次构造函数 而此时,会默认再调用一次拷贝构造函数(浅拷贝)
2.自己实现拷贝构造函数,称为深拷贝
// 拷贝构造函数
Student(Student &s)
{
cout << "&Student" << endl;
}
//----------------------------------------
demo:
class Student
{
public:
Student(const char *name)
{
cout << "Student" << endl;
this->name = new char[100];
strcpy(this->name,name);
cout << "name = " << this->name << endl;
}
// 拷贝构造函数
// s == Jack
Student(Student &s)
{
cout << "&Student" << endl;
this->name = new char[100];
strcpy(this->name,s.name);
cout << "name = " << this->name << endl;
}
void show()
{
cout << name << endl;
}
~Student()
{
cout << "~Student" << endl;
delete []this->name;
this->name = NULL;
}
private:
char *name;
};
Student Jack("Tom");
// Student Rose = Jack;
Student Rose(Jack);
Jack.show();
Rose.show();
总结:对象赋值的时候一定会调用默认拷贝构造函数,所以要自己实现深拷贝,防止内存泄漏 //--------------------------------
二.继承
人的类Person(属性:姓名 性别 年龄 行为:吃饭,睡觉)
学生的类Student(属性:学号 行为:学习)
学生 = 人类 + 学习
继承好处:
1.代码复用
2.升级功能
//---------------------------
继承格式
继承方式 : public protected private
class 子类名 :继承方式 父类类名
{
}
继承方式总结:
1.类外访问父类成员
1.子类以公有方式(public)继承父类 子类对象可以访问父类的公有成员2.子类以保护方式(protected)继承父类 子类对象不可以访问父类的公有成员
3.子类以私有方式(private)继承父类 子类对象不可以访问父类的公有成员
所以所的继承方式都是以公有继承 //------------------------------------
2.子类,类内访问父类成员
1.父类的成员是私有(private)属性 子类无法访问
2.父类的成员是受保护(protected)属性 子类可以访问
3.父类的成员是公有(public)属性 子类可以访问 //------------------------------------
3.子类继承父类后,先调用父类构造函数再调用子类构造函数
4.子类与父类都有相同的方法,会默认调用子类的方法
5.子类与父类方法不能重载,重载只能在同一个类里面
6.子类与父类都有相同的方法,如果想要调用父类的方法 子类对象.父类名::方法(); jack.Person::show(10);
练习:自行设计子类和父类,进行测试
#include <iostream>
using namespace std;
// 父类(基类)
class Person
{
public:
Person()
{
cout << "person" << endl;
}
~Person()
{
cout << "~Perosn" << endl;
}
void show(int a)
{
cout << "person show" << endl;
cout << a << endl;
}
public:
int sex;
protected:
int age;
private:
string name;
};
// 子类(派生类)
class Student : public Person
{
public:
Student()
{
cout << "student" << endl;
//string Name = name;
int Age = age;
int Sex = sex;
}
~Student()
{
cout << "~student" << endl;
}
void show()
{
cout << "student show" << endl;
}
private:
int number;
};
int main()
{
Student jack;
// 子类可以调用父类的方法
jack.show();
jack.Person::show(10);
}
2.父类构造函数带参
1.父类构造函数参数列表初始化
Person(string name,int age,char sex):name(name),age(age),sex(sex)
{
cout << "person" << endl;
cout << this->name << " " << this->age << " " << this->sex << endl;
}
2.子类传参给父类
Student(string name,int age,char sex) : Person(name,age,sex)
{
cout << "student" << endl;
//string Name = name;
int Age = age;
int Sex = sex;
}
练习 : 学生类继承父类 学生类给父类传递参数,父类把参数打印出来,父子类构造函数类外实现
//--------------------------------
demo:
#include <iostream>
using namespace std;
// 父类(基类)
class Person
{
public:
Person(string name,int age,char sex);
~Person()
{
cout << "~Perosn" << endl;
}
void show(int a)
{
cout << "person show" << endl;
cout << a << endl;
}
public:
char sex;
protected:
int age;
private:
string name;
};
// 父类构造函数实现
Person::Person(string name,int age,char sex):name(name),age(age),sex(sex)
{
cout << "person" << endl;
cout << this->name << " " << this->age << " " << this->sex << endl;
}
//----------------------------------
// 子类(派生类)
class Student : public Person
{
public:
Student(string name,int age,char sex);
~Student()
{
cout << "~student" << endl;
}
void show()
{
cout << "student show" << endl;
}
private:
int number;
};
//-------------------------------
Student::Student(string name,int age,char sex) : Person(name,age,sex)
{
cout << "student" << endl;
//string Name = name;
int Age = age;
int Sex = sex;
}
int main()
{
Student jack("Tome",18,'M');
}