c++类的基本操作

#include<iostream>
using namespace std;

//  第一种调用方法

/*class Time//声明
{
private://私人定义,分别定义
	int height, width, lth;//以求长方体体积为例
public://公共部分  所有成员都可用
	int  volumu()
	{
		return height*width*lth;//求出体积
	}
	void sr()//输入函数调用
	{
		cin >> height >> width >> lth;
	}
};

int main()//主函数
{
	Time p1, p2;//定义对象p1,p2
	p1.sr();//调用函数(在class 中定义好了)
	cout << p1.volumu() << endl;
	p2.sr();
	cout << p2.volumu() << endl;
	return 0;
}

*/
/

/第二种

class Time
{
private:
	int h, w, l;
public:
	int tj();
	void sr();
};
//外声明
int Time::tj()
{
	return h*w*l;
}
void Time::sr()
{
	cin >> h >> w >> l;
}
int main()//主函数
{
	Time p1, p2;//定义对象p1,p2
	p1.sr();//调用函数(在class 中定义好了)
	cout << p1.tj() << endl;
	p2.sr();
	cout << p2.tj() << endl;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_44922497/article/details/100624802
今日推荐