Some really amateur questions

Started by
15 comments, last by TheTroll 17 years, 9 months ago
Quote:Original post by Old Speckled Monkey
Do you think that perhaps not diving straight into object-oriented programming is a good idea, and perhaps start by learning something more simple like BASIC?

OSM



starting with OO isn't necessarily a bad idea and java is a pretty decent first language.

i can try to explain some of the stuff atleast.

a variable could be seen as a container,

int a; would create a "container" for an integer and name that container 'a'.

a=5; would then put the number '5' into that container.

and if(a==5) { do some stuff } would do some stuff if a contains the number '5'.

a function does something (java doesn't have functions though, only methods, but a method is pretty much a function that belongs to a class).

a simple function would be

int add(int a, int b) {
return a+b
}

the first int is the return type (so the add function would return an integer)

int a,int b is the arguments or parameters

if you then do int result = add(5,15);

result would contain the number '15'.

a class describes an object

for example

public class Point {
private int x;
private int y;

public Point() { //simple constructor
x=0;
y=0;
}

public void setPosition(int px, int py) {
x=px;
y=py;
}
public void draw();
//code to draw the point to the screen.
}
}

you can then create instances of that class like this:

Point myPoint = new Point();
Point myPoint2 = new Point();
and then use that point.

myPoint.setPosition(100,100);
myPoint.draw();
myPoint2.setPosition(100,100);
myPoint2.draw();

so the class contains the code (methods) and data members (attributes) that describes a type of object. a car could have attributes such as speed, acceleration, weight, etc and methods such as turnLeft(), turnRight, Accelerate(), etc.

im sure someone else can explain it better though.
Advertisement
Ok thanks guys
Ok thanks guys
Hi...
I agree that it could be confusing...
Here is some good link I recommend.. it is redder easy to read.


ENG
http://www.cplusplus.com/doc/tutorial/

SWE
http://www.infa.abo.fi/~chakie/kurser/c++/html/book1.html
Am I right in saying that an object is any statment within a class? Or is it just the variables and functions?
Or am i completely wrong!

OSM
Object is like a box. There are variables and functions in it.
So only one variable from that object is not the whole object, just a part of it.
I hope you understand.
We will start with 8 first. And object is a way of thinking about the parts or your code. There are two types of programming (this is not really true, but close enough for what we are talking about here), procedural and Object oriented. In procedural programming, we think in verbs or actions. In Object Oriented programming we think in Nouns or classes. Let’s take a car, in procedural, we would talk about, turning, go forward, go reverse, stop. In object oriented programming we would have, tires, engine, transmission, steering box.

A class is a way to describe a noun in object oriented programming. A class has member variables and methods. A member variable holds data of a specific type (there are ways around this, but those are special cases). A method does some type of action. In the car we have a steering box, a variable might be the current rotation of the box, a method might be Turn left, Center, or Turn right. Your functions often act on your variables. In a class variables and methods can be private, protected or public. Private means that they can only be used within that class, public means that they can be directly accessed outside the class. Protected is kind of a special access type we will talk about in a couple.

When you “declare” a method (declaring a method is creating a template in code that shows how this method will be used) in your code you set your method parameters and the method’s return type. So back to the steering box, we make a method Turn_Left and define is as follows; float Turn_Left(float amount). This says we will turn left by a given amount and when it is done it returns a float saying how much it turned. The reason they might be different is if we reached the left limit. The return value lets you know how much you really turned it, the amount you sent into the function was just really a request of how much you wanted to turn it. The reason you do it this way is because the code that uses your class does not care about how the inner workings of your class work, it only cares about the implementation of the class. This is called encapsulation.

To use your class in your code you first have to instantiate it. This means we create it in memory. Once we create it in memory we then call it an object. It is now something we can use. So we instantiate the Steering_Box class and name it My_Steering_Box. We now use the Turn_Left function we created. My_Steering_Box.Turn_Left(35.5). The 35.5 is an argument. It is how we tell the function what we want it to do. You can use literals (the 35.5 is a literal) or a variable in the argument.

Well I think that covers everything you asked. Hope that helps.

theTroll

This topic is closed to new replies.

Advertisement