Jump to content
  • Advertisement
Sign in to follow this  
KenWill

Why isn't this working?

This topic is 1041 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts


#include <iostream>
#include "person.h"
#include "PP.h"
using namespace std;

int main()
{
    Person person;
    PP pp(person);
    cout << person.getAge();

}

Person.h

#ifndef PERSON_H
#define PERSON_H
#include <string>
using namespace std;
class Person
{
private:
    string cName;
    int cAge;
public:
    Person(): cName(""), cAge(0){}
    // GETTERS
    string getName(){return cName;}
    int getAge(){return cAge;}

    // SETTERS
    void setName(string name){cName = name;}
    void setAge(int age){cAge = age;}
};
#endif // PERSON_H

PP.h

#ifndef PP_H
#define PP_H
#include "person.h"
#include "nameRand.h"
class PP
{
private:
    Person cPerson;
    NameRand nameR;
public:
    PP(Person& person):cPerson(person){cPerson.setAge(nameR.getAge());}
    void run();


};
#endif // PP_H

nameRand.h

#ifndef NAMERAND_H
#define NAMERAND_H
#include <string>
using namespace std;
class NameRand
{
private:
    string rName;
    int age;
public:
    NameRand():rName("ytvt64f5"), age(1234){}

    // GETTERS
    string getName(){return rName;}
    int getAge(){return age;}
};
#endif // NAMERAND_H


I want the output to be "1234" but its actually "0". Why can't i change the member variables like this? I swear i tried the same thing and it worked as expected but now is not.

Share this post


Link to post
Share on other sites
Advertisement

"Person cPerson;" inside PP is a local variable, independent from "Person person;" in the main program.

Either make the former a reference, or query pp.cPerson in the main program (although you are not allowed to do that currently due to "private" access).

 

If you make it a reference, it's weird to have it private imho, as it's not private at all. To reduce the chance on surprises I'd recommend not to share private data with others, its the entire point of "private", imho

 

Edit: And if you have getters and setters like you have, they are useless and offer no protection at all. Why not make the variables public instead, and get rid of the getter/setter clutter?

Edited by Alberth

Share this post


Link to post
Share on other sites

Correct, however, I'd caution you regarding the use of member references. They can get really cantankerous if you start moving objects around. Dangling references are no fun at all. They're legal C++, but if you use them just be very careful and if you encounter a "weird" bug, suspect them immediately.

Share this post


Link to post
Share on other sites

"Person cPerson;" inside PP is a local variable, independent from "Person person;" in the main program.

Either make the former a reference, or query pp.cPerson in the main program (although you are not allowed to do that currently due to "private" access).

 

If you make it a reference, it's weird to have it private imho, as it's not private at all. To reduce the chance on surprises I'd recommend not to share private data with others, its the entire point of "private", imho

 

Edit: And if you have getters and setters like you have, they are useless and offer no protection at all. Why not make the variab

 

 

Oh, i didn't even know you can do that to variables other than function parameters...i'm sorry for all those dumb question. I have read about those stuff a while ago and i kinda forgot some things...i should start reading it again xD

Share this post


Link to post
Share on other sites
C++ is a very orthogonal language, in general anything can be done anywhere, except where it makes absolutely no sense at all.

Usually though, most things are not equally useful at every place. You often want to restrict yourself further than the boundaries of the language, or you're setting yourself up for lots of trouble. How much you want to restrict yourself though, is entirely up to you.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!