friend.cpp

来自「C++的一些简单代码 希望能给初学者带来小小的帮助」· C++ 代码 · 共 42 行

CPP
42
字号
#include <iostream>
using std::cout;
using std::endl;

class Box{
public:
	//Construtor
	Box(){
	cout<<"Default constructor called"<<endl;
	length=width=height=1.0;
}
	Box(double lvalue,double wvalue,double hvalue):length(lvalue),width(wvalue),height(hvalue){
		cout<<"Box constructor called"<<endl;
	}

	//Function to calculate the volume of a box
	double volume(){
		return length*width*height;
	}

	friend double boxSurface(Box theBox);

private:
	double length;
	double width;
	double height;
};
	
void main(){
	Box firstBox(80.0,50.0,40.0);
    cout<<endl;
	cout<<"Volume of first Box object is "<<firstBox.volume()
		<<endl;
	cout<<"Surface of first Box object is "<<boxSurface(firstBox)
		<<endl;
}

double boxSurface(Box theBox){
	return 2.0*(theBox.length*theBox.width+
		        theBox.length*theBox.height+
				theBox.width*theBox.height);
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?