C++ 类成员 有静态成员, 5只猫咪总体重。

#include 	 
using namespace std;
class Cat {
	char kind[20];
	double weight;
	static double total_weight;
public:
	Cat();
	void Print();
	static void TPrint();
};

double Cat::total_weight = 0;
int main() {
	Cat obj[5];
	int i;
	for (i = 0; i < 5; i++)
		obj[i].Print();
	
	Cat::TPrint();

	return 0;

}
Cat::Cat() {
	cin >> kind >> weight;
	total_weight += weight;
}
void Cat::Print() {
	cout << "品种:" << kind << " 重量:" << weight << endl;
}
void Cat::TPrint() {
	cout << "小猫总重量是:" << total_weight << endl;
}

你可能感兴趣的:(算法)