Question about class

This is a discussion on Question about class within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> using namespace std; class person { private: string name; int age; public: person(const string &getname): name(getname), age(0){ ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    4

    Question about class

    Code:
    #include <iostream>
    
    using namespace std;
    
    class person
    {
       private:
       string name;
       int age;
    
       public:
       person(const string &getname):
                  name(getname), age(0){ }
    
       void addAge(const string &getname)
       {
          age++;
       }
    
       void getAge(void)
       {
          cout << endl;
          cout << name << " now " << age << " years old";
       }
    
    };
    
    
    int main()
    {
       person *p;
       p = new person("John");
       p->addAge("John"); 
       p->addAge("John");
       p->addAge("John");
       p->getAge(); //3
    
       p = new person("Connor");
       p->addAge("Connor");
       p->getAge(); //1
    
       *p = person("John");
       p->addAge("John");
       p->getAge();      //John age start at 1 again????
    
       cout << endl;
    
       return 0;
    }
    How do I increase John age, after I insert new person???
    can anyone help me please.
    Thank You

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What do you mean? When you overwrite Connor with another person called John, the age of the John person is zero. What else did you expect?

    Just because you are not doing "new", doesn't mean that the object isn't being created from scratch. The operator new simply gives you some memory to store the object. Regardless of how the memory for the object is created, the constructor is called, which sets age to zero.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    4
    Quote Originally Posted by matsp View Post
    What do you mean? When you overwrite Connor with another person called John, the age of the John person is zero. What else did you expect?

    Just because you are not doing "new", doesn't mean that the object isn't being created from scratch. The operator new simply gives you some memory to store the object. Regardless of how the memory for the object is created, the constructor is called, which sets age to zero.

    --
    Mats
    Hi, thanks for the reply.
    So are they any way to go back to my John class to increase his age??

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    C++ != Java

    When you allocate something with the new keyword, you need to delete it.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    The person object represents one person. For this reason it is unclear, why you need the name argument to increment the age of a person (the argument is unused in the method). If you want more persons, you'd create more instances (and put them in a container or array, for example).

    Besides, when you make p point to another instance allocated with new, you are leaking the object that p pointed to earlier. It still exists in memory but there are no means to get at it.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 02:03 AM
  2. Replies: 8
    Last Post: 10-02-2005, 01:27 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  4. question about DLL's and class functions
    By btq in forum Windows Programming
    Replies: 2
    Last Post: 02-25-2003, 06:08 AM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 07:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21