Aliaks, there are a number of problems you will have with this class. The biggest is that you are storing refenences to strings. This is a problem becuase you are passing in a temporary copy to name in your constructor. When the constructor completes, the copy is destroyed and the reference is invalid. The class members should be strings, not references to strings. You should pass in references in your constructor so a temporary copy does not have to be made during the call of the constructor. Even so, you should store copies of strings, not references.
If userName and userAge are declared const, it means they will not change, so there is no point in having a setName or a setAge method.
You should initialize age in the constructor.
You don't need a destructor because the class is not allocating any resources that are not released automatically.
Try this:
In classes.h
Code:
#include <string>
using namespace std;
class User
{
public:
User (const string name&, const string& age);
string GetName();
string GetAge();
void setName(const string& userName);
void setAge(const string& userAge);
private:
string userName;
string userAge;
};
in classes.cpp
Code:
#include <iostream>
#include "classes.h"
User::User(const string& name, const string& age) : userName(name), userAge(age) { }
string User::GetName() { return userName; }
string User::GetAge() { return userAge;}
void User::setName(const string& name) { userName = name; }
void User::setAge(const string& age) { userAge = age; }
int main(void)
{
string name = "MyName";
string age = "99";
User u(name, age);
cout << "name is " << u.GetName() << endl;
cout << "age is " << u.GetAge() << endl;
}
Does that help you?