E
Erik Wikström
Hello
I've recently started to learn C++ but I'm not new to
programming.
While reading the book "Teach Yourself C++ for Linux in 21 days"
from Sams I came across the following:
"Private data is available to the member functions of a class,
and different instances of the class can access each other's
data. In other words, if Frisky and Boots are both instances of
Cat, Frisky's member functions can access Frisky's data and also
Boots's data."
Having learned OO using Java I thought that this was quite
strang, should you be able to access private data from other
objects just because they are of the same type?
So I decided to test using the following code:
--------test.h
class tester {
public:
tester(int);
int getT() const;
void setOT(tester, int);
int getOT(tester) const;
private:
int T;
};
--------test.cpp
#include <iostream>
#include "test.h"
tester::tester(int t) {
T = t;
}
int tester::getT() const {
return T;
}
void tester::setOT(tester t,int n) {
cout << "Setting t.T to " << n << "...";
t.T = n;
cout << "Done!\n";
}
int tester::getOT(tester t) const{
cout << "t.T: " << t.T << endl;
}
--------t.cpp
#include <iostream>
#include "test.h"
int main() {
tester t1(5), t2(10);
cout << "t1: " << t1.getT() << endl;;
cout << "t2: " << t2.getT() << endl;;
t2.getOT(t1); // Try getting value of private member of other
object
t2.setOT(t1,0); // Try changing value of rivate member of
other object
t2.getOT(t1); // Tyr getting again
cout << "t1: " << t1.getT() << endl;;
cout << "t2: " << t2.getT() << endl;;
}
-------Output
t1: 5
t2: 10
t.T: 5
Setting t.T to 0...Done!
t.T: 5
t1: 5
t2: 10
I'm using gcc 2.95.4 running FreeBSD 4.8.
Now, the most supprising thing wasn't that it compiled without
any problem, but the fact that I could get values of other
objects private members but not set them. Is it supposed to be
this way and it's my interpretation of the text that is wrong or
should I be able to set the values too?
As things are now they make more sence from a OO point of view.
I've recently started to learn C++ but I'm not new to
programming.
While reading the book "Teach Yourself C++ for Linux in 21 days"
from Sams I came across the following:
"Private data is available to the member functions of a class,
and different instances of the class can access each other's
data. In other words, if Frisky and Boots are both instances of
Cat, Frisky's member functions can access Frisky's data and also
Boots's data."
Having learned OO using Java I thought that this was quite
strang, should you be able to access private data from other
objects just because they are of the same type?
So I decided to test using the following code:
--------test.h
class tester {
public:
tester(int);
int getT() const;
void setOT(tester, int);
int getOT(tester) const;
private:
int T;
};
--------test.cpp
#include <iostream>
#include "test.h"
tester::tester(int t) {
T = t;
}
int tester::getT() const {
return T;
}
void tester::setOT(tester t,int n) {
cout << "Setting t.T to " << n << "...";
t.T = n;
cout << "Done!\n";
}
int tester::getOT(tester t) const{
cout << "t.T: " << t.T << endl;
}
--------t.cpp
#include <iostream>
#include "test.h"
int main() {
tester t1(5), t2(10);
cout << "t1: " << t1.getT() << endl;;
cout << "t2: " << t2.getT() << endl;;
t2.getOT(t1); // Try getting value of private member of other
object
t2.setOT(t1,0); // Try changing value of rivate member of
other object
t2.getOT(t1); // Tyr getting again
cout << "t1: " << t1.getT() << endl;;
cout << "t2: " << t2.getT() << endl;;
}
-------Output
t1: 5
t2: 10
t.T: 5
Setting t.T to 0...Done!
t.T: 5
t1: 5
t2: 10
I'm using gcc 2.95.4 running FreeBSD 4.8.
Now, the most supprising thing wasn't that it compiled without
any problem, but the fact that I could get values of other
objects private members but not set them. Is it supposed to be
this way and it's my interpretation of the text that is wrong or
should I be able to set the values too?
As things are now they make more sence from a OO point of view.