![]() |
#2
色盲怎注册2011-03-23 14:11
冒死附上要求原文!事先说一下,这只是类的最基本应用,最好不要用虚函数等后续深入的知识来解答。对于原文,都是满篇的“了解”,“知道”。我看了五天了。任务做到后面的, 这个一直不会。我杯具!同求提高c++水平的方法!
Design a class with C++. Classes are a software construct that can be used to emulate a real world object. For instance, a software model of a car, a car "class", might contain data about the type of car and abilities such as accelerate or decelerate. A class is a programmer defined data type that has data, its members, and abilities, its methods. An object is a particular instance of a class. Please design a car ‘class’, include constructor and destructor, four properties: weight, color, speed and runstatus, three methods: run(), stop(), speedup(). The default speed is 0, Run() will change the car’s runstatus from ‘stop’ to ‘run’ and set the speed=50, stop() will change the runstatus from ‘run’ to ‘stop’ and set the speed=0, speedup() only be used when the car is running, it will increase the speed, add 5 one time, give some tips when the car’s properties changed. It is suggested to implement a method to show the car’s current properties. You should know three important concepts, Encapsulation, Inheritance and Polymorphism. 在多说一句,在C FREE 下运行的。加速函数的效果那叫一个杯具,代码肯定是有问题的。帮帮我吧! [ 本帖最后由 色盲怎注册 于 2011-3-23 14:14 编辑 ] |

用c++的知识,写一个汽车类,要求三个方法 run() stop() speedup().四个属性。要在启动汽车后才可以停止和加速。并且速度的起始值是50.每加一次,速度加5,并且显示此时汽车的状态。先按停止,提示汽车没启动。我自己写了个,杯具呀!我写的暂时没有写颜色重量等四个属性,纠结在三个成员函数对于功能的实现上。
求指导!讨论!BS,我深刻意识到我大学的时光白白浪费了。。。。

#include <iostream>
using std::cout;
using std::cin;
using std::endl;
#include <string>
using namespace std;
class CCar
{
public:
void carRun()
{
//string c;
//cout<<"请输入r/R后回车启动汽车:";
//cin>>c;
cout<<"汽车启动成功.初始速度:50KM/S。"<<endl<<endl;
};
void carStop()
{
//string s;
cout<<"汽车已停止前进."<<endl;
};
int carspeedUp()
{
//
// {
//string s;
//cin>>s;
//speed =50;
int i;
// i = 50;
i = i+5;
cout<<"现在速度是:"<<i<<"KM/S."<<endl;
return i;
// }
}
//private:
//int speed;
};
int main()
{ string c;
int speed;
int i;
i=50;
// cout<<"请输入r/R启动汽车:";
//cin>>c;
cout<<"按r/R后回车启动汽车,启动后按s/S停止前进,按a/A加速:"<<endl;
CCar car1;
while (c!="e"||c!="E")
{
cout<<endl;
cin>>c;
cout<<endl;
cout<<"请继续进行操作:";
cout<<endl
<<"启动后按s/S停止前进,按a/A加速:"
<<endl
<<"按e/E键退出。"<<endl<<endl;
if (c=="R"||c=="r" )
{
car1.carRun();
//cout<<"汽车启动成功.现在速度是50km/s。"<<endl<<endl;
}
else if (c=="s"||c=="S")
{cout<<"先按r/R启动汽车。" ;
cout<<"汽车没有正常启动!";
}
//cout<<"下一步操作:" ;
//cin>>c;
if (c=="s"||c=="S")
{
car1.carStop();
}
// {
// cout<<"汽车已停止前进."<<endl<<endl;
//}
//else
// {
// cout<<"请先启动汽车。" <<endl;
// }
if (c=="a"||c=="A")
{
car1.carspeedUp();
}
if (c=="e"||c=="E")
break;
//cout<<"下一步操作:" ;
// cin>>c;
}}
/* for (speed=50;speed<=300;speed++)
{
cin>>c;
if (c=="s"||c=="S")
{
cout<<"汽车已停止前进."<<endl<<endl;
}
else {
cout<<"请先启动汽车。" ;
}
if (c=="a"||c=="A")
{car1.carspeedUp(i);
cout<<speed;}
else {cout<<"请先启动汽车。"; }
}
}*/
//{for ((c=="R"||c=="r")&&(su=="s"||su="S") )
//{
//cout<<"汽车已停止前进."<<endl<<endl;
//}}
//}
!using std::cout;
using std::cin;
using std::endl;
#include <string>
using namespace std;
class CCar
{
public:
void carRun()
{
//string c;
//cout<<"请输入r/R后回车启动汽车:";
//cin>>c;
cout<<"汽车启动成功.初始速度:50KM/S。"<<endl<<endl;
};
void carStop()
{
//string s;
cout<<"汽车已停止前进."<<endl;
};
int carspeedUp()
{
//
// {
//string s;
//cin>>s;
//speed =50;
int i;
// i = 50;
i = i+5;
cout<<"现在速度是:"<<i<<"KM/S."<<endl;
return i;
// }
}
//private:
//int speed;
};
int main()
{ string c;
int speed;
int i;
i=50;
// cout<<"请输入r/R启动汽车:";
//cin>>c;
cout<<"按r/R后回车启动汽车,启动后按s/S停止前进,按a/A加速:"<<endl;
CCar car1;
while (c!="e"||c!="E")
{
cout<<endl;
cin>>c;
cout<<endl;
cout<<"请继续进行操作:";
cout<<endl
<<"启动后按s/S停止前进,按a/A加速:"
<<endl
<<"按e/E键退出。"<<endl<<endl;
if (c=="R"||c=="r" )
{
car1.carRun();
//cout<<"汽车启动成功.现在速度是50km/s。"<<endl<<endl;
}
else if (c=="s"||c=="S")
{cout<<"先按r/R启动汽车。" ;
cout<<"汽车没有正常启动!";
}
//cout<<"下一步操作:" ;
//cin>>c;
if (c=="s"||c=="S")
{
car1.carStop();
}
// {
// cout<<"汽车已停止前进."<<endl<<endl;
//}
//else
// {
// cout<<"请先启动汽车。" <<endl;
// }
if (c=="a"||c=="A")
{
car1.carspeedUp();
}
if (c=="e"||c=="E")
break;
//cout<<"下一步操作:" ;
// cin>>c;
}}
/* for (speed=50;speed<=300;speed++)
{
cin>>c;
if (c=="s"||c=="S")
{
cout<<"汽车已停止前进."<<endl<<endl;
}
else {
cout<<"请先启动汽车。" ;
}
if (c=="a"||c=="A")
{car1.carspeedUp(i);
cout<<speed;}
else {cout<<"请先启动汽车。"; }
}
}*/
//{for ((c=="R"||c=="r")&&(su=="s"||su="S") )
//{
//cout<<"汽车已停止前进."<<endl<<endl;
//}}
//}
多谢了!分不多!
[ 本帖最后由 色盲怎注册 于 2011-3-23 14:19 编辑 ]