Wednesday, October 8, 2008

the most simple class

//dog.h
#ifndef IOSTREAM
#define IOSTREAM
#include < iostream.h>
#endif
class Dog_T
{
public:
  Dog_T();
  bool ChangeName();
  bool PastOneDay();
  bool DisplayInfo();
private:
  int iAge;
  char* cpName;
};

//dog.cpp
#include "dog.h"
Dog_T::Dog_T():iAge(1){
  cpName=new char();
  ChargeName();
}
bool Dog_T::ChargeName(){
  cout<< "Please input your dog`name:";
  cin>>cpName;
  cout<< endl;
  return true;
}
bool Dog_T::DisplayInfo(){
  cout<<"Dog`s name is "<< cpName<<"."<< endl;
  cout<<"Dog is "<< iAge<<" days old."<< endl;
  cout<< endl;
  return true;
}
bool Dog_T::PastOneDay(){
  iAge++;
  return true;
}

//main.cpp
#ifndef IOSTREAM
#define IOSTREAM
#include
#endif
#include "dog.h"
////////////////menu//////////////
int main(){
  int n;
  Dog_T myDog;
  do{
    do{
      cout<<"Menu:"<< endl;
      cout<<" 1.Display Dog`s info"<< endl;
      cout<<" 2.Past one day"<< endl;
      cout<<" 3.Charge Dog`name"<< endl;
      cout<<" 4.Exit"<< endl;
      cout<<"and your input is:";
      cin>>n;
      cout<< endl;
    }while(n<1 || n>4);
    switch(n){
      case 1:
        myDog.DisplayInfo();
        break;
      case 2:
        myDog.PastOneDay();
        break;
      case 3:
        myDog.ChargeName();
        break;
      }
    }while(n!=4);
//////////////////////////////////////////////
  cout<<"See you"<< endl;
  return 0;
}
-------------------------------------------------------------------------
this is the most simple class i think.
BUT,there is still has some problem that i don't know how to deal with. It is once i input the letter insteal of number or a long number. it will entry a dead loop... i hope someone could tell me why.
and I think there must be something wrong about my English grammar or spell. if i do, please tell me.
at last, if you have any suggest or other, please tell me also.

No comments:

Post a Comment