Guest

ryutenchi

By: a guest on Sep 7th, 2008  |  syntax: C++  |  size: 1.87 KB  |  hits: 59  |  expires: Never
download  |  raw  |  embed  |  report abuse
Copied
  1. /*
  2.  *      pointer.cpp
  3.  *      
  4.  *      Copyright 2008 Unknown <system@SMOOTHMACHINE>
  5.  *      
  6.  *      This program is free software; you can redistribute it and/or modify
  7.  *      it under the terms of the GNU General Public License as published by
  8.  *      the Free Software Foundation; either version 2 of the License, or
  9.  *      (at your option) any later version.
  10.  *      
  11.  *      This program is distributed in the hope that it will be useful,
  12.  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  *      GNU General Public License for more details.
  15.  *      
  16.  *      You should have received a copy of the GNU General Public License
  17.  *      along with this program; if not, write to the Free Software
  18.  *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  19.  *      MA 02110-1301, USA.
  20.  */
  21.  
  22.  
  23. #include <iostream>
  24.  
  25. using namespace std;
  26.  
  27. class people
  28. {
  29. public:
  30.         int getAge(){return age;};
  31.         void setAge(int a){age=a;};
  32. protected:     
  33.         int age;
  34. };
  35.  
  36. int main(int argc, char** argv)
  37. {
  38.         people* pPerson;
  39.         people me, you;
  40.        
  41.         pPerson = new people();
  42.         pPerson->setAge(12);
  43.         me.setAge(274);
  44.         you.setAge(43);
  45.        
  46.         cout<<"The pointer says: "<<pPerson->getAge()<<endl;
  47.        
  48.         delete pPerson;
  49.        
  50.         pPerson = &me;
  51.        
  52.         cout<<"The pointer says: "<<pPerson->getAge()<<endl;
  53.        
  54.         pPerson = &you;
  55.        
  56.         cout<<"The pointer says: "<<pPerson->getAge()<<endl;
  57.        
  58.         int* pAge;
  59.         int meAge, youAge;
  60.        
  61.         pAge = new int();
  62.         *pAge = 12;
  63.         meAge = 274;
  64.         youAge = 43;
  65.        
  66.         cout<<"The pointer says: "<<*pAge<<" It is stored at: "<<pAge<<endl;
  67.        
  68.         delete pAge;
  69.        
  70.         pAge = &meAge;
  71.        
  72.         (*pAge)++;
  73.        
  74.         cout<<"The pointer says: "<<*pAge<<" It is stored at: "<<pAge<<endl;
  75.        
  76.         pAge = &youAge;
  77.        
  78.         (*pAge)++;
  79.        
  80.         cout<<"The pointer says: "<<*pAge<<" It is stored at: "<<pAge<<endl;
  81.        
  82.         return 0;
  83. }