class Person
{
public:
Person(void);
~Person(void);
string name;
int age;
string getName()
{
return _name;
}
int getAge()
{
if(_age > 0)
{
return _age;
}
}
string setName()
{
return name;
}
int setAge()
{
return age;
}
Person::Person(int age, string name)
{
_age = age;
_name = name;
}
void write()
{
cout << "This is the age: " << _age << endl;
cout << "This is the name: " << _name << endl;
}
private:
string _name;
int _age;
};
and my main contains;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
int main(int argc, char **argv) {
Person(5,"Max");
void write();
int keypress; cin >> keypress;
}
Sorry for long post first of all, but from here you can see i have a write function in my person header file, and i attempt to call it in main. However my program does not do anything...I have been able to test that the instance i placed into main work (Person(5,"Max");) as i put the cout data into my construct and it printed out correctly.
Can anyone see anything blatently wrong i have done as to why i cannot call my write function?
You are not actually initializing the data members in the default constructor. You are just declaring some local variables that you give values but never use.
This code should not compile because on line line 36 you have Person:: which should not be there.
in main, line 4 creates a temporary Person that is never used. What you want is probably to create a named object. Person p(5,"Max");
Line 6 is a function declaration. To call the write() function on the Person object: p.write();