Calgarypuck Forums - The Unofficial Calgary Flames Fan Community

Go Back   Calgarypuck Forums - The Unofficial Calgary Flames Fan Community > Main Forums > The Off Topic Forum > Tech Talk
Register Forum Rules FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools Search this Thread
Old 05-10-2010, 04:43 PM   #1
FanIn80
GOAT!
 
FanIn80's Avatar
 
Join Date: Jun 2006
Exp:
Default OO and U

...or Object Oriented, and You.

OK, so I just started an OO class, and we're using C# as the language of choice. Our instructor has an incredibly thick Asian accent that he is apparently unaware of, because he talks a mile a minute with it and just rips right through with no one having a clue what he's saying.

Anyway, I'm not really having any problems with the syntax side of things, but I just need to get my head wrapped around the very simple basics. I've heard people here say that "most people don't have a clue how OO works." I'm hoping someone can help fill in the blanks.

Here's my understanding of things right now...

Class: Functions? Methods? I still don't get the difference between a function and a method, and now I have class to add to it? As near as I can tell, "class" is just another name for a function - which is another name for a method... although I can help feel I'm missing out on something here. No one in their right minds would invent three different terms for exactly the same concept.

Object: I've always thought of elements as objects (form fields, tables, etc), but now I'm getting the sense that things like classes are objects too. In the realm of OO, what exactly defines an object? Is it just anything that can be referenced in code?

Public/Private/Static/Void: Logic tells me that public and private mean the same thing they've always meant. Public is public to the application, and private is private to the... class? Static is... actually, I have no clue what static means. I know the main bit of code that runs automatically is static, but I don't know why. Void... As near as I can tell, void is just the absence of type?

Anyway, I know it's kind of a lot to ask... and I do have a text book, but I'm just having a hard time getting my head around these things. One minute, I think I have it figured out, the next minute the instructor uses something somewhere that makes me questions things all over again. I don't know why the light bulb isn't going off here. I'm usually pretty quick at picking these things up. The weird thing is that I can always get my code working, but I can't explain any of it for the life of me.

Edit: Whoops. I just realized that some of the things I've mentioned are C# specific and aren't necessarily related to OO at all...

Last edited by FanIn80; 05-10-2010 at 05:56 PM.
FanIn80 is offline   Reply With Quote
Old 05-10-2010, 05:07 PM   #2
FlamesPuck12
First Line Centre
 
Join Date: Apr 2007
Exp:
Default

Class - Like structs, but for OO where "functions" that deal with that "struct" (class) are called methods.
Objects - They're like the name of the class. So if the class was "hockey players" then "Iginla" would be an object.
Public/Private - Access levels of variables and methods. Anything under public access means any functions outside of its own methods can adjust or access. Private access means only its member functions (methods) can adjust or access.
FlamesPuck12 is offline   Reply With Quote
Old 05-10-2010, 05:11 PM   #3
FlamesPuck12
First Line Centre
 
Join Date: Apr 2007
Exp:
Default

An example, copy and pasted from my notes. (Well not really, I just rewrote one on the spot)

Edit: This is in C++

//So class is like "struct" and the class name is Stats

class Stats {
public: //Anything below is public access, until the keyword private

int getGoals(); //These functions are methods
void setGoals(int x); //int, void are return types. Void doesn't return anything and since you're only using them to adjust member variables, you don't need a return value

int getAssists();
void setAssists(int x);

private:
int G; //These are private variables that only methods can access or adjust
int A;
};

int main() {
class Iginla;
Iginla.setGoals(50);
Iginla.setAssists(50);
}

Last edited by FlamesPuck12; 05-10-2010 at 05:15 PM.
FlamesPuck12 is offline   Reply With Quote
The Following User Says Thank You to FlamesPuck12 For This Useful Post:
Old 05-10-2010, 05:15 PM   #4
FanIn80
GOAT!
 
FanIn80's Avatar
 
Join Date: Jun 2006
Exp:
Default

Ohhh... So a class is just a set of parameters (attributes, behaviors, properties), while an object is just a named instance of that class?

That makes a lot more sense, thanks!

I'm still out to lunch with the method/function thing though. So... method is just another name for a private function?

Last edited by FanIn80; 05-10-2010 at 05:19 PM.
FanIn80 is offline   Reply With Quote
Old 05-10-2010, 05:16 PM   #5
FlamesPuck12
First Line Centre
 
