C++ 笔记(18)— 类和对象(this 指针、指向类的指针、类静态成员变量和函数)
发布日期:2021-05-10 08:44:00 浏览次数:18 分类:精选文章

本文共 4257 字,大约阅读时间需要 14 分钟。

this ���������������������������������������������������

1. this ������

��� C++ ��������������������������������������� this ���������������������������������������������������������������������������������������������������������������������������������������������������������

���������������������������������������������������������������������������������������������������������������

��������������������������������������������������������� this ������������������������������������������������������������������������������������������������������������������ this ������������������������������������������������������

���������������������������Talk("hello world") ��������������������������������� Talk(this, "hello world")���

class Human {
private:
void Talk(string msg) {
cout << msg << endl;
}
public:
void IntroduceSelf() {
Talk("hello world"); // ��������� Talk(this, "hello world")
}
};

2. ������������������

������ C++ ���������������������������������������������������������������������������

  • ������ -> ������������������������������������
  • ������������������������������������������������������������������������������������������

���������

class Box {
public:
Box(double l = 2.0, double b = 2.0, double h = 2.0) {
length = l;
breadth = b;
height = h;
}
double Volume() {
return length * breadth * height;
}
};
int main() {
Box Box1(3.3, 1.2, 1.5);
Box Box2(8.5, 6.0, 2.0);
Box* ptrBox;
ptrBox = &Box1;
cout << "Volume of Box1: " << ptrBox->Volume() << endl;
ptrBox = &Box2;
cout << "Volume of Box2: " << ptrBox->Volume() << endl;
return 0;
}

3. ������������

���������������������������������������������������������������������������������������������������������������

3.1 ������������������

������������������������������������������������������������������������������������������������

  • ���������������������������������������������������������������
  • ���������������������������������������������������������������
  • ��������������������������������������������������������������� $:: ���������������������
  • ������������������������������������������������������
  • ������������������������������������������������������

���������

class Box {
public:
Box(double l = 2.0, double b = 2.0, double h = 2.0) {
length = l;
breadth = b;
height = h;
objectCount++;
}
static int objectCount;
double Volume() {
return length * breadth * height;
}
};
int Box::objectCount = 0; // ���������������������������

3.2 ������������������

��������������������������������� static���������������������������������������������������������������������������������������

  • ������������������������������������������������������������������������������������
  • ������������������������������������������������������������������������������������������������������ $:: ���������

���������

class Box {
public:
Box(double l = 2.0, double b = 2.0, double h = 2.0) {
length = l;
breadth = b;
height = h;
objectCount++;
}
static int objectCount;
static int getCount() {
return objectCount;
}
double Volume() {
return length * breadth * height;
}
};
int main() {
// ���������������������������������������
cout << "������������: " << Box::getCount() << endl;
Box Box1(3.3, 1.2, 1.5);
Box Box2(8.5, 6.0, 2.0);
// ���������������������������������
cout << "������������: " << Box::getCount() << endl;
return 0;
}

���������������������������������

������������: 0
������������������
������������: 2

���������������������������������������������������������������������

  • ������������������������������������������������������������������������������
  • ���������������������������������������������������������������������������������������������������������
  • ������������������������������������������������������

������������������������������������������ this ���������������������������������������������������������������������������

上一篇:C++ 笔记(19)— 标准模板库(STL容器、STL迭代器、STL算法、STL容器特点、STL字符串类)
下一篇:C++ 笔记(17)— 类和对象(构造函数、析构函数)

发表评论

最新留言

哈哈,博客排版真的漂亮呢~
[***.90.31.176]2025年04月03日 13时26分21秒