|
-
March 26th, 2008, 06:41 AM
#16
Re: pointers....
 Originally Posted by armen_shlang
Ok I'm sorry but the answers didn't digest quite well. Even though I'm sure an average person would've got it by now, but I didn't.
Code:
string& getCompanyName() {return companyName;};
string& getTitle() {return title;};
What practical purpose would you have of returning a reference to a string? Just return the string.
Code:
string getCompanyName() {return companyName;};
string getTitle() {return title;};
What if I want to get the company name, and in some other non-related function, write that name to a file, long after the object is gone? You can't do that with the code you posted, since that reference is tied to the object.
By returning a string, that string can last beyond the life the object.
Regards,
Paul McKenzie
-
March 26th, 2008, 07:20 AM
#17
Re: pointers....
skanthaverl, I understand what you mean and why to make the firstjob a reference and the secondjob a pointer. However, I'm having problems trying to code it....I'm a kinda newbie tackling really hard. This is what i got so far, and it's completly wrong, how can i fix this according to what you said...
Code:
class Job
{
public:
Job(string compName, string titl, double sal)
: companyName(compName), title(titl), salary(sal){};
Job(){};
string& getCompanyName() {return companyName;};
string& getTitle() {return title;};
double getSalary() {return salary;};
void setCompanyName(string name) {companyName = name;};
void setTitleName(string name) {title = name;};
void setSalery(double name) {salary = name;};
private:
string companyName;
string title;
double salary;
};
//*********************************************************8
class person
{
public:
person(string name, string last, string compName, string title, double sal)
: m_name(name), m_last(last), m_firstJob(compName,title,sal)
{};
int getAge(){return m_age;};
string getName(){return m_name;};
string getLast(){return m_last;};
void setAge(int age) {m_age = age;};
Job& get1stJob(){ return m_firstJob;};
//------------------------------------
Job* get2ndJob(){ return &m_secondJob;};
private:
string m_name;
string m_last;
int m_age;
Job& m_firstJob;
Job* m_secondJob;
};
//*********************************************************
void main()
{
person RafaelB("Rafael","BakerMan", "Starbucks", "maneger", 8.50);
RafaelB.setAge(22);
cout << RafaelB.getName() << " --" << RafaelB.getLast() << " --" << RafaelB.getAge() << endl;
cout << RafaelB.get1stJob().getCompanyName() << " --" <<
RafaelB.get1stJob().getTitle() << " --" << RafaelB.get1stJob().getSalary() << endl;
}
Last edited by armen_shlang; March 26th, 2008 at 07:38 AM.
-
March 26th, 2008, 08:06 AM
#18
Re: pointers....
to get your code compiled, you need to fix the following things..
1. include the follwoing in the top
Code:
#include <string>
#include <iostream>
using namespace std;
2. in your get2ndJob() you need to have
Code:
Job* get2ndJob(){ return m_secondJob;};
not return &m_secondJob; because, m_secondJob itself is a Job* type, and your get2ndJob() expect a Job* as it's return type. so simply return m_secondJob. if you say &m_secondJob, you are returning a pointer to pointer to Job, ie Job** type.
3. In your initialisation list, to init m_firstJob, you need to pass an instance/reference of a Job object; not the Job's constructor parameters.
it will be like this
Code:
person(string name, string last, string compName, string title, double sal, Job& job): m_name(name), m_last(last), m_firstJob(job){};
note that I've changed the signature of the constructor person(.......). do necessary changes in the main() as well.
now your code should be OK.
-
March 26th, 2008, 08:15 AM
#19
Re: pointers....
ASDKJGASDJKASDASD!!!!111
main returns an int, not anything else.
-
March 26th, 2008, 08:23 AM
#20
Re: pointers....
Thank you,
I didn't know How to have the m_firstJob() to take only one argument also I didn't know about the constructor and how to write the member initialization list.
One last thing...
How do declare it in the main function...what should i have in the last argument.
Code:
person RafaelB("Rafael","BakerMan", "Starbucks", "maneger", 8.50, ???);
also how can I initialize the m_secondJob inside the main.
Thank you very much again.
i stayed up all night for this concept.
Last edited by armen_shlang; March 26th, 2008 at 08:42 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|