God is love
About this Entry
Posted by: darkdee

Visit darkdee's Xanga Site

Original: 3/12/2008 10:56 PM
Views: 12
Comments: 0
eProps: 0

Read Comments
Post a Comment
Back to Your Xanga Site



Wednesday, March 12, 2008

Inheritance and Polymorphism

 

#include <iostream>
#include <fstream>
using namespace std;

const int MAX_MEMERY=100;

class Mood{
public:
   virtual ~Mood();
   virtual void setHappyness(int h)=0;
   void setReason(int t);
   void displayReason();
   int getHaveReason() const;
protected:
   Mood();
private:
   void setReason();
   string *reason;
   int have_reason;
   int length_of_memory;
};

class Happiness: public Mood{
public:
   Happiness();
   virtual ~happiness()
   virtual void setHappyness(int h);
private:
   int is_happiness;
};

Mood::~Mood(){
   if(reason!=NULL){
      delete [] reason;
   }
}

Mood::Mood():reason(NULL),have_reason(0),length_of_memory(0){
}

void Mood::setReason(int t){
   have_reason=t;
   if(have_reason){
      setReason();
   }
}

void Mood::setReason(){
   ifsteam fin;
   fin.open("today_memory.txt");
   reason = new string[MAX_MEMERY];
   fin>>string[0];
   length_of_memory=1;
   while(!fin.eof()){
      fin>>reason[length_of_memory++];
   }
   fin.close();
}

int Mood::getHaveReason() const{
   return have_reson;
}

void Mood::displayReason(){
   for(int i=0; i<length_of_memory; i++){
      cout << reason[i] << endl;
   }
}

Happiness::Happiness():mood(),is_happiness(0){
}

Happiness::~Happiness(){
}

void Happiness::setHappiness(int h){
   is_happiness=h;
}

int main(){
   Mood *today_mood;
   today_mood = new Happiness();
   today_mood->setReason(1);

   if(today_mood->getHaveReason()){
      today_mood->displayReason();
   }
   else
      cout << "There is nothing happen today";

   delete today_mood;

   return EXIT_SUCCESS;
}

// try to read it

 Posted 3/12/2008 10:56 PM - 12 Views - 0 eProps - 0 comments

Give eProps or Post a Comment

Choose Identity
(?)
 
Give eProps (?)
Post a Comment
Add Link | Preview HTML comment help 
  • Say it with Minis! (?)

Profile Pic:
Default  |  Choose »  (?)



Back to darkdee's Xanga Site!
Note: your comment will appear in darkdee's local time zone:
GMT +08:00 (China Coast)