求C++考试题答案啊!急!急!急!
写一个程序,定义抽象基类“话费”,含有计算话费的纯虚函数,由该类派生出3个类,套餐1:数据成员为月租费20,通话时间,及每分钟话费0.2元。套餐2:数据成员为基本消费12...
写一个程序,定义抽象基类“话费”,含有计算话费的纯虚函数,由该类派生出3个类,套餐1:数据成员为月租费20,通话时间,及每分钟话费0.2元。套餐2:数据成员为基本消费120元,超时时间及每分钟话费0.1元。套餐3:固定话费500元,不限时。用虚函数分别求各种套餐的每月话费情况,并定义基类指针数组,使每个元素指向派生类对象,输出每一种对象的话费值。在主程序中验证并给出结果。
有人在做么?在做先说句话给点希望… 展开
有人在做么?在做先说句话给点希望… 展开
展开全部
#include <iostream>
using namespace std;
class fee{
public:
virtual double getTotalFee(int iMinutes) = 0;
};
class A : public fee{
public:
A():dRent(20.0), dFeilv(0.2){}
virtual double getTotalFee(int iMinutes)
{
return dRent += iMinutes * dFeilv;
}
private:
double dRent ;
double dFeilv ;
};
class B : public fee{
public:
B():dRent(120), iOverTime(200), dFeilv(0.1){}
virtual double getTotalFee(int iMinutes)
{
return (iMinutes>iOverTime)?(120+(iMinutes-200)*dFeilv):120.0;
}
private:
double dRent;
double dFeilv;
int iOverTime;
};
class C : public fee{
public:
virtual double getTotalFee(int iMinutes)
{
return 500.0;
}
};
int main()
{
fee *feeArray[3];
feeArray[0] = new A;
feeArray[1] = new B;
feeArray[2] = new C;
for (int i = 0;i<3; ++i) {
cout<<feeArray[i]->getTotalFee(500)<<endl;
}
return 0;
}
using namespace std;
class fee{
public:
virtual double getTotalFee(int iMinutes) = 0;
};
class A : public fee{
public:
A():dRent(20.0), dFeilv(0.2){}
virtual double getTotalFee(int iMinutes)
{
return dRent += iMinutes * dFeilv;
}
private:
double dRent ;
double dFeilv ;
};
class B : public fee{
public:
B():dRent(120), iOverTime(200), dFeilv(0.1){}
virtual double getTotalFee(int iMinutes)
{
return (iMinutes>iOverTime)?(120+(iMinutes-200)*dFeilv):120.0;
}
private:
double dRent;
double dFeilv;
int iOverTime;
};
class C : public fee{
public:
virtual double getTotalFee(int iMinutes)
{
return 500.0;
}
};
int main()
{
fee *feeArray[3];
feeArray[0] = new A;
feeArray[1] = new B;
feeArray[2] = new C;
for (int i = 0;i<3; ++i) {
cout<<feeArray[i]->getTotalFee(500)<<endl;
}
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询