Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Scope of class variables

The short piece of code below works fine as long as I declare variables in classes to be "public:" But I thought the functions which use these variables, since they are declared within the class, should have access to these variables without a public declaration.

As I say, code runs fine with the two "public:" declarations, but once removed, the variables are no longer "seen" by the functions.

Any suggestions, explanation, etc.?

Thanks in advance.

#include <iostream.h>
char newline;

class poet
{
class poet_name
{
public: //if removed, variable no longer visible
char lastname[20];
}name;

class poet_books
{
public: //if removed, variables no longer visible
double books;
double career_years;
}books;

public:
void data_input(void);
void data_output(void);
};

void poet::data_input()
{
cout << "Enter poet's last name: ";
cin >> name.lastname;
cin.get(newline);

cout << "Enter number of books published: ";
cin >> books.books;
cout << "Enter number of years in writing career: ";
cin >> books.career_years;
cout << "\n\n";
}

void poet::data_output()
{
cout << name.lastname << endl;
cout << "Production rate: 1 book every "
<< books.books / books.career_years << "years" <<endl;
}

void main()
{
poet writer_corp;
writer_corp.data_input();
writer_corp.data_output();
return;
}
[1674 byte] By [amalafrida] at [2007-11-11 6:47:53]
# 1 Re: Scope of class variables
class variables default to private unless you have public on them. Private variables are visible to methods of the class they are declared in but not to other classes and functions. The friend and protected keywords can have some effects but are not used a lot these days.

That said, what line of code was not able to see a variable or method when you took public away?
jonnin at 2007-11-11 21:02:50 >
# 2 Re: Scope of class variables
Oh couple of style things:
Use
<iostream>
using namespace std;

instead of <iostream.h>

also
putting a variable on a class as your did (name) is not attractive. It becomes a burden for readers to find name -- the eye skips over the class defination and misses the variable hidden on the end of it. It is a leftover from C structs and is not attractive in C++. (I won't use it in C either).

newline is a global, and while globals have a place, there is no compelling reason for one here.

void main is not legal in C++ --however most compilers will tolerate it. Main should be int returning zero for correct program termination and other numbers for error states (programmer defined).
jonnin at 2007-11-11 21:03:51 >
# 3 Re: Scope of class variables
Wow! thanks for all the advice.

There are two lines of code with comments. Each line says "public:" Program runs fine with those lines in; but take them out and program will not run because the class member variables are not 'in scope' for the calling functions. My question was why? The functions are declared within the class definitions . . . it seems like the variables should be visible to the "public" functions . . . perhaps it has something to do with the 'nested classes'.

And yes, I'm sure I am picking up all sorts of very bad programming habits; I'm working out of books written 10 years ago . . . hard to come up with good C++ instructional material in English . . . living here in France.

Once again, thanks for your advice.
amalafrida at 2007-11-11 21:04:55 >
# 4 Re: Scope of class variables
It has nothing to do with the functions, you have member functions of one class trying to access a private data member of *another* class directly:

void poet::data_input()
{
//..
cin >> name.lastname;

name isn't a poet object, it's a different object, so you're not allowed to access it directly. Instead, you need to define a public accessor function in poet_name:

string poet_name::get_name() {return string(last_name); }

Btw, the nested classes are a bad idea in this case. They really add nothing to the clarity/functionaly of the design. Instead, ordinary setter and getter member functions can do the job.

Finally, you really want to toss those 10 years old C++ books...C++ has changed so much in the last decade that these books not only teach outdated material, they actually cause damage. There are plenty of up to date C++ books, even in French, and some of them are available online, for free. You can also find tons of useful material here on dev-archive.com.
Danny at 2007-11-11 21:05:56 >
# 5 Re: Scope of class variables
Thanks again. I'll look about for some more recent material.
amalafrida at 2007-11-11 21:07:00 >