View Full Version : what is object-oriented mean?
average_guy
July 2nd, 2002, 22:30
C++ and Java is object-oriented language, but what DOES IT MEAN ?!?!?!?! what does object-oriented mean?????????? :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :dead: :dead: :dead: :eyes: :eyes: :eek: :eek: :confused: :confused:
ammoQ
July 3rd, 2002, 12:05
In the classical (structured) paradigm, a program consists of functions that manipulate data; in OO, the program consists of objects that exchance messages.
A program in an OO language defines classes, which contain data (so called "attributes") and functions (called "methods"). This is called "abstraction".
Instances of a class are called objects. Classes can be build on top of other classes, which is called inheritance.
Classes often hide their attributes and some of their methods, and offer only some methods to other classes - this is called data hiding; it provides that the whole program independent of the implementation details of that class.
OO languages support polymorphism; this means, different classes offer similary named methods, and you can call the method of an object without knowing which class it exactly belongs to.
Demigod
July 3rd, 2002, 16:58
OO was designed with large programs in mind. When programs get to a certain size it's difficult to manage all the data and instructions. A single error would require you to skim through the entire program to find out what went wrong (because the data is shared with every function). A program like Windows 2000 is 2 million lines of code, which would take a VERY long time to debug if it were made in structured programming. OO uses the concept of data encapsulation, where the data is kept private to other functions. Objects contain their own data and is not shared by any others (unless they're a friend of the function). Because only the object has access to its own data this allows greater control over the program and helps greatly in debugging (when something goes wrong you just have to look at the object data, not the entire program). And unlike functions the private data is kept in memory when the function ends, allowing you to access it in the future through the object.
Another great thing about objects is their portability. Objects are not dependent on the main program so you can plug it into other programs and they'll work. It's kinda like plug-ins in epsxe:). When something goes wrong you only have to look at the plug-in that's causing the problem and not the entire program (errors reading CD-ROM -> CD-ROM plug-in, errors with sound -> SPU plug-in). The plug-ins can also be used on other emulators.
A good way to look at objects is to look at humans. The human race is the class and the human being is an object. We each have similar features (defined in the class) but we each have our own memory and certain capabilities. We also act independently of each other. Inheritance is deriving another class based on an existing class. For example, spiderman is derived from the human class but has some extra features (wall_climb(), web_shoot(), etc.).
ammoQ
July 3rd, 2002, 17:19
The problem is that many programs simply deal with mindless data, thatīs their job. In OO, you tend to give that mindless data a life of their own, only because you donīt want to have classes without methods so it would look like you write structured programs.
average_guy
July 3rd, 2002, 18:37
can anyone give me a example of a part of code in C++?
----------------------------------------------------------
In object-oriented programming, a generalized category that describes a group of more specific items, called objects, that can exist within it. A class is a descriptive tool used in a program to define a set of attributes or a set of services (actions available to other parts of the program) that characterize any member (object) of the class. Program classes are comparable in concept to the categories that people use to organize information about their world, such as animal, vegetable, and mineral, that define the types of entities they include and the ways those entities behave. The definition of classes in object-oriented programming is comparable to the definition of types in languages such as C and Pascal.
Demigod
July 3rd, 2002, 21:48
Here's a Class example in C++:
Header file - Human.h -
class human
{
private: // private data cannot be accessed by outside functions
char *data;
public: // public data can be accessed by outside functions
human();
walk ();
talk ();
breathe ();
~human();
};
- human.cpp -
#include "human.h"
human::human()
{
data = new char [30];
}
human::walk()
{
data = "abcde\0"; // Private members can only be accessed through the class functions
}
...
Main file - main.cpp -
#include "human.h"
void main ()
{
human Bob; //Create an object called "Bob" from the human class
human Jason;
Jason.walk();
Bob.talk();
}
sithspawn
July 4th, 2002, 02:38
Oh, don't forget the concepts of polymorphism and inheritance. I'm too lazy to elaborate :p Anyone else care to explain?
ammoQ
July 4th, 2002, 13:58
read the posts above
average_guy
July 5th, 2002, 15:50
ammoQ, which emulator author r u?
ShADoWFLaRe85
July 5th, 2002, 17:01
Originally posted by average_guy
ammoQ, which emulator author r u?
He created the pad plugin, padJoy ;)
average_guy
July 6th, 2002, 05:15
good
Nezzar
July 7th, 2002, 13:21
When talking about C++ I have some questions, too. (I don't want to start another thread on it)
Could somebody explain me this piece of code (the problem is the pointer):
void somefn(int number, struct somestruct *pArray) {
int i;
for(i = 0; i < number; i++) {
pArray[i].pName = (char*)0; /* what does this mean? */
}
}
The Structure goes like the following:
struct somestruct {
char *pName;
/* other stuff... */
}
The part I don't understand is the line "pArray[i].pName = (char*)0;".
(OK, this isn't actually a C++-Prob, it's more C)
ammoQ
July 7th, 2002, 16:46
(char*) 0 is a null pointer, it point's to "nothing"; it is used to signal that this pointer does not point to real data.
"Thou shalt not follow the NULL pointer, for chaos and madness await thee at its end. " (more like this:
http://www.cs.bgu.ac.il/~omri/Humor/ten-commandments.html)
Nezzar
July 7th, 2002, 16:59
Thanks.
Nezzar
July 8th, 2002, 10:04
Another question:
What's HEAP-Memory and what is it good for?
ammoQ
July 8th, 2002, 12:53
Heap is the memory used for allocation with alloc, malloc() or new; as opposed to stack, which is used for local variables, return adresses etc.
Nezzar
July 8th, 2002, 15:08
And is it faster or something and why do I have to allocate manually?
N-Rage
July 8th, 2002, 15:31
Originally posted by Nezzar
And is it faster or something and why do I have to allocate manually?
Just guess You want to make a List of Telephonenumbers with an Array ( long Telnumbers[100] ), you would have to know how much numbers You will be receiving, else it would be
a) wasting much memory if You just use a part of the Array
b) limited by the size of the Array.
If you allocate dynamically, You are only restricted by Heapsize and the memory usage is better than always ussing big Arrays.
the code would be like this:
long *TelNumbers = (long*)malloc( 10 * sizeof(long) ); // Allocate space for 10 Numbers
TelNumbers = (long*)realloc( TelNumbers, 20 * sizeof(long) ); // Expand to 20 numbers.
Nezzar
July 8th, 2002, 15:43
OK, I think I got it. But how do I put the numbers in there? It's just some memory I have allocated and TelNumbers is pointing to it. Isn't this just one var (I thought I'd need an array)?
N-Rage
July 8th, 2002, 15:57
its a pointer-to-long.
You can dereference( get the value theire pointing to ) pointers with *Telnumbers and You can access them just like an array : Telnumbers[5] would be the 6th element.
You can even calculate with them, but as flexible they are they are also the main reason for access violations and other nice errors.
For example if You do this: Telnumbers[-1] = 3;
then You write a value to an arrea that AINT allocated( 1 spot BEFORE your array ), the outcome can be anything from nothing, a access-violation to a lockup, depending on what this Memory-Value is used.
Pointers are a major feature of C/C++, you should learn about em.
ammoQ
July 8th, 2002, 17:45
If you want to build structures like trees, lists etc. you will have to allocate memory from the heap. There is obviously the problem that you have to give back the allocated memory once you don't need it any more; if your program keeps allocating memory without freeing it, it's called a memory leak and it will sooner or later crash. Some languages like Java and C# have a so called garbage collector which automatically collects unused memory, so you don't have to worry any more about it.
Nezzar
July 9th, 2002, 12:40
long *TelNumbers = (long*)malloc( 10 * sizeof(long) ); // Allocate space for 10 Numbers
This means I created an array with place for ten longs, right?
Thank you guys, you helped me a lot.
ammoQ
July 9th, 2002, 12:47
Originally posted by Nezzar
This means I created an array with place for ten longs, right?
right.
vBulletin® v3.8.7, Copyright ©2000-2013, vBulletin Solutions, Inc.