複合類、析構函數、指針複合、new、delete
1、複合類:
1.1 複合類的成員變量,是另一個類的對象
2、析構函數:
2.1 收回構造函數時申請的資源,垃圾回收
2.2 調用析構函數的兩種情況:離開對象的作用域、銷燬對象刪除一個指向對象的指針
2.3 特點:無返回值,無參,唯一,公有
2.4 當一個類中有指針成員變量,而且指針指向的空間是手動開闢的堆空間時,必須寫析構函數
D、複合類
Work.h
#ifndef __C__No727Class__Work__
#define __C__No727Class__Work__
#include <iostream>
using namespace std;
class Work //最先編寫的頭文件
{
private:
int number; //作業數量
public:
Work(int n); //自定義構造函數
~Work(); //析構函數
void print(); //輸出信息
};
#endif /* defined(__C__No727Class__Work__) */
Work.cpp
#include "Work.h" //成員函數的實現
Work::Work(int n)
{
number = n;
}
Work::~Work()
{
cout << "析構Work" << endl;
}
void Work::print()
{
cout << "number is :" << number << endl;
}
Student.h
#include <iostream>
#include "Work.h" //包含
class Student
{
private:
char *name;
int num;
Work w; //類類型的私有成員變量w,表示作業
public:
Student(char *a, int b, int c);
~Student();
void print();
};
#endif /* defined(__C__No727Class__Student__) */
Student.cpp
#include "Student.h"
Student::Student(char *a, int b, int c):w(c) //:成員變量(賦給成員變量的值),初始化列表
{
name = a;
num = b;
}
Student::~Student()
{
cout << "析構Student" << endl;
}
void Student::print()
{
w.print();
cout << "name is :" << name << endl;
cout << "num is :" << num << endl;
}
main.cpp
#include "Student.h"
int main()
{
Student s("名字", 123401, 10);
s.print();
return 0; //首先析構Student,過程中因需要析構Work纔可以完成析構Student,所以調用析構Work的函數
}
E、指針複合
Toy.h
#ifndef __C__No727Class__Toy__
#define __C__No727Class__Toy__
#include <iostream>
using namespace std;
class Toy
{
private:
int number;
public:
Toy(int n);
~Toy();
void print();
};
#endif /* defined(__C__No727Class__Toy__) */
Toy.cpp
#include "Toy.h"
Toy::Toy(int n)
{
number = n;
}
Toy::~Toy()
{
cout << "析構Toy" << endl;
}
void Toy::print()
{
cout << "number is " << number << endl;
}
Baby.h
#ifndef __C__No727Class__Baby__
#define __C__No727Class__Baby__
#include <iostream>
#include "Toy.h"
using namespace std;
class Baby
{
public:
Baby(char * n, int a, int number);
~Baby();
void print();
private:
char * name;
int age;
Toy *t; //類類型的指針
};
#endif /* defined(__C__No727Class__Baby__) */
Baby.cpp
#include "Baby.h"
Baby::Baby(char * n, int a, int number)
{
name = n;
age = a;
t = new Toy(number); //手動分配內存,指針指向new出來的堆空間,堆空間裏面是一個Toy類的對象
}
Baby::~Baby()
{
cout << "析構Baby" << endl;
if(t) //如果指針所指向的空間有內容
{
delete t; //手動回收內存,把new出來的空間收回
t = NULL; //指針置空
}
}
void Baby::print()
{
(*t).print(); //t -> print();
cout << " name is " << name << endl;
cout << " age is " << age << endl;
}
main.cpp
#include "Baby.h"
int main()
{
Baby a("a", 1, 10);
a.print();
return 0; //首先析構Boy,並沒有直接使用Toy的對象,所以不會自動調用析構Toy的函數,需要手動回收,delete時調用析構Toy的函數
}
F、兩個成員變量
Paper.h
#ifndef __C__No727Class__Paper__
#define __C__No727Class__Paper__
#include <iostream>
using namespace std;
class Paper
{
public:
Paper(int n, char *s);
~Paper();
void show();
private:
int number; //兩個成員變量
char * subject;
};
#endif /* defined(__C__No727Class__Paper__) */
Paper.cpp#include "Paper.h"
Paper::Paper(int n, char *s)
{
number = n;
subject = s;
}
Paper::~Paper()
{
cout << "析構Paper" << endl;
}
void Paper::show()
{
cout << "數量 is : " << number << endl;
cout << "科目 is : "<< subject << endl;
}
Teacher.h
#ifndef __C__No727Class__Teacher__
#define __C__No727Class__Teacher__
#include <iostream>
#include "Paper.h"
class Teacher
{
private:
int num;
char * name;
Paper p;
public:
Teacher(int a, char * b, int n, char * s);
~Teacher();
void show();
};
#endif /* defined(__C__No727Class__Teacher__) */
Teacher.cpp#include "Teacher.h"
Teacher::Teacher(int a, char * b, int n, char * s):p(n, s)
{
num = a;
name = b;
}
Teacher::~Teacher()
{
cout << "析構Teacher" << endl;
}
void Teacher::show()
{
p.show();
cout << "編號 is : " << num << endl;
cout << "姓名 is : " << name << endl;
}
main.cpp#include "Teacher.h"
int main()
{
Teacher t(1234, "名字", 50, "計算機");
t.show();
return 0;
}