hqt

Simple OOP

hqt
Aug 14th, 2012
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.21 KB | None | 0 0
  1. #include<string>
  2. #include<iostream>
  3.  
  4. using namespace std;
  5.  
  6. class People{
  7.  
  8.     // properties || attributes
  9.     int tuoi;
  10.     string name;
  11.  
  12. public:
  13.  
  14.     People(){  // constructor
  15.         tuoi = 0;
  16.         name = "unknown";
  17.     }
  18.  
  19.     People(int _tuoi, string _name){ // overloading constructor
  20.         tuoi = _tuoi;
  21.         name = _name;
  22.     }
  23.  
  24.     ~People(){  // destructor
  25.         cout<<"you're die"<<endl;
  26.     }
  27.  
  28.     // getter
  29.     int getYear(){ return tuoi; }
  30.     string getName(){ return name; }
  31.  
  32.     // setter
  33.     void setYear(int _tuoi){
  34.         tuoi=_tuoi;    
  35.     }
  36.     void setName(string _name){
  37.         name = _name;
  38.     }
  39.    
  40.  
  41.    
  42. private:
  43.     void eating(){
  44.         cout<<"I'm eating"<<endl;
  45.     }
  46.  
  47.     void eating(string food){  // overloading method
  48.         cout<<"I'm eating "<<food<<endl;
  49.     }
  50.  
  51.     void sleeping(){
  52.         cout<<"I'm sleeping"<<endl;
  53.     }
  54.  
  55. public:
  56.  
  57.     void daily(){
  58.         eating();
  59.         sleeping();
  60.     }
  61.  
  62. };
  63.  
  64.  
  65. int main(int argc, char** argv)
  66. {
  67.     //People student(20,"huynh quang thao"); //object
  68.     //cout<<student.name<<endl;
  69.  
  70.     // create object on heap
  71.     People* pupil = new People(20, "pham thi thu hoa");
  72.     cout<<pupil->getName()<<endl;
  73.     pupil->setName("huynh quang thao");
  74.     cout<<pupil->getName()<<endl;
  75.     cout<<"delete object"<<endl;
  76.     delete(pupil);
  77.    
  78.     return 0;
  79. }
Advertisement
Add Comment
Please, Sign In to add comment