im new in the C++ world and coding in general, just started studying getters and setters and made areally simple exercise to practice them, the build seems to have 0 errors and 2 minor warnings, however, when I try to use a function to return a private variable and print it, it simply crashes the program, however, if i use the last function I made "getAccount()" It seems to work just fine.
After some poking, it seems like the problem is with the getter functions, just calling them crashes the program, here's the code:
main.cpp
#include <iostream>
#include "Person.h"
using namespace std;
int main()
{
User user;
user.setUser("someuser");
user.setPassw("somepassword");
cout << user.getPassw() << endl;
cout << user.getUser() << endl;
user.getAccount();
}
Person.h
#define PERSON_H
#include <iostream>
using namespace std;
class User
{
private:
string username;
string password;
public:
string setUser(string usernm);
string setPassw(string pass);
string getUser();
string getPassw();
void getAccount();
};
#endif // PERSON_H
Person.cpp
#include <iostream>
#include "Person.h"
using namespace std;
string User::setUser(string usernm){
usernm = username;
}
string User::setPassw(string pass){
pass = password;
}
string User::getUser(){
return username;
}
string User::getPassw(){
return password;
}
void User::getAccount(){
cout << "Account is:" << endl;
cout << "Username: " + username << endl;
cout << "Password: " + password << endl;
}