我是靠谱客的博主 感性咖啡,这篇文章主要介绍c++——指向类的指针,现在分享给大家,希望可以做个参考。

#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;
		length = l;
		breadth  = b;
		height = h;
	}
	double Volume() 
	{
		return length * breadth*height;
	}
private:
	double length;
	double breadth;
	double height;
};

int main() 
{
	Box box1(2.0,1.0,2.0);
	Box box2(2.1, 1.2, 2.3);

	Box* ptrBox;
	ptrBox = &box1;

	cout << "Volume of box1:" << ptrBox->Volume() << endl;

	ptrBox = &box2;

	cout << "Volume of box2:" << ptrBox->Volume() << endl;

	return 0;
}

在这里插入图片描述

最后

以上就是感性咖啡最近收集整理的关于c++——指向类的指针的全部内容,更多相关c++——指向类内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(105)

评论列表共有 0 条评论

立即
投稿
返回
顶部