构造函数与析构函数

来源:互联网 发布:cnc数控编程软件下载 编辑:程序博客网 时间:2024/06/16 19:39

head.h头文件的内容

#pragma once#include "iostream"#include "cstring"using namespace std;//-----------------------------------------类的定义-----------------------------------------------//  描述:定义类的地方//------------------------------------------------------------------------------------------------//-----------------------------------------【电脑类】---------------------------------------------//  描述:电脑类//------------------------------------------------------------------------------------------------class computer {private :    char brand[256];    int price;public:    void setBrand(char *brand) {        //因为参数和私有变量同一名称,所以加上类名表明是私有的。        strcpy_s(computer::brand, strlen(brand)+1,brand);    }     void setPrice(int price) {        computer::price = price;    }    void printf() {        cout << "brand=" << computer::brand << "--price=" << computer::price << endl;    }    //=====================================【外部函数】=========================================    //  描述:类的函数可以定义在类的外面,只要加类名就行。    //==========================================================================================    int func();};//------------------------------------------【Point类】-----------------------------------------//  描述:Point类 在这个类//  通过这个类可以了解到构造函数与虚构函数//----------------------------------------------------------------------------------------------class Point {private:    int x;    int y;public:    //====================================【构造函数】========================================    //  描述:创建类的时候会自动调用这个函数    //  构造函数没有返回值和返回类型    //  如果没有手动创建构造函数,系统会默认创建一个无参构造函数.    //  如果手动创建构造函数,系统将不会自动创建构造函数.    //  构造函数支持重构,系统会根据参数自动调用相对于的重构函数    //  可以通过参数表达式来对类的成员简便赋值    //========================================================================================    Point() {        cout << "这是一个无参构造函数" << endl;    }    //====================================【构造函数的重构函数】==============================    //  描述:重构函数    //========================================================================================    Point(int x, int y) {        Point::x = x;        Point::y = y;    }    Point(int x) :x(x) { //如果有多个参数用,号分开。例如:x(x),y(y)        cout << "通过参数表达式赋值:x=" << Point::x <<endl;    }    void printf() {        cout << "Point:x=" << x << "Point:y=" << y << endl;    }};        //----------------------------------[学生类]---------------------------------------//  描述:这个类拥有构造函数,析构函数//---------------------------------------------------------------------------------class student {private:    char *name;public:    //=============================【构造函数】================================    //  描述:拥有一个参数的构造函数    //=========================================================================    student(char *name) {        cout << "student的构造函数被调用" << endl;        student::name = new char[256];        strcpy_s(student::name,strlen(name)+1,name);    }    //==============================【析构函数】=================================    //  描述:析构函数,类似构造函数,区别在于只有一个函数,所以不能重构,要在函数名前加~    //  当类的离开它的作用域或者被销毁时会自动调用该函数    //===========================================================================    ~student()    {        cout << "student的析构函数被调用" << endl;        delete[] student::name;    }};

源.cpp的内容

#include "head.h"//类的函数可以定义在外部int computer::func() {    return 0;}void createStudent() {    student s("橘子味");    //程序运行接受,student离开其作用域,自动调用析构函数。}int main() {    //定义一个computer类    computer cmpt;    cmpt.setPrice(5000);    cmpt.setBrand("联想");    cmpt.printf();    cout << "------------------------------------------" << endl;    //这会调用Point的无参构造函数    Point pt1;    cout << "------------------------------------------" << endl;    //这会调用Point的有参构造函数    Point pt2(200, 200);    pt2.printf();    cout << "------------------------------------------" << endl;    //这个例子演示通过参数表达式来给类成员赋值。    Point pt3(230);    cout << "------------------------------------------" << endl;    //这个例子用来演示析构函数    createStudent();//在这个函数里面会创建一个student类,当离开这个函数时,student会自动调用他的析构函数.}
0 0
原创粉丝点击