#include <iostream>
#include <string>
using namespace std;
//首先定义一个Sensor类如下
class Sensor
{
protected:
string sensorId; //传感器ID
int interval; //采集频率
public:
Sensor(string sId, int inv)
{
sensorId = sId;
interval = inv;
}
void print()
{
cout << "sensorId: " << sensorId << endl;
cout << "interval: " << interval << endl;
}
};
//定义一个温度传感器类如下:
class TSensor : virtual public Sensor
{
protected:
double temperature;
public:
TSensor(string sId, int inv, double st) : Sensor(sId, inv)
{
temperature = st;
}
void print()
{
cout << "sensorId: " << sensorId << endl;
cout << "interval: " << interval << endl;
cout << "temperature: " << temperature << endl;
}
};
//参照TSensor补全HSensor
class HSensor : virtual public Sensor
{
protected:
double humidity;
public:
HSensor(string sId, int inv, double hu) : Sensor(sId, inv)
{
humidity = hu;
}
void print()
{
cout << "sensorId: " << sensorId << endl;
cout << "interval: " << interval << endl;
cout << "humidity: " << humidity << endl;
}
};
class THSensor : public TSensor, public HSensor
{
public:
THSensor() : Sensor("c000", 0), TSensor("c000", 0, 0), HSensor("c000", 0, 0) {}
THSensor(string sId, int inv, double st, double sh) : Sensor(sId, inv), TSensor(sId, inv, st), HSensor(sId, inv, sh)
{
}
string getSensorId()
{
return sensorId;
}
double getTemperature()
{
return temperature;
}
void print()
{
cout << "sensorId: " << TSensor::sensorId;
cout << " interval: " << TSensor::interval;
cout << " temperature: " << temperature;
cout << " humidity: " << humidity << endl;
}
};
class SensorWareHouse
{
private:
int capacity;
THSensor *p;
int size;
public:
SensorWareHouse(int capacity)
{
p = new THSensor[capacity];
this->capacity = capacity;
size = 0;
}
~SensorWareHouse()
{
delete []p;
/**请补全**/
}
void add(THSensor &s)
{
*(p+size) = s;
size++;
/**请补全**/
}
void print()
{
for (int i = 0; i < size; i++)
{
p[i].print();
}
}
void print(double t)
{
cout << "higher temperature than " << t << " is list as follow:" << endl;
for (int i = 0; i < size; i++)
{
if (p[i].getTemperature()>t)
{
p[i].print();
}
}
}
THSensor *getTHSensor(string sensorId)
{
for (int i = 0; i < size; i++)
{
if (p[i].getSensorId() == sensorId)
{
return &p[i];
//返回查询到的传感器地址
}
}
return NULL;
}
};
int main()
{
THSensor s1("s001", 10, 20, 30);
THSensor s2("s002", 10, 21, 31);
SensorWareHouse wh(10);
wh.add(s1);
wh.add(s2);
wh.print();
double t;
cout<<"please input max temperature: ";
cin>>t;
wh.print(t);
while(true) {
string sensorId;
cout<<"please input your sensorId(quit input s000): ";
cin>>sensorId;
if(sensorId == "s000") return 0;
else {
THSensor *s = wh.getTHSensor(sensorId);
if(s != NULL) s->print();
else {
cout<<"not find."<<endl;
}
}
}
return 0;
}
c:\Users\Lenovo\Desktop\CODE_Cpp\Exercise\2.cpp: In member function 'void SensorWareHouse::add(THSensor&)':
c:\Users\Lenovo\Desktop\CODE_Cpp\Exercise\2.cpp:37:7: warning: defaulted move assignment for 'TSensor' calls a non-trivial move assignment operator for virtual base 'Sensor' [-Wvirtual-move-assign]
class TSensor : virtual public Sensor
c:\Users\Lenovo\Desktop\CODE_Cpp\Exercise\2.cpp:63:7: warning: defaulted move assignment for 'HSensor' calls a non-trivial move assignment operator for virtual base 'Sensor' [-Wvirtual-move-assign]
class HSensor : virtual public Sensor
补充:上面的是warning 信息。 我发现了就是 *(p+size) = s; 的问题,但是不知道产生的原因。
想知道warning产生的原因和解决方法(当然这个warning对程序的实现没有影响)。
更新:目前找到了2个解决办法:
1)修改编译器版本回退为c++03
2)添加~Sensor() 析构函数
但是目前 仍不知道内部出现waring原因!