Join Date: Apr 2007
Exp:
Default

Edited my last post with some comments, I hope that clarifies it.

Edit: In the example, "Stats" would be the class, where Iginla would be an object.

Methods and functions are pretty much the same thing. Methods are any functions that are listed in the class definition. So in that "Stats" class, all the functions inside: getGoals, setGoals, getAssists, setAssists are all methods that belongs to the Stats class.

Last edited by FlamesPuck12; 05-10-2010 at 05:18 PM.
FlamesPuck12 is offline   Reply With Quote
The Following User Says Thank You to FlamesPuck12 For This Useful Post:
Old 05-10-2010, 05:19 PM   #6
FanIn80
GOAT!
 
FanIn80's Avatar
 
Join Date: Jun 2006
Exp:
Default

Oh wait, I think I get it now. So a method is a function that belongs to an object's class? Kind of like a behavior?

You the man, btw. Thanks!
FanIn80 is offline   Reply With Quote
Old 05-10-2010, 05:34 PM   #7
photon
The new goggles also do nothing.
 
photon's Avatar
 
Join Date: Oct 2001
Location: Calgary
Exp:
Default

Heh, this is like taking the red pill. It's true, OO is something that most people don't really get, even working in the industry for a long time, so don't worry that it's gonna take a while to sink in.

Function vs Method:

The difference between a method and a function is small, but significant. A function is a piece of code that can be called by name to perform some task. It may be passed data on which to operate (i.e. void foo(int x)) and it may return data (i.e. int foo(int x)). All data for the function to operate is passed into the function.

A method is also a chunk of code that is called by name, but it is a function that is associated with an object, a method is part of an object's identity and behaviour. This is important because in addition to the information passed into a method, the method has implicit access to all the rest of the data and methods of that object.

