Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


OOP is so confusing[wrong question]


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
9 replies to this topic

#1 Kurai Tsubasa   Members   -  Reputation: 207

Like
0Likes
Like

Posted 10 January 2014 - 05:39 AM

Hi guys i want to know the process of object orientated programming(OOP) i tried my best to understand what is exactly the process but i have no idea what is it.I have a code like this:







class Song(object):

	def __init__(self, lyrics):
		self.lyrics = lyrics
	
	def sing_me_a_song(self):
		for line in self.lyrics:
			print line
			
happy_bday = Song(["Happy birthday to you",
		   "I don't want to get sued",
                   "So I'll stop right there"])	
	   
bulls_on_parade = Song(["They rally around the family",
			"With pockets full of shell"])
						
happy_bday.sing_me_a_song()

bulls_on_parade.sing_me_a_song()

And i don't understand what is "for" and "in" statement it is really confusing me. and i don't understand what :







self.lyrics = lyrics

and 







for line in self.lyric:
        print line

is do for .Sorry for asking to muchunsure.png


Edited by Kurai Tsubasa, 10 January 2014 - 06:01 AM.


Sponsor:

#2 jjd   Crossbones+   -  Reputation: 2118

Like
3Likes
Like

Posted 10 January 2014 - 05:50 AM

Hi guys i want to know the process of object orientated programming(OOP) i tried my best to understand what is exactly the process but i have no idea what is it.I have a code like this:

class Song(object):

	def __init__(self, lyrics):
		self.lyrics = lyrics
	
	def sing_me_a_song(self):
		for line in self.lyrics:
			print line
			
happy_bday = Song(["Happy birthday to you",
		   "I don't want to get sued",
                   "So I'll stop right there"])	
	   
bulls_on_parade = Song(["They rally around the family",
			"With pockets full of shell"])
						
happy_bday.sing_me_a_song()

bulls_on_parade.sing_me_a_song()

And i don't understand what is "for" and "in" statement it is really confusing me. and i don't understand what :

self.lyrics = lyrics

and 

for line in self.lyric:
        print line

is do for .Sorry for asking to muchunsure.png

 

The problem here is nothing really to do with object oriented programming. Keywords like 'for' and 'in' are fundamental to python (and a lot of other programming languages -- object oriented or not) so I would suggest that you take I look a 'getting started' tutorial for python.

 

-Josh


Edited by jjd, 10 January 2014 - 07:33 AM.

--www.physicaluncertainty.com
--linkedin
--irc.freenode.net#gdnet


#3 BitMaster   Crossbones+   -  Reputation: 4275

Like
3Likes
Like

Posted 10 January 2014 - 05:51 AM

The question has nothing to do with object orientation. That is simply Python's for loop. I would strongly suggest taking a few steps back, you should learn more about the fundamentals of your chosen language before you try learning about more advanced concepts.

Edited by BitMaster, 10 January 2014 - 05:51 AM.


#4 rip-off   Moderators   -  Reputation: 8540

Like
0Likes
Like

Posted 10 January 2014 - 05:51 AM

Where did you find this code? Are you reading a book or following a tutorial online? If so, are you doing the excersizes after each chapter?

 

A good set of introductory material should be explaining each of these concepts on their own.



#5 Kurai Tsubasa   Members   -  Reputation: 207

Like
0Likes
Like

Posted 10 January 2014 - 05:59 AM

 

Hi guys i want to know the process of object orientated programming(OOP) i tried my best to understand what is exactly the process but i have no idea what is it.I have a code like this:



class Song(object):

	def __init__(self, lyrics):
		self.lyrics = lyrics
	
	def sing_me_a_song(self):
		for line in self.lyrics:
			print line
			
happy_bday = Song(["Happy birthday to you",
		   "I don't want to get sued",
                   "So I'll stop right there"])	
	   
bulls_on_parade = Song(["They rally around the family",
			"With pockets full of shell"])
						
happy_bday.sing_me_a_song()

bulls_on_parade.sing_me_a_song()

And i don't understand what is "for" and "in" statement it is really confusing me. and i don't understand what :



self.lyrics = lyrics

and 



for line in self.lyric:
        print line

is do for .Sorry for asking to muchunsure.png

 

This problem here is nothing really to do with object oriented programming. Keywords like 'for' and 'in' are fundamental to python (and a lot of other programming languages -- object oriented or not) so I would suggest that you take I look a 'getting started' tutorial for python.

 

-Josh

 

 

 

The question has nothing to do with object orientation. That is simply Python's for loop. I would strongly suggest taking a few steps back, you should learn more about the fundamentals of your chosen language before you try learning about more advanced concepts.

 

 

Where did you find this code? Are you reading a book or following a tutorial online? If so, are you doing the excersizes after each chapter?

 

A good set of introductory material should be explaining each of these concepts on their own.

 

oh,sorry i think i misunderstanding this,i was thinking this is OOP.Sorry ^^a



#6 rip-off   Moderators   -  Reputation: 8540

Like
4Likes
Like

Posted 10 January 2014 - 06:01 AM

The code involves OOP, sure. But the root cause is that you seem to be trying to understand a (relatively) complex example while being new to the language and not sufficiently exposed to the various constructs.



#7 Kurai Tsubasa   Members   -  Reputation: 207

Like
0Likes
Like

Posted 10 January 2014 - 06:04 AM

The code involves OOP, sure. But the root cause is that you seem to be trying to understand a (relatively) complex example while being new to the language and not sufficiently exposed to the various constructs.

Yeah i still beginner and i think i'm trying to understand that i haven't ready yet.i guess i will try to find some free books that is easy to learn for beginner^^


