Chinaunix首页 | 论坛 | 博客
  • 博客访问: 130038
  • 博文数量: 30
  • 博客积分: 972
  • 博客等级: 中士
  • 技术积分: 332
  • 用 户 组: 普通用户
  • 注册时间: 2012-02-08 10:04
文章分类

全部博文(30)

文章存档

2012年(30)

分类: C/C++

2012-07-26 10:08:02


点击(此处)折叠或打开

  1. /******************mystr.h***********/
  2. #include<string.h>
  3. #include<iostream>

  4. //using namespace std;
  5. using std::cin;
  6. using std::cout;
  7. using std::endl;

  8. class Mystring{

  9.     char *str;

  10. public:
  11.     Mystring(char *string){
  12.         str=new char[strlen(string)+1];
  13.         strcpy(str,string);
  14.     }
  15.     Mystring(const Mystring &p){
  16.             
  17.         str=new char[strlen(p.str)+1];
  18.         strcpy(str,p.str);
  19.     }
  20.     ~Mystring(){ delete str;}

  21.     Mystring& operator = (const Mystring &str3){
  22.         delete this->str;
  23.         this->str = new char[strlen(str3.str)+1];
  24.         strcpy(this->str,str3.str);
  25.         return *this;
  26.     }
  27.     friend char* operator + (Mystring&,Mystring&);

  28.     char index(int i);
  29.     int length(void);
  30.     bool replace(int i,char ch);
  31.     bool remove(int i);
  32.     char* display();
  33. };

  34. char Mystring::index(int i)
  35. {
  36.     return *(str+i-1);
  37. }

  38. int Mystring::length(void)
  39. {
  40.     int i=0;
  41.     while(*(str+i) != '\0')
  42.         i++;
  43.     return i;
  44. }

  45. bool Mystring::replace(int i,char ch)
  46. {
  47.     *(str+i-1) = ch;
  48.     return true;
  49. }

  50. bool Mystring::remove(int i)
  51. {
  52.     memset(str+i,0,strlen(str)-i);
  53.     return true;
  54. }

  55. char* Mystring::display()
  56. {
  57.     return this->str;
  58. }

  59. char* operator +(Mystring& str1,Mystring& str2)
  60. {
  61.     char *p;
  62.     p = new char[strlen(str1.str)+strlen(str2.str)+2];
  63.     strcpy(p,str1.str);
  64.     strcat(p,str2.str);
  65.     
  66.     return p;
  67. }


  68. /**************animal.h*************/
  69. #include "mystr.h"

  70. class animal{

  71.     Mystring name;

  72. public:
  73.     animal(char *ch):name(ch)
  74.     {
  75.         //cout<<"animal构造"<<endl;
  76.     }
  77.     ~animal() {}
  78.     virtual void sleep();
  79.     virtual void eat();
  80.     virtual void play();
  81.     char* display();
  82. };

  83. void animal::sleep()
  84. {
  85.     cout<<this->name.display()<<"is sleeping...."<<endl;
  86. }

  87. void animal::eat()
  88. {
  89.     cout<<this->name.display()<<"is eating...."<<endl;
  90. }

  91. void animal::play()
  92. {
  93.     cout<<this->name.display()<<"is playing...."<<endl;
  94. }

  95. char* animal::display()
  96. {
  97.     return name.display();
  98. }

  99. /**************cat.h*************/
  100. #include "animal.h"

  101. class cat:public animal{

  102. public:
  103.     cat(char *name_cat):animal(name_cat)
  104.     {
  105.         cout<<"Hello,I am "<<cat::display()<<endl;
  106.     }
  107.     ~cat() {}
  108.     void sleep();
  109.     void eat();
  110.     void play();
  111. };

  112. void cat::sleep()
  113. {
  114.     cout<<"cat "<<cat::display()<<" is sleeping..."<<endl;
  115. }

  116. void cat::eat()
  117. {
  118.     cout<<"cat "<<cat::display()<<" is eating..."<<endl;
  119. }

  120. void cat::play()
  121. {
  122.     cout<<"cat "<<cat::display()<<" is playing..."<<endl;
  123. }


  124. class dog:public animal{

  125. public:
  126.     dog(char *name_dog):animal(name_dog)
  127.     {
  128.         cout<<"Hello,I am "<<dog::display()<<endl;
  129.     }
  130.     ~dog() {}
  131.     void sleep();
  132.     void eat();
  133.     void play();
  134. };

  135. void dog::sleep()
  136. {
  137.     cout<<"dog "<<dog::display()<<" is sleeping..."<<endl;
  138. }

  139. void dog::eat()
  140. {
  141.     cout<<"dog "<<dog::display()<<" is eating..."<<endl;
  142. }

  143. void dog::play()
  144. {
  145.     cout<<"dog "<<dog::display()<<" is playing..."<<endl;
  146. }

  147. /*************human.h****************/
  148. #include "cat.h"

  149. class human:public animal{

  150.     int age;

  151. public:
  152.     human(char *name_human,int age):animal(name_human)
  153.     {
  154.         this->age = age;
  155.         cout<<"Hello,I am "<<human::display()<<endl;
  156.     }
  157.     ~human() {}

  158.     void play_with(animal&);

  159. };

  160. void human::play_with(animal &p)
  161. {
  162.     cout<<human::display()<<" is play with "<<p.display()<<endl;
  163.     p.play();
  164. }


点击(此处)折叠或打开

  1. #include "human.h"

  2. int main()
  3. {
  4.     cat cat1("congcong");
  5.     dog dog1("huanhuan");

  6.     human boy("hanhan",25);
  7.     boy.play_with(cat1);
  8.     boy.play_with(dog1);

  9.     return 0;
  10. }

阅读(2100) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~