In C there are only functions, no methods. In Java (and C# I think, but i'm not sure) there are only methods, no functions. In C++ there are both.

How does this relate to OO? In a non OO world, if you wanted to have a register function for various animals, you might have to do this:

Code:
Dog dog = new Dog();
Cat cat = new Cat();

register(cat);
register(dog);
But your register method would have to know how to register both dogs and cats, and if you added a Bird type you'd have to change your register method again, the register code would have a bunch of conditional statements and be brittle and difficult to maintain.

In OO, you would instead have a register method:

Code:
Dog dog = new Dog();
Cat cat = new Cat();

cat.register();
dog.register();
So the cat and dog would know how to register themselves.

Class vs Object:

A class is a definition of an object, an object is an instance of a class.

A class is the code that defines the methods, properties, interfaces, etc of whatever type it is you are creating. So I would have a Cat class file that would define the register method, as well as the cat's name, age, date of birth, colour, whatever else it needed.

Code:
class Cat
{
  string name;
  
  public void speak()
  {
    Console.WriteLine("Meow");
  }

  public void register()
  {
    // do stuff
  }
}
An object is an actual instance of that class.

Code:
Cat cat = new Cat();
cat.register();
cat.speak();
The variable cat contains a reference to an object in memory that is constructed based on the Cat class.

Public / Private, this gets into scope, and scope can be complex and different for different languages, but from a high level as you say private means private within the scope, and public means accessible to scopes outside.

So if in our example the register method was public, I could call the register method as I did above. If the speak method was private though, I could not, it would not be visible. I could only call the method from within the class itself. This is useful if you want to have utility methods inside the class that aren't part of the class's interface with the rest of the world.

Static means that the thing it's talking about is visible at compile time rather than runtime, or rather that it's at a class level rather than an object level.

What does that mean? In order to give a cat a name, first I have to instantiate a cat object, and then assign a name to it. Every cat object can have a different name, and the idea of a name at the class level doesn't make any sense, since a name is a property of a cat object.

But sometimes you want to define something at a class level, something that is common to all cats.

Code:
class Cat
{
  private static final string species = "Felis Catus";
  private string name;
  
  public string getName()
   {
     return name;
   }

  public static string getSpecies()
  {
    return species;
  }
 
}
So now I put in a species property that is public, accessible by anyone, and is static meaning I can access it without instantiating an object. I can't get the name without first creating a cat object and assigning it a name, but I can get the species.

Code:
Console.WriteLine("Species: " + Cat.getSpecies());
Cat cat = new Cat();
Console.WriteLine("Name: " + cat.getName());
Notice I called the getSpecies method with a cat with a capital C, meaning I'm calling the method on the class, not on an object.

void as you say just means no type.. if you declare a method as void it means the method won't be returning anything when it finishes.

Ignore the case conventions in my code, I'm a long time removed from C# and my case conventions are mostly from Java.
__________________
Uncertainty is an uncomfortable position.
But certainty is an absurd one.
photon is offline   Reply With Quote
The Following User Says Thank You to photon For This Useful Post:
Old 05-10-2010, 06:21 PM   #8
FanIn80
GOAT!
 
FanIn80's Avatar
 
Join Date: Jun 2006
Exp:
Default

Whoa. That is insanely awesome, man. Thanks!

The light bulb finally went on! Especially the bit about static. That makes a lot of sense, and now that I look at my code I can see that's exactly what's going on.

PS: You should be teaching this class. I've been banging my head against the wall for over a week here. Every time I asked my instructor to explain things, I just couldn't grasp it for the life of me. He's a good teacher, in that he obviously cares and tries really hard... plus he clearly knows what he's talking about... but I just could not get a handle on what he was saying.

Last edited by FanIn80; 05-10-2010 at 06:24 PM.
FanIn80 is offline   Reply With Quote
Old 05-10-2010, 06:31 PM   #9
FanIn80
GOAT!
 
FanIn80's Avatar
 
Join Date: Jun 2006
Exp:
Default

Sorry, one last question if you guys don't mind... How does a "constructor" fit into all of this? Is it just where you define how an object is instantiated?
FanIn80 is offline   Reply With Quote
Old 05-10-2010, 06:34 PM   #10
photon
The new goggles also do nothing.
 
photon's Avatar
 
Join Date: Oct 2001
Location: Calgary
Exp:
Default

Heh, I would like to be a teacher, I used to do some teaching of this stuff (well Pascal) in school as a TA.

Yup, a constructor is exactly that.

We'll wait here until you go over inheritance and interfaces since you'll be back then
__________________
Uncertainty is an uncomfortable position.
But certainty is an absurd one.
photon is offline   Reply With Quote
Old 05-10-2010, 06:36 PM   #11
FanIn80
GOAT!
 
FanIn80's Avatar
 
Join Date: Jun 2006
Exp:
Default

Haha, awesome. I can't wait!

Edit: I'm going to bookmark this thread. I know I'll have to revisit it a few times this week while I work through some of my assignments.

Last edited by FanIn80; 05-10-2010 at 06:40 PM.
FanIn80 is offline   Reply With Quote
Old 05-10-2010, 09:57 PM   #12
Bill Bumface
My face is a bum!
 
Bill Bumface's Avatar
 
Join Date: Feb 2003
Exp:
Default

Quote:
Originally Posted by FanIn80 View Post
Sorry, one last question if you guys don't mind... How does a "constructor" fit into all of this? Is it just where you define how an object is instantiated?
Exactly. If you are making a new HockeyPlayer, HockeyPlayer may not be valid without a name and number, so you could have a constructor requiring those fields:

new HockeyPlayer Iggy = new HockeyPlayer("Jarome", 12);

I haven't coded in 7 years, so excuse any errors, but that's the idea.
Bill Bumface is offline   Reply With Quote
The Following User Says Thank You to Bill Bumface For This Useful Post:
Old 05-11-2010, 12:24 AM   #13
FlamesPuck12
First Line Centre
 
Join Date: Apr 2007
Exp:
Default

^Just to add, (not sure about C# but at least for C++) the compiler creates a default constructor and a destructor that does nothing. You can also have constructor that doesn't take any parameters.
FlamesPuck12 is offline   Reply With Quote
Old 05-11-2010, 12:42 AM   #14
cheung31
Scoring Winger
 
cheung31's Avatar
 
Join Date: Sep 2004
Location: Calgary, AB
Exp:
Default

Obligatory mentioning of concepts, for when the time comes:
- Encapsulation
- Inheritance
- Polymorphism
__________________
cheung31 is offline   Reply With Quote
Old 05-11-2010, 09:22 AM   #15
BlackEleven
Redundant Minister of Redundancy
 
BlackEleven's Avatar
 
Join Date: Apr 2004
Location: Montreal
Exp:
Default

Quote:
Originally Posted by FanIn80 View Post
Sorry, one last question if you guys don't mind... How does a "constructor" fit into all of this? Is it just where you define how an object is instantiated?

There's also a copy constructor, which is an important concept. It allows you to define the behaviour when your object is copied. For instance, if you have a class containing pointers as member variables, you likely don't want to copy the pointers into the new object. Otherwise, you'd have two pointers pointing to the same memory address and thus modifying one would modify the other, which is most cases is not what you want. You can define a copy constructor to specify exactly what is done with the member variable when copying an object.

It's one of those things you have to be careful with when working in language that allow you to control memory (like C++). If you're not, it can lead to loads of nasty bugs.

It typically looks something like this:

myObject( const &myObject copyObject);

The const is highly advisable so you don't accidentally modify the source object.

Edit: Sorry, I realize this was a bit beyond the scope of your original question. You may not have deleved into the topic of pointers. Suffice it to say you can use a copy constructor to define how exactly one object is copied from another.

Last edited by BlackEleven; 05-11-2010 at 09:52 AM.
BlackEleven is offline   Reply With Quote
Old 05-11-2010, 10:07 AM   #16
ZedMan
Scoring Winger
 
Join Date: Apr 2008
Exp:
Default

C# is more like java than C or C++. There are no pointers per se, only references. There are no copy constructors. To accomplish the same thing, you can write a constructor that takes as a parameter a reference to an existing object, and use that explicitly.

For example: http://msdn.microsoft.com/en-us/libr...16(VS.80).aspx

Last edited by ZedMan; 05-11-2010 at 10:11 AM.
ZedMan is online now   Reply With Quote
Old 05-11-2010, 10:10 AM   #17
FanIn80
GOAT!
 
FanIn80's Avatar
 
Join Date: Jun 2006
Exp:
Default

This is all good stuff man, thanks. I've only finished the first week of this class, so I still have a lot to cover before it's done... and then there's an advanced class after this one is done. Lots of room for covering lots of concepts.

Edit: Yeah, I've heard from quite a few people that C# is similar to Java. Also, this class is using C#, but the advanced OO class after uses Java.

Last edited by FanIn80; 05-11-2010 at 10:14 AM.
FanIn80 is offline   Reply With Quote
Old 05-11-2010, 10:34 AM   #18
kermitology
It's not easy being green!
 
kermitology's Avatar
 
Join Date: Oct 2001
Location: In the tubes to Vancouver Island
Exp:
Default

Once you understand the basics of object oriented programming you should be able to swiftly pick up other languages. The only difference coming in some syntax or native API that you're able to call. The design principles remain the same, regardless of the language that you're using.

Some people deride Java for it's performance, but it's gotten a lot better over time, and it's really nice to have a huge base of API that does things you need to do without you having to write that code.

At least I found the transition from C++ to C style C++ to Java to Objective C pretty easy. Then again, I'm ridiculously smart.

The one thing to get into the habit of now, and I learned this writing assembly is to document your code. Sure, it's just for school, big deal right? WRONG. If you learn good design principles now and get into the habit of doing it right away, life becomes so much simpler for you. I'll bet you'll get improved marks on code that's reviewed for school because you're clearly explaining what you're doing in English as well as in code.
__________________
Who is in charge of this product and why haven't they been fired yet?

Last edited by kermitology; 05-11-2010 at 10:38 AM.
kermitology is offline   Reply With Quote
The Following User Says Thank You to kermitology For This Useful Post:
Old 05-11-2010, 10:45 AM   #19
FanIn80
GOAT!
 
FanIn80's Avatar
 
Join Date: Jun 2006
Exp:
Default

Yeah, exactly. That's why I'm really trying to get a solid base going here. My code always works, but I want to actually know why it works so I can write it myself naturally, instead of just logically.

Good tip on the comments too. I'm starting to see how usefull that is already.
FanIn80 is offline   Reply With Quote
Old 05-11-2010, 10:58 AM   #20
ZedMan
Scoring Winger
 
Join Date: Apr 2008
Exp:
Default

Quote:
Originally Posted by kermitology View Post
The one thing to get into the habit of now, and I learned this writing assembly is to document your code.
Jeebus, assembly. I will never forget that feeling of looking at code I had written only the day before and having NO IDEA what it did anymore

That said, higher level languages like C# and Java can be much more self documenting through the use of well chosen variable, function and parameter names.

Transitioning between Java and C# is pretty painless. With the exception of library class names, simpler programs will look pretty similar between the two.
ZedMan is online now   Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -6. The time now is 11:57 AM.

Calgary Flames
2024-25




Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright Calgarypuck 2021