Edited by Kurai Tsubasa, 10 January 2014 - 06:06 AM.


#8 Shippou   Members   -  Reputation: 1720

Like
2Likes
Like

Posted 10 January 2014 - 08:38 AM

Attempting ton understand some of the more complex constructs in a language before learning the basics can be quite a bit confusing.

 

"for" is a loop statement

"in" is used to iterate through a list, dictionary, or array

"self" is a class variable construct - The syntax is a lot like C++ and is confusing to learn at first

 

 I would strongly recommend reading How to Think Like a Computer Scientist: Java or How to Think Like a Computer Scientist: Python and doing the exercises at the end of each chapter.

 The Ebooks are free.


Edited by Shippou, 10 January 2014 - 08:39 AM.

 Reactions To Technologies:
1. Anything that is in the world when you’re born is normal and ordinary and is just a natural part of the way the world works.
2. Anything that's invented between when you’re fifteen and thirty-five is new and exciting and revolutionary and you can probably get a career in it.
3. Anything invented after you're thirty-five is against the natural order of things.

- Douglas Adams 2002


 


#9 Tutorial Doctor   Members   -  Reputation: 1657

Like
0Likes
Like

Posted 10 January 2014 - 08:50 PM

I had the hardest time understanding Object Oriented Programming. I tried to collect my thoughts and make a tutorial to explain it (I understand it now). It might help. 

 

http://forum.maratis3d.com/viewtopic.php?id=839

 

The langauge I used is LUA. 

 

If you need me to clarify more, I can. 


Edited by Tutorial Doctor, 10 January 2014 - 08:53 PM.

They call me the Tutorial Doctor.


#10 Subtle_Wonders   Members   -  Reputation: 225

Like
1Likes
Like

Posted 04 February 2014 - 12:59 PM

To understand OOP (Object Oriented Programming), it's likely best to understand what an Object is when referring to programming.

 

Consider the following:

 

Scenario #1

You are a car mechanic. 

A car is delivered to you, and it requires a tune up.

You find that several nuts and bolts are lose on examination.

You search for and acquire an adjustable wrench.

Using the adjustable wrench, you tighten the nuts and bolts.

The result of using the adjustable wrench returns a task completed.

Since there are no other tasks associated with this tune up, the tuneup is completed.

 

Scenario #2

A second car comes in.

The car requires a replacement radiator.

On examination of the radiator, you determine that an array of tools are required, including an adjustable wrench.

You search for and acquire an adjustable wrench.

You use the adjustable wrench to loosen bolts.

You use other tools to further allow the removal and replacement of the radiator.

A new radiator is installed.

You use a combination of tools to again mount the radiator.

You again use the adjustable wrench to tighten the bolts.

The result of using the adjustable wrench returns a completed task (as with the other tools).

Since there are no other tasks associated with this radiator replacement, the replacement is completed.

 

An object seen in the two examples above would be the adjustable wrench

 

In programming, a object is essentially a class that gets instantiated, meets a criteria, and can be reused. For proper object creation, it must meet these four requirements:

It must support polymorphism.

It must support overloading.

It should support overriding (for consistency).

It should support encapsulation (for security).

 

Much like the adjustable wrench in the above example, the adjustable wrench was performed on both a tune up and a radiator replacement. It essentially was used to remove and tighten bolts and nuts in two different scenarios.  The nuts and bolts between the two cars were different, but were classified as the same type of property of which an adjustable wrench be used with. Likewise, two different sets of code may require the use of a class for different reasons, but pass a similar type of peropty and require a similar return value. This is essentially polymorphism, because the same object was used for the same reason with a different set or properties in two different scenarios (you had to adjust the wrench size for different size nuts/bolts two separate jobs, but got the job done with one adjustable wrench).

 

Polymorphism

The biggest challenge in OOP is thinking outside of primitives. As novice programmers, we tend to think int, float, double... and the associated values to such. But OOP is both similar and different. An object is a container of values/properties. The easiest way to truly understand an object is to understand a structure in C. You have an entity, and that entity has properties. When you use that entity, you can call one property, a set of properties, or all properties stored. If you want to get more complicated, you can stored objects within objects, and would have to iterate through the first object to access the nested objects, for the values stored in the desired object. Tricky, huh?  wink.png To further understand this, I would highly recommend studying data structures.  

 

Overloading

Overload is hard to explain without confusing people.  I would recommend researching it. In simplest definition, overloading is when you call the same function/method of a class, pass a different set of parameters. The parameters define which of the identically named function/methods you are calling.

 

Overriding

Overriding is when you are quite literately calling a method of identical name and parameter, but of a child class, which invokes the use of that method over the identical method of a super class.  A clean example of this is when you define a default constructor which is required in instantiating a class (for Java and C#). Another way to look at this is saying you have two classes, class 1, and class 2. You have a function/method called public int GetMe(int i) in both. To get to class 2, you must first instantiate class 1, then using that object, instantiate class 2. But instead of using GetMe(int i) in class 1, you use it in Class 2. You are overriding the GetMe(int i) method of class 1 with the GetMe(int i) method of class 2.

 

Encapsulation

Encapsulation is when you use access modifiers like private, protected, etc to hide variables from super classes, but give access to change such values through methods like setters/getters.

 

Again, if you truly want to understand OOP, study polymorphism, overloading, overriding and encapsulation. I would also advise studying further into containers and data structures, and learn how to use iterators. You'll never ask this question again if you know these key aspects. Best of luck to you.


Edited by Subtle_Wonders, 04 February 2014 - 01:08 PM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS