Joystick Class header File
Class Lever{protected:int Position;public:lever (); ~lever (); Lever (int); int getPosition (); void upposition (); void Downposition ();};
Control bar Class source file
#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;} Elsethis->position++;return;} void Lever::d ownposition () {if (position <= 1) {cout<< "overflow,you can ' t down the lever!" <<endl;} Elsethis->position--;return;}
Dial class header file
Class Dial{protected:int position;public:dial (); ~dial ();D ial (int); int getPosition (); void upposition (); void Downposition ();};
Dial class source file
#include "Dial.h" #include <iostream.h>dial::D ial () {}dial::D ial (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;} Elsethis->position++;return;} void Dial::d ownposition () {if (position <= 1) {cout<< "overflow,you can ' t down the dial!" <<endl;} Elsethis->position--;return;}
Wiper class Header File
Class Wiper{protected:int Speed;public:wiper (); ~wiper (); Wiper (int); void setwiperspeed (int s); int getwiperspeed ();};
Wiper class Source files
#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;}
Proxy class header File
#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 ();};
proxy class source files
#include "Agent.h" #include <iostream.h>agent::agent (): OBJL (1), OBJD (1) {}agent::~agent () {}void Agent:: Upleverposition () {this->objl.upposition ();} void Agent::d ownleverposition () {this->objl.downposition ();} void Agent::updialposition () {this->objd.upposition ();} void Agent::d owndialposition () {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 position is: [" <<this->objd.getposition () << "]" <<endl;cout << "The Wiper ' s speed is: ["<<this->objw.getwiperspeed () <<"] "<<ENDL<<ENDL;}
Main function Source file
#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;}}
Let's take a good look at this code and figure out the relationship between classes, especially the encapsulation of classes, the low coupling between classes, and the effect of class cohesion on object-oriented design. And then
Again to improve the seventh time about the inheritance of the job.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Wiper Job--four classes of implementations