Advertisement

Why isn't this working?

Started by November 15, 2015 02:54 PM
4 comments, last by Alberth 9 years, 1 month ago


#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.

"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?

Advertisement

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.

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

Good point, better restrict the usage of references to parameters of functions and methods

"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

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.

This topic is closed to new replies.

Advertisement