Jump to content

Objects and References

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1
BlaineSch

BlaineSch

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,448 posts
So, as you all know OOP is a very big thing in programming today.

I am currently taking a C++ class which is neat since I have never taken a C/C++ class before so most of the things are fairly new.

Yesterday, we were talking about Objects. He told me if you assign one object to another you get an "exact copy" which is exactly the opposite of what my Java teacher told me saying "all objects are references and by assigning one to the other the value is a reference so you get one object with two pointers". I quickly woke up when he said that and I asked him about it he said it was a copy not just a pointer unless you specifically assign it as a pointer putting the ampersand infront of the variable name.

So I don't have Java Eclipse installed after the reformat but I have access to PHP and C++ so I tested them both.

PHP Code:
<?PHP
class dataCloset {
	private $data = array();
	public function __set($name, $value) {
		$this->data[$name] = $value;
	}
	public function __get($name) {
		return $this->data[$name];
	}
	public function showall() {
		print_r($this->data);
	}
}
$one = new dataCloset();
$two = new dataCloset();
//$three not defined
$one->b = "Hello World";
$two = $three = $one;
$one->showall();
$two->showall();
$three->showall();
$two->a = "I Rule the world";
$three->c = "I iz hungry";
$one->showall();
$two->showall();
$three->showall();
?>
Output:
Output:
	Array
	(
		[b] => Hello World
	)
	Array
	(
		[b] => Hello World
	)
	Array
	(
		[b] => Hello World
	)
	Array
	(
		[b] => Hello World
		[a] => I Rule the world
		[c] => I iz hungry
	)
	Array
	(
		[b] => Hello World
		[a] => I Rule the world
		[c] => I iz hungry
	)
	Array
	(
		[b] => Hello World
		[a] => I Rule the world
		[c] => I iz hungry
	)

C++ Code:
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
class dataCloset {
	private:
		int temp;
		int data[5];
		int at;
	public:
		dataCloset() {
			this->at = 0;
			for(temp = 0;temp<5;temp++) {
				data[temp] = -1;
			}
		}
		void set(int value) {
			data[this->at] = value;
			at++;
		}
		int operator=(int name) {
			data[at] = name;
			at++;
			return name;
		}
		void showall() {
			for(temp = 0;temp<5;temp++) {
				cout << temp << " => " << data[temp] << endl;
			}
		}
};
int main(int argc, char *argv[]) {
	int temp = 0;
	dataCloset one;
	one.set(19);
	one.set(21);
	one.set(20);
	dataCloset &three = one, &two = one;
	one.showall();
	two.showall();
	three.showall();
	three.set(34);
	cout << "\n----------\n" << endl;
	one.showall();
	two.showall();
	three.showall();
    cin >> temp;
    return 1;
}
Output:
0 => 19
1 => 21
2 => 20
3 => -1
4 => -1
0 => 19
1 => 21
2 => 20
3 => -1
4 => -1
0 => 19
1 => 21
2 => 20
3 => -1
4 => -1
----------
0 => 19
1 => 21
2 => 20
3 => 34
4 => -1
0 => 19
1 => 21
2 => 20
3 => 34
4 => -1
0 => 19
1 => 21
2 => 20
3 => 34
4 => -1

Which I thought was weird, I never realized this would be "language dependent" and it is? What's up with this?

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
What's up is that C++ and Java take very different approaches to class variables. Every Java class variable is the equivalent of a C++ pointer. This is a MAJOR distinction. It also means that in Java, class variables are very different from primitives (such as int, float, etc).

C++ was designed with the goal of having class objects not be different from primitives. Bjarne Stroustrup did not want to have two different types of variables (primitive vs object). This has some definite advantages, such as allowing you to instantiate objects on the stack or on the heap, having objects clean up when they go out of scope, etc.

I came at these two languages in the reverse order, and kept finding it annoying that I couldn't do things in Java that I could with C++. I've yet to find anything Java can do that C++ doesn't.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog