阿里云wordpress搭建网站,万网个人网站建设教程,郑州千锋教育,中山好的网站建设公司哪家好文章目录 【 1. 基本原理 】【 2. 实例 】 【 1. 基本原理 】
在 C 中#xff0c;只有成员函数才有 this 指针#xff08;友元函数没有 this 指针#xff0c;因为友元不是类的成员#xff09;#xff0c;this 指针是所有成员函数的隐含参数。 在成员函数内部#xff0c;… 文章目录 【 1. 基本原理 】【 2. 实例 】 【 1. 基本原理 】
在 C 中只有成员函数才有 this 指针友元函数没有 this 指针因为友元不是类的成员this 指针是所有成员函数的隐含参数。 在成员函数内部this指针 可以用来指向调用对象每一个对象都能通过 this 指针来访问自己的地址。
【 2. 实例 】
// 比较Box1与Box2体积的大小
#include iostreamusing namespace std;class Box
{public:// 构造函数定义Box(double l2.0, double b2.0, double h2.0){cout Constructor called. endl;length l;breadth b;height h;}double Volume(){return length * breadth * height;}int compare(Box box){return this-Volume() box.Volume();}private:double length; // Length of a boxdouble breadth; // Breadth of a boxdouble height; // Height of a box
};int main(void)
{Box Box1(3.3, 1.2, 1.5); // Declare box1Box Box2(8.5, 6.0, 2.0); // Declare box2if(Box1.compare(Box2)){cout Box2 is smaller than Box1 endl;}else{cout Box2 is equal to or larger than Box1 endl;}return 0;
}