C++ 类

构造函数&析构函数

  • 构造函数在每次创建类的新对象时执行,为某些成员变量设置初始值。
  • 构造函数的名称与类的名称是完全相同的,并且不会返回任何类型,也不会返回 void
  • 可以采用 初始化列表 构建构造函数
  • 析构函数在每次删除所创建的对象时执行,有助于在跳出程序(比如关闭文件、释放内存等)前释放资源。
  • 析构函数的名称与类的名称是完全相同的,只是在前面加了个波浪号(~)作为前缀,它不会返回任何值,也不能带有任何参数
  • 析构函数在程序结束时自动调用,不用在程序中声明。
#include <iostream>

using namespace std;

class Box {
    int length;
    int width;
    int height;
public:
    Box(int l, int w, int h);

    ~Box();

    void show();
};

// 初始化列表构造函数
Box::Box(int l, int w, int h) : length(l), width(w), height(h) {};

Box::~Box() { cout << "Object is being deleted" << endl; } // 不用调用,程序结束时自动调用

void Box::show() {
    cout << length << '\t' << width << '\t' << height << endl;
    cout << "体积为:" << length * width * height << endl;
}

int main() {
    Box box = Box(2, 3, 4);
    box.show();
}
2   3   4
体积为:24
Object is being deleted

友元函数

  • 友元函数并不是成员函数(尽管友元函数的原型有在类的定义中出现过),但可以访问类的所有私有(private)成员和保护(protected)成员。
  • 在函数声明最前面加关键字 friend
#include <iostream>

using namespace std;

class Box {
    double width;
public:
    friend void printWidth(Box box); // 友元函数

    void setWidth(double wid);
};

// 成员函数定义
void Box::setWidth(double wid) {
    width = wid;
}

// 请注意:printWidth()不是任何类的成员函数
void printWidth(Box box) {
    cout << "Width of box : " << box.width << endl; //因为 printWidth() 是 Box 的友元,它可以直接访问该类的任何成员
}

// 程序的主函数
int main() {
    Box box;
    box.setWidth(10.0); // 使用成员函数设置宽度
    printWidth(box); // 使用友元函数输出宽度(width是private型)
    return 0;
}
Width of box : 10

this指针

  • 在 C++ 中,每个对象都能通过 this 指针来访问自己的地址(this打印出来就是地址
  • this 指针是所有成员函数的隐含参数,在成员函数内部可以用来指向调用对象
  • 友元函数没有 this 指针,因为友元不是类的成员。只有成员函数才有 this 指针。
#include <iostream>

using namespace std;

class Box {
public:
    // 构造函数定义
    Box(double l = 2.0, double b = 2.0, double h = 2.0) {
        cout << "Constructor called." << endl;
        cout << this << endl; // 打印对象地址
        length = l;
        breadth = b;
        height = h;
    }

    double Volume() {
        return length * breadth * height;
    }

    // 调用this
    int compare(Box box) {
        return this->Volume() > box.Volume();
    }

private:
    double length;     // Length of a box
    double breadth;    // Breadth of a box
    double height;     // Height of a box
};

int main() {
    Box Box1(3.3, 1.2, 1.5);    // Declare box1
    Box Box2(8.5, 6.0, 2.0);    // Declare box2

    if (Box1.compare(Box2)) {
        cout << "Box2 is smaller than Box1" << endl;
    } else {
        cout << "Box2 is equal to or larger than Box1" << endl;
    }
    return 0;
}
Constructor called.
0x7fff5b500b80
Constructor called.
0x7fff5b500b68
Box2 is equal to or larger than Box1

类的静态成员

  • static 关键字来把类成员定义为静态的。当我们声明类的成员为静态时,这意味着无论创建多少个类的对象,静态成员都只有一个副本(即只初始化一次)
#include <iostream>

using namespace std;

class Box {
private:
    double length;     // 长度
    double breadth;    // 宽度
    double height;     // 高度

public:
    static int objectCount; // 静态成员只初始化一次

    // 构造函数定义
    Box(double l = 2.0, double b = 2.0, double h = 2.0) {
        cout << "Constructor called." << endl;
        length = l;
        breadth = b;
        height = h;
        objectCount++; // 每次创建对象时增加 1
    }
};

// 初始化类 Box 的静态成员
int Box::objectCount = 0;

int main() {
    Box Box1(3.3, 1.2, 1.5);    // 声明 box1
    Box Box2(8.5, 6.0, 2.0);    // 声明 box2

    // 输出对象的总数
    cout << "Total objects: " << Box::objectCount << endl;

    return 0;
}
Constructor called.
Constructor called.
Total objects: 2
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 本文博客园地址:http://www.cnblogs.com/xiongxuanwen/p/4290086.htm...
    先之阅读 863评论 0 5
  • 类和对象 如果说对象是楼房,那么实例化就是建造楼房,类表示的就是建造图纸;对象的四大特征:抽象:抽象出具体事物的普...
    秋风弄影阅读 661评论 0 0
  • C++文件 例:从文件income. in中读入收入直到文件结束,并将收入和税金输出到文件tax. out。 检查...
    SeanC52111阅读 2,850评论 0 3
  • 慢慢的 你走来 这一趟 走了四百万年 漫步时光里 回望来时的路 你是谁 ...
    文懿阅读 95评论 0 1