OOP language Object concept!

Questions about programming languages and debugging
Post Reply
User avatar
Hiram
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 153
Joined: 09 May 2009, 16:00
14

OOP language Object concept!

Post by Hiram »

Hey guys!
The previous couple of days I have been going through Java programming
seeking to understand well the Object concept of an Object Oriented Programming
(OOP) language.

Code: Select all

Vehicle car;
car = new Vehicle ();
In the first statement, a variable “car” of class Vehicle is declared, but this doesn’t
create an object. The second, which is an assignment statement, creates
a new object which is an instance of class Vehicle, and a reference of that
object is stored in variable car.

In Java no variable holds an object. A variable holds reference to an object.

Quoting David J. Eck of Hobart and William Smith Colleges, Geneva, New York:
You should think of objects as floating around independently in the computer’s memory. In
fact, there is a special portion of memory called the heap where objects live. Instead of holding
an object itself, a variable holds the information necessary to find the object in memory. This
information is called a reference or pointer to the object. In effect, a reference to an object
is the address of the memory location where the object is stored. When you use a variable of
class type, the computer uses the reference in the variable to find the actual object.
In a program, objects are created using an operator called new, which creates an object
and returns a reference to that object.
Guys; I can’t still help myself with this object concept. The object is so theoretical.
How do other OOP languages like C++ define an object? Bring it on guys!

User avatar
Gogeta70
^_^
^_^
Posts: 3275
Joined: 25 Jun 2005, 16:00
18

Re: OOP language Object concept!

Post by Gogeta70 »

Easy...

Code: Select all

#include <iostream>
#include <cstdio>
#include <cstring>

class ABC
{
	private:
		char str[1024]; // Private 'property' (aka variable)


	public:
	
		ABC() // Constructor...
		{
			for(short i = 0; i < 1024; i++)
			{
				str[i] = 0; // Set to null...
			}
		}

		void setstr(char* instr) // Public 'method' (aka function)
		{
			if(strlen(instr) > 1023)
				return;
			
			strcpy(str, instr);
		}

		char* getstr()
		{
			return str;
		}
};

int main(int argc, char *argv[])
{
	ABC* test = new ABC;
	
	test->setstr("Test string...");
	printf("%s\n", test->getstr());
	
	delete test;
	return 0;
}
Hope that helps, i tried to keep it as simple as possible.
¯\_(ツ)_/¯ It works on my machine...

User avatar
Hiram
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 153
Joined: 09 May 2009, 16:00
14

Re: OOP language Object concept!

Post by Hiram »

Thanks gogeta70, but you haven't stated the language in which the
source code is! Somehow looks like Java, but I don't understand this:
#include <iostream>
#include <cstdio>
#include <cstring>

User avatar
Gogeta70
^_^
^_^
Posts: 3275
Joined: 25 Jun 2005, 16:00
18

Re: OOP language Object concept!

Post by Gogeta70 »

It's C++.

The #include <x> line is a way of including external code into your program. The three you mentioned are part of the standard c/c++ headers.
¯\_(ツ)_/¯ It works on my machine...

User avatar
Lundis
Distorter of Reality
Distorter of Reality
Posts: 543
Joined: 22 Aug 2008, 16:00
15
Location: Deadlock of Awesome
Contact:

Re: OOP language Object concept!

Post by Lundis »

Hiram, in java you would be typing import instead of include. They work a bit differently but the concept's the same.

User avatar
floodhound2
∑lectronic counselor
∑lectronic counselor
Posts: 2117
Joined: 03 Sep 2006, 16:00
17
Location: 127.0.0.1
Contact:

Re: OOP language Object concept!

Post by floodhound2 »

Not sure if this helps but here goes

Think of an object as “code someone has already written”. Most have an input and output - no matter the language, and most are limited to what they are intended to be doing: A USB communications object is not intended to do the things that a Floating point arithmetic object will do. Also keep in mind the objects are not to be modified, at least not much. This is not always the case but that’s the idea.

Variables are meant to be used as a place to store data that is meant to be manipulated during run time. The object itself is not intended to be written over or changed during run time.

Again I am not sure if this help and I never programmed in java but I have experience in Visual basic – object oriented language (not to mention many other languages – they all seem the same).
₣£ΘΘĐĦΘŮŇĐ

Post Reply