Thread: Method Help

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    46

    Method Help

    I'm trying to get back into the groove of writing C++. I have written a class called person. It contains two accessor methods for each of my attributes, a constructor and destructor, and method that shows my info. I want to make a method that enables the person to "grow older"(each time the method is called, the person should age by one year basically)...just something simple.. I can't find the right way to create a growOlder() Method.... that when called makes my person age one year. Any help would be cool..and yeah I know my code is sloppy I'm out of practice!...yikes!!!


    Code:
    #include<iostream>
    #include<string>
    using namespace std;
    
    class person {
    private:
      string fname;
      string lname;
      int age;
      char sex;
    public:
      void setfName(string _fname);
      void setlName(string _lname);
      void setAge(int _age);
      void setSex(char _sex);
      string getfName();
      string getlName();
      int getAge();
      char getSex();
      person(string, string, int, char);
      ~person();
      void showInfo();
        };
    
    person::person(string _fname, string _lname, int _age, char _sex)
    {
      fname = _fname;
      lname= _lname;
      age = _age;
      sex = _sex;
    }
    person::~person(){
    }
    void person::setfName(string _fname)
    {
      fname = _fname;
    }
    string person::getfName()
    {
      return fname;
    }
    void person::setlName(string _lname)
    {
      lname = _lname;
    }
    string person::getlName()
    {
      return lname;
    }
    void person::setAge(int _age)
    {
      age = _age;
    }
    int person::getAge()
    {
      return age;
    }
    void person::setSex(char _sex)
    {
      sex = _sex;
    }
    char person::getSex()
    {
      return sex;
    }
    void person::showInfo()
    {
    cout<<"First Name: "<<fname<<endl<<"Last Name: "<<lname<<endl<<"Age: "<<age<<endl<<"Sex : "<<sex<<endl;
    }
    
    int main(){
    
      person John("John","Cavender",25,'M');
      John.showInfo();
    
      return 0;
    }

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Increase the class member age by one each time the function is called.
    That should do the trick!
    Last edited by Elysia; 10-23-2007 at 01:25 AM.

  3. #3
    Registered User mikeman118's Avatar
    Join Date
    Aug 2007
    Posts
    183
    Code:
    void person::getOlder()
    {
       age += 1;
    }

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by mikeman118 View Post
    Code:
    void person::getOlder()
    {
       age += 1;
    }
    age++ or ++age is more clear, I'd say.

    --
    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.

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    46

    calling it right?

    Thanks Guys! I inserted it into my code but it doesn't seem to do anything. How do I call it so it makes the person grow older...


    Code:
    char person::getSex()
    {
      return sex;
    }
    void person::showInfo()
    {
      cout<<"First Name: "<<fname<<endl<<"Last Name: "<<lname<<endl<<"Age: "<<age<<\
    endl<<"Sex : "<<sex<<endl;
    }
    void person::getOlder()  //added your line in
    {
      age++;
      }
    
    int main(){
    
      person John("John","Cavender",25,'M');
      John.showInfo();
      John.getOlder(); //calling it right???

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    How do you know it doesn't do anything? Call showInfo again after the getOlder call and see if it has changed.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, that will make John a year older [or whatever unit "age" is].

    --
    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.

  8. #8
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Your "get" methods should be declared const. Also, prefer using initializer lists in the construction of the class.
    Code:
    class person {
    private:
      string fname;
      string lname;
      int age;
      char sex;
    public:
      void setfName(string _fname);
      void setlName(string _lname);
      void setAge(int _age);
      void setSex(char _sex);
      string getfName() const;
      string getlName() const;
      int getAge() const;
      char getSex() const;
      person(string, string, int, char);
      ~person();
      void showInfo();
      void getOlder();
        };
    
    person::person(string _fname, string _lname, int _age, char _sex) : fname(_fname), lname(_lname),
                                                                        age(_age), sex(_sex)
    {
      //fname = _fname;
      //lname= _lname;
      //age = _age;
      //sex = _sex;
    }
    person::~person(){
    }
    void person::setfName(string _fname)
    {
      fname = _fname;
    }
    string person::getfName() const
    {
      return fname;
    }
    void person::setlName(string _lname)
    {
      lname = _lname;
    }
    string person::getlName() const
    {
      return lname;
    }
    void person::setAge(int _age)
    {
      age = _age;
    }
    int person::getAge() const
    {
      return age;
    }
    void person::setSex(char _sex)
    {
      sex = _sex;
    }
    char person::getSex() const
    {
      return sex;
    }
    void person::showInfo()
    {
    cout<<"First Name: "<<fname<<endl<<"Last Name: "<<lname<<endl<<"Age: "<<age<<endl<<"Sex : "<<sex<<endl;
    }
    void person::getOlder()  //added your line in
    {
      age++;
    }
    void person::setSex(char _sex)? You expecting a need for the person to spontaneously change sexes after construction? (I guess maybe sex change operation or one of those species [Clown Fish/Groupers?] that can change sex on the fly?)
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
      person(string, string, int, char);
    And put the names of the parameters, as that will help when you want to know what they are... Having to find the implementation in the code would be more of a hazzle.

    --
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. on method pointers and inheritance
    By BrownB in forum C++ Programming
    Replies: 2
    Last Post: 03-02-2009, 07:50 PM
  2. Best communication method to thousand childs?
    By Ironic in forum C Programming
    Replies: 8
    Last Post: 11-08-2008, 12:30 AM
  3. C# method
    By siten0308 in forum C# Programming
    Replies: 6
    Last Post: 07-15-2008, 07:01 AM
  4. Overriding a method in C
    By DavidDobson in forum C Programming
    Replies: 1
    Last Post: 07-05-2008, 07:51 AM
  5. Replies: 2
    Last Post: 01-22-2008, 04:22 PM