#include
using namespace std;
int main()
{
//在堆区空间中申请int大小空间
int *p1 = new int;
cout << *p1 << endl;
*p1 = 1314;
cout << *p1 << endl;
//在堆区空间中申请int大小空间并初始化
int *p2 = new int(10);
cout << *p2 << endl;
//在堆区空间中申请char大小空间并初始化
char *p3 = new char('H');
cout << *p3 << endl;
//释放(回收)内存空间
delete p1;
delete p2;
delete p3;
cout << "----------------------" << endl;
//在堆区空间中连续申请5个int大小空间
int *p4 = new int[5];
for(int i=0; i<5; i++)
{
cout << p4[i] << endl;
}
cout << "----------------------" << endl;
//在堆区空间中连续申请5个int大小空间并初始化
int *p5 = new int[5]{100,200,300,400,500};
for(int i=0; i<5; i++)
{
cout << p5[i] << endl;
}
//释放(回收)连续内存
delete []p4;
delete []p5;
//给指针来个指向 避免野指针
p1 = nullptr;
p2 = nullptr;
p3 = nullptr;
p4 = nullptr;
p5 = nullptr;
return 0;
}
类名(形参列表)
{
函数体内容;
}
示例:
#include
using namespace std;
class Stu
{
private:
string name;
int id;
public:
Stu() //无参构造函数 由系统默认提供
{
cout << "Stu::无参构造函数" << endl;
}
Stu(string name, int id)
{
this->name = name;
Stu::id = id;
cout << "Stu::有参构造函数" << endl;
}
void show()
{
cout << "姓名:" << name << " 学号:" << id << endl;
}
};
int main()
{
Stu s1; //自动调用无参构造函数
Stu s2("张三", 1001); //自动调用有参构造函数
s2.show();
return 0;
}
类名 (形参1,形参2,形参n):成员变量1(形参1),成员变量2(形参2),······,成员变量n(形参n)
注意:只有构造函数才有初始化列表,其他普通函数没有初始化列表。
示例:
#include
using namespace std;
class Bir
{
private:
int year;
int month;
int day;
public:
Bir(int y,int m, int d):year(y),month(m),day(d)
{
cout << "Bir::有参构造函数" << endl;
}
};
class Stu
{
private:
string name;
int id;
Bir bir;
public:
//无参构造函数
//Stu() {}
//有参构造函数
Stu(string name, int id, int y,int m, int d):name(name),id(id),bir(y,m,d)
{
cout << "Stu::有参构造函数" << endl;
}
void show(){cout << name;}
};
int main()
{
Stu s1("zhangsan", 1001, 2002, 9, 9); //先调用子对象的构造函数,再调用自己的构造函数
return 0;
}
~类名()
{
函数体内容;
}
示例 :
#include
using namespace std;
class Stu
{
private:
string name;
int id;
public:
//无参构造函数
Stu() {cout << "Stu::无参构造函数" << endl;}
//有参构造函数
Stu(string name, int id):name(name),id(id)
{
cout << "Stu::有参构造函数" << endl;
}
//析构函数
~Stu()
{
cout << "Stu::析构函数" << endl;
cout << this << endl;
}
void show()
{
cout << name << endl;
}
};
int main()
{
Stu s1; //自动调用无参构造
Stu s2("zhangsan", 1001); //自动调用有参构造函数
cout << "&s1 = " << &s1 << " &s2 = " << &s2 << endl;
//先构造的 后析构 后构造的 先析构
return 0;
}
#include
using namespace std;
class Stu
{
private:
string name;
int id;
double *score; //有指针成员变量时,并申请了堆区空间
public:
//无参构造函数
Stu() {cout << "Stu::无参构造函数" << endl;}
//有参构造函数
Stu(string name, int id, double b):name(name),id(id),score(new double(b))
{
//score = new double(b);
cout << "Stu::有参构造函数" << endl;
}
//析构函数
~Stu()
{
cout << "Stu::析构函数" << endl;
cout << this << endl;
delete score; //需要把析构函数显性定义出来,在其中把指针成员释放掉
}
void show(){cout << name << endl;}
};
int main()
{
Stu s1; //自动调用无参构造
Stu s2("zhangsan", 1001, 89); //自动调用有参构造函数
cout << "&s1 = " << &s1 << " &s2 = " << &s2 << endl;
Stu *p = new Stu; //自动调用无参构造函数
delete p; //自动调用析构函数
return 0;
}
类名(const 类名 &other)
{
函数体内容
}
类名 &operator=(const 类名&other)
{
函数体内容;
}
#include
using namespace std;
class Stu{
private:
int* id;
string name;
public:
Stu (); //无参构造
Stu(int id, string name); //有参构造
Stu(const Stu &other); //拷贝构造
~Stu(); //析构
Stu &operator=(const Stu &other);
//set
void set_id(int id);
void set_name(string name);
//get
int get_id();
string get_name();
//其他方法
void show_info();
};
//无参构造
Stu::Stu(){
cout << this->get_name() << " 调用了无参构造函数" << endl;
};
//有参构造
Stu::Stu(int id, string name):id(new int(id)),name(name)
{
// this->id = id;
// Stu::name = name;
cout << this->get_name() << " 调用了有参构造函数" << endl;
}
//拷贝构造
Stu::Stu(const Stu &other):id(other.id),name(other.name)
{
cout << this->get_name() << " 调用了拷贝构造函数" << endl;
}
//析构
Stu::~Stu(){
cout << this->get_name() << " 调用了析构函数" << endl;
}
//拷贝赋值
Stu& Stu::operator=(const Stu &other){
if(this != &other){
id = new int(*other.id);
name = other.name;
cout << this->get_name() << " 调用了拷贝赋值函数" << endl;
}
return *this;
}
//set
void Stu::set_id(int id){
*(this->id) = id;
}
void Stu::set_name(string name){
this->name = name;
}
//get
int Stu::get_id(){
return *(this->id);
}
string Stu::get_name(){
return this->name;
}
//其他方法
void Stu::show_info(){
cout << "id = " << *(this->id) <<endl;
cout << "name = " << this->get_name() << endl;
}
int main()
{
Stu s2(1010, "法外狂徒");
Stu *p = new Stu(1020, "德玛西亚");
Stu *p2 = new Stu(1030, "艾欧尼亚");
Stu s1(s2);
Stu s3;
s3 = *p2;
puts("");
s1.show_info();
s2.show_info();
s3.show_info();
(*p).show_info();
(*p2).show_info();
puts("");
delete p;
delete p2;
return 0;
}
设计一个Per类,
类中包含私有成员:
姓名、年龄、指针成员身高、体重,
再设计一个Stu类,
类中包含私有成员:
成绩、Per类对象p1,
设计这两个类的 构造函数、 析构函数 和 拷贝构造函数。
#ifndef __CLASS_H__
#define __CLASS_H__
#include
using namespace std;
class Pre
{
private:
string name;
int age;
int *high;
int *weight;
public:
//构造函数
Pre();
Pre(string name, int age, int high, int weight);
//析构函数
~Pre();
//拷贝构造函数
Pre(const Pre &other);
//获取姓名
string get_name();
};
class Stu
{
private:
//构造函数
int score;
Pre p;
public:
Stu();
Stu(int score, Pre p);
//析构函数
~Stu();
//拷贝构造函数
Stu(const Stu &other);
};
#endif // __CLASS_H__
#include "class.h"
//构造函数
Pre::Pre(){
cout << this->name << " 调用了无参构造函数" << endl;
}
Pre::Pre(string name, int age, int high, int weight){
this->name = name;
this->age = age;
this->high = new int(high);
this->weight = new int(weight);
cout << this->name << " 调用了有参构造函数" << endl;
}
//析构函数
Pre::~Pre(){
delete this->high;
delete this->weight;
cout << this->name << " 调用了析构函数" << endl;
}
//拷贝构造函数
Pre::Pre(const Pre &other){
this->name = other.name;
this->age = other.age;
this->high = new int(*(other.high));
this->weight = new int(*(other.weight));
cout << this->name << " 调用了拷贝构造函数" << endl;
}
string Pre:: get_name(){
return this->name;
}
#include "class.h"
Stu::Stu(){
cout << this->p.get_name() << " 调用了无参构造函数" << endl;
}
Stu::Stu(int score, Pre p){
this->score = score;
this->p = p;
cout << this->p.get_name() << " 调用了有参构造函数" << endl;
}
//析构函数
Stu::~Stu(){
cout << this->p.get_name() << " 调用了析构函数" << endl;
}
//拷贝构造函数
Stu::Stu(const Stu &other){
this->score = other.score;
this->p = other.p;
cout << this->p.get_name() << " 调用了拷贝构造函数" << endl;
}