#include
#include
using namespace std;
class product
{
public:
product(string name,double _price,int _store)
{
total_price += _price*_store;
total_store += _store;
}
static double get_total_price(){return total_price;)
static int get_total_store(){return total_store;}
static double total_store;
static int total_price;
};
double product::total_price;
int product::total_store;
int main()
{
product p1("p1",1,100);
product p2("p2",2,100);
cout <<product::get_total_price() <<endl;
cout <<product::get_total_store() <<endl;
return 0;
}
修改处见注释,供参考:
#include <iostream>
#include <string>
using namespace std;
class product
{
public:
product(string name,double _price,int _store)
{
total_price += _price*_store;
total_store += _store;
}
static double get_total_price(){return total_price;}//')'这里打成括号了
static int get_total_store(){return total_store;}
static int total_store; //数据类型错误
//static double total_store;
static double total_price;//数据类型错误
//static int total_price;
};
double product::total_price;
int product::total_store;
int main()
{
product p1("p1",1,100);
product p2("p2",2,100);
cout <<product::get_total_price() <<endl;
cout <<product::get_total_store() <<endl;
return 0;
}
是这样吗?
#include<iostream>
using namespace std;
class product
{
public:
product(string name, double _price, int _store)
{
total_price += _price * _store;
total_store = _store;
}
double total_store=0;
int total_price=0;
double get_total_price() {return total_price;}
int get_total_store() { return total_store; }
};
int main()
{
product p1("p1", 1, 100);
product p2("p2", 2, 100);
cout << p1.get_total_price() << endl;
cout << p2.get_total_store() << endl;
return 0;
}