C++ unnecessary repeated definitions?
Dec 5, 2015 at 11:12pm UTC
Good afternoon!
Sorry if the question seems rather vague, but here's some (incomplete) code for some context. Specifically, this is about the "UserInfo inputInfo" definition part as seen in the functions UserInfo::setUserInfo() and UserInfo::displayProfile() in the implementation file.
project02.cpp (implementation file)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
#include <iostream>
#include "project02.h"
using namespace std;
void UserInfo::setUserInfo()
{
UserInfo inputInfo;
string fName;
string lName;
int bYear;
string city;
string occupation;
cout << "Please enter your first name: " ;
cin >> fName;
inputInfo.setFirstName(fName);
cout << "Please enter your last name: " ;
cin >> lName;
inputInfo.setLastName(lName);
cout << "You are now registered as: " << inputInfo.getFirstName() << " " << inputInfo.getLastName();
}
void UserInfo::displayProfile()
{
UserInfo inputInfo;
cout << "Profile Information:" << endl;
cout << "Name: " << inputInfo.getFirstName() << " " << inputInfo.getLastName();
}
void UserInfo::setFirstName(string fName)
{
_firstName = fName;
}
string UserInfo::getFirstName()
{
return _firstName;
}
void UserInfo::setLastName(string lName)
{
_lastName = lName;
}
string UserInfo::getLastName()
{
return _lastName;
}
project02.h (header file)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
#ifndef PROJECT02_H
#define PROJECT02_H
using namespace std;
class UserInfo
{
public :
string getFirstName();
void setFirstName(string first);
string getLastName();
void setLastName(string last);
int getBirthYear();
void setBirthYear(int year);
string getCurrentCity();
void setCurrentCity(string city);
string getOccupation();
void setOccupation(string occ);
void setUserInfo();
void displayProfile();
private :
string _firstName;
string _lastName;
int _birthYear;
string _currentCity;
string _occupation;
};
#endif // PROJECT02_H
project02main.cpp (main file)
1 2 3 4 5 6 7 8 9 10 11
#include <iostream>
#include "project02.h"
using namespace std;
int main()
{
UserInfo inputInfo;
inputInfo.setUserInfo();
return 0;
}
Now the question is: is there an alternative to repeatedly defining the object "UserInfo inputInfo;" each time for a different function in the implementation file?
Dec 5, 2015 at 11:39pm UTC
Simply remove any reference to userinfo inputinfo- you're using classes so the instance will hold the variables.
For Instance:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
void UserInfo::setUserInfo()
{
string fName;
string lName;
int bYear;
string city;
string occupation;
cout << "Please enter your first name: " ;
cin >> fName;
setFirstName(fName);
cout << "Please enter your last name: " ;
cin >> lName;
setLastName(lName);
cout << "You are now registered as: " << getFirstName() << " " << getLastName();
}
Dec 5, 2015 at 11:43pm UTC
Oh that works, thanks! No idea why my professor's example included something like that.
Topic archived. No new replies allowed.