//dog.h
class Dog_T{
public:
  Dog_T();
  ~Dog_T();
  bool Cry();
  bool ChangeName();
  bool PassDay();
  bool Display();
private:
  char *name;
  int age;
};
//dog.cpp
........
//cat.h
class Cat_T{
public:
  Cat_T();
  ~Cat_T();
  bool Cry();
  bool ChangeName();
  bool PassDay();
  bool Display();
private:
  char *name;
  int age;
};
//cat.cpp
.......
//main.cpp
int main(){
  Cat_T *cat;
  Dog_T *dog;
  int n=1;
  int k=0;
  while(n!=6){
  while(!(k==1||k==2)){
    cout<<"1.cat 2.dog\nyour choice:";
    cin>>k;
    if(k==1)
      cat=new Cat_T();
    else if(k==2)
      dog=new Dog_T();
    }
    do{
      cout<< endl;
      cout<<"1.change name"<< endl;
      cout<<"2.pass a day"<< endl;
      cout<<"3.display"<< endl;
      cout<<"4.cry"<< endl;
      cout<<"5.change animal"<< endl;
      cout<<"6.exit"<< endl;
      cout<<"your input:";
      cin>>n;
      cout<< endl;
    }while(n<1 || n>6);
    switch(n){
    case 1:
      if(k==1)
        cat->ChangeName();
      if(k==2)
        dog->ChangeName();
      break;
    case 2:
      if(k==1)
        cat->PassDay();
      if(k==2)
        dog->PassDay();
        break;
    case 3:
      if(k==1)
        cat->Display();
      if(k==2)
        dog->Display();
      break;
    case 4:
      if(k==1)
        cat->Cry();
      if(k==2)
        dog->Cry();
      break;
    case 5:
      if(k==1)
        delete cat;
      if(k==2)
        delete dog;
      k=0;
      break;
    default:
      break;
    }
  }
  return 0;
}
--------------------------------------------------------------------------------
this a two classes. and they make a lot complexity. then, what about three, four or more classes?
this is a bad examle for using two similar classes. i wrote it down because i want to give a better one (i don't dare to say it's the best) on this weekend. i wish to make the next one feeling much better than this one.
i skiped the dog.cpp and cat.cpp because they're similar to the last time i have given. but, anyway you can easily guess what it should be if you want.
this is.see you
Saturday, October 11, 2008
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment