-
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;
}
-
Increase the class member age by one each time the function is called.
That should do the trick!
-
Code:
void person::getOlder()
{
age += 1;
}
-
Quote:
Originally Posted by
mikeman118
Code:
void person::getOlder()
{
age += 1;
}
age++ or ++age is more clear, I'd say.
--
Mats
-
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???
-
How do you know it doesn't do anything? Call showInfo again after the getOlder call and see if it has changed.
-
Yes, that will make John a year older [or whatever unit "age" is].
--
Mats
-
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?) :D
-
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