C++第二节(2):复合类、析构函数、new、delete

来源:互联网 发布:apache cxf 2.7.6下载 编辑:程序博客网 时间:2024/06/05 02:09

复合类、析构函数、指针复合、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;}

0 0
原创粉丝点击