标签:
控制杆类头文件
class Lever{
protected:
int position;
public:
Lever();
~Lever();
Lever(int);
int getPosition();
void upPosition();
void downPosition();
};控制杆类源文件
#include "Lever.h"
#include <iostream.h>
Lever::Lever()
{}
Lever::Lever(int p)
{
this->position = p;
}
Lever::~Lever()
{}
int Lever::getPosition()
{
return this->position;
}
void Lever::upPosition()
{
if(position >= 4)
{
cout<<"Overflow,you can‘t up the lever!"<<endl;
}
else
this->position++;
return ;
}
void Lever::downPosition()
{
if(position <= 1)
{
cout<<"Overflow,you can‘t down the lever!"<<endl;
}
else
this->position--;
return ;
}刻度盘类头文件
class Dial{
protected:
int position;
public:
Dial();
~Dial();
Dial(int);
int getPosition();
void upPosition();
void downPosition();
};刻度盘类源文件
#include "Dial.h"
#include <iostream.h>
Dial::Dial()
{}
Dial::Dial(int p)
{
this->position = p;
}
Dial::~Dial()
{}
int Dial::getPosition()
{
return this->position;
}
void Dial::upPosition()
{
if(position >= 3)
{
cout<<"Overflow,you can‘t up the Dial!"<<endl;
}
else
this->position++;
return ;
}
void Dial::downPosition()
{
if(position <= 1)
{
cout<<"Overflow,you can‘t down the Dial!"<<endl;
}
else
this->position--;
return ;
}雨刷类头文件
class Wiper{
protected:
int speed;
public:
Wiper();
~Wiper();
Wiper(int);
void setWiperSpeed(int s);
int getWiperSpeed();
};雨刷类源文件
#include "Wiper.h"
Wiper::Wiper()
{}
Wiper::~Wiper()
{}
Wiper::Wiper(int s)
{
this->speed = s;
}
void Wiper::setWiperSpeed(int s)
{
this->speed = s;
}
int Wiper::getWiperSpeed()
{
return this->speed;
}代理类头文件
#include "Lever.h"
#include "Dial.h"
#include "Wiper.h"
class Agent{
protected:
Wiper objw;
Lever objl;
Dial objd;
public:
Agent();
~Agent();
void upLeverPosition();
void downLeverPosition();
void upDialPosition();
void downDialPosition();
void judgeWiperSpeed();
void show();
};代理类源文件
#include "Agent.h"
#include <iostream.h>
Agent::Agent():objl(1),objd(1)
{
}
Agent::~Agent()
{}
void Agent::upLeverPosition()
{
this->objl.upPosition();
}
void Agent::downLeverPosition()
{
this->objl.downPosition();
}
void Agent::upDialPosition()
{
this->objd.upPosition();
}
void Agent::downDialPosition()
{
this->objd.downPosition();
}
void Agent::judgeWiperSpeed()
{
int leverPosition = this->objl.getPosition();
int dialPosition = this->objd.getPosition();
int wiperSpeed = 0;
switch(leverPosition)
{
case 1:
wiperSpeed = 0;break;
case 2:
switch(dialPosition)
{
case 1:
wiperSpeed = 4; break;
case 2:
wiperSpeed = 6;break;
case 3:
wiperSpeed = 12;break;
}
break;
case 3:
wiperSpeed = 30;break;
case 4:
wiperSpeed = 60;break;
}
this->objw.setWiperSpeed(wiperSpeed);
}
void Agent::show()
{
cout<<"Now the Lever‘s position is: ["<<this->objl.getPosition()<<"]"<<endl;
cout<<"The Dial‘s position is: ["<<this->objd.getPosition()<<"]"<<endl;
cout<<"The Wiper‘s speed is: ["<<this->objw.getWiperSpeed()<<"]"<<endl<<endl;
}主函数源文件
#include <iostream.h>
#include <stdlib.h>
#include "Agent.h"
void menu()
{
cout<<"============== 1 Lever up ================"<<endl;
cout<<"============== 2 Lever down =============="<<endl;
cout<<"============== 3 Dial up ================="<<endl;
cout<<"============== 4 Dial down ==============="<<endl;
cout<<"============== 0 Exit ===================="<<endl;
cout<<endl;
cout<<"Please input your choice:"<<endl;
}
void main()
{
Agent obja;
int input;
menu();
cin>>input;
while(1)
{
switch(input)
{
case 1:
obja.upLeverPosition();
break;
case 2:
obja.downLeverPosition();
break;
case 3:
obja.upDialPosition();
break;
case 4:
obja.downDialPosition();
break;
case 0:
exit(0);
}
obja.judgeWiperSpeed();
obja.show();
menu();
cin>>input;
}
}大家好好看下这个代码,把类之间的关系搞清楚,尤其是类的封装性、类之间的低耦合性以及类的内聚性对面向对象设计的影响。然后
再去完善一下第七次关于继承的作业。
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/laoduan_78/article/details/49888711