Join 244,213 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,455 people online right now. Registration is fast and FREE... Join Now!
Hey guys, Ime only 14 but been learning C++ well lately untill I got to the topic OOP in the book ime reading, I dont understand OOP one bit, its frustrating and feel like giving the whole c++ language up and moving onto c, I suppose just a little patience is needed though, after all I am a little tired tonight...
Maybe I should search for a online tutorial to see If i can understand OOP better, I understand "how to create a object" but the books going on about some other stuff with in OOP which I dont get. the Book is confusing me because it keeps telling me to add code into my previous file in different places and renaming files, maybe I need a better tutorial for OOP
Is OOP important in programming? I hear some of you experts talk about it abit, and hope I get my head around it someday, Never giving up the language though, Ime too far in to do that.
So basically question is, Is OOP confusing to learn, and how important is it to the C++ programming language its self.
Thanks guys
This post has been edited by xAlanxEnglandx: 26 Jul, 2008 - 02:08 PM
OOP (object oriented programming) is a cornerstone of almost all the major languages out there. It is very important to know and is found in Java, all the .NET languages, C++, PHP etc.
It is one of those things that will take a bit of time and then suddenly hit you like a ton of bricks (the eureka moment). To help with that I can give you a quick simple example of how it works.
OOP is a way for the programmer to organize code into the idea of an object so that people, who learn and categorize our world into objects, can understand complex problems. For example... you know what a "ball" is right? Well your computer doesn't. So we have teach it. We do this by constructing a class. This is a "blueprint" of how to construct a ball. It has 4 main things...
1) It has data members which store information (these are usually private variables like "color" or "size"
2) It has methods. These are functions of the object that describe actions or what it can do. Our ball might have a function called "bounce()"
3) It has properties. These are ways of getting at and setting the private members to values. Like we might have a property called "color" which we can use to set the color and get its color.
4) Then it has a special function called a constructor which is a function of the object with the same name as the object (in our case it will be called ball()) and is "called" any time we create an instance of our object.
So what do I mean by instance? Well we have a blueprint for our ball, but we still need to create many ball objects. Just like a blueprint for a building, it is just how to build a building, but we can use the same blueprint to create many buildings of the same type.
Now sometimes these blueprints are a very generalized plan. It shows us how to build a ball, but we might also have plans on how to build a specialized ball like a "tennis ball". It will borrow some of the blueprints for a ball likes its shape and color properties, but add onto it by specifying that it is only 3 inches in diameter. This is called inheritance. The tennis ball "inherits" some of the idea of a ball from "super class" or "parent class".
Lets say that our ball class was only meant to be "template" of specialized balls and never meant to create a generic ball, it is said to be abstract. Abstract classes only serve the purpose of providing code that other objects can inherit from.
So what does this look like in code?
cpp
#include <iostream> #include <string> using namespace std;
// Class for a ball class ball { private: // Private variable to hold its color name string color;
public: // Constructor function, when we create a ball // we are setting its default color to red ball() { color = "Red"; }
// Property functions (get and set the color variable) string getColor() { return color; } void setColor(string newColor) { color = newColor; }
// Method describing an action of the object string bounce() { return "The ball was bounced!"; } };
int main() { // Create a ball ball *redball = new ball();
// Create another ball (these two are INSTANCES of the ball class) ball *anotherball = new ball();
// Ask the ball its color using our property cout << "Color of the ball is: " << redball->getColor() << endl;
// Bounce a ball using its method bounce() cout << anotherball->bounce() << endl; return 0; }
Here we implement a ball class, give it a private variable to hold its color name, a constructor to initialize the variable, a couple property functions to set and get the color name, and a method called bounce to bounce our ball.
Notice that we then use this "blueprint" to create two ball instances. One called redball and the other called anotherball. We then ask those objects to do things for us. The first one to get its color and the other to bounce.
cpp
#include <iostream> #include <string> using namespace std;
// Class for a ball class ball { protected: // Private variable to hold its color name string color;
public: // Constructor function, when we create a ball // we are setting its default color to red ball() { color = "Red"; }
// Property functions (get and set the color variable) string getColor() { return color; } void setColor(string newColor) { color = newColor; }
// Method describing an action of the object string bounce() { return "The ball was bounced!"; } };
// Specialized class inherits from ball class tennis : public ball { private: // New size variable int size;
public: // Constructor... notice we can access color of the ball class // as if it was part of this class. We inherited this member // Notice back in "ball" that we had to make it "protected" to do this tennis() { color = "green"; size = 3; }
// Notice how this has the same name and type as the ball class // it "overrides" the ball class version. So if we call "bounce" on this object // it will call this one instead of the ball class' version. string bounce() { return "Bouncing a tennis ball!"; } };
int main() { // Create a ball ball *redball = new ball();
// Create another ball (these two are INSTANCES of the ball class) ball *anotherball = new ball();
// Ask the ball its color using our property cout << "Color of the ball is: " << redball->getColor() << endl;
// Bounce a ball using its method bounce() cout << anotherball->bounce() << endl;
// Create a tennis ball tennis *tennisball = new tennis();
// Notice it has a getColor too it inherited from the parent class cout << "Color of the tennis ball is: " << tennisball->getColor() << endl; cout << "Size of the ball is: " << tennisball->getSize() << endl;
// We call the tennis ball bounce method here, but if it wasn't defined // for the tennisball, it would have called the "ball" class version. cout << tennisball->bounce() << endl; return 0; }
There is the code again but with an inherited "tennis ball" class. It borrows the code in "ball" and adds onto it. Read the in code comments to see what I am doing with this.
Hopefully this all makes some sense and gives you a better idea of their relationship. Its a hierarchy. Generalized objects at the top, more specialized inherit down. Just like a kid inherits his dads eyes and even his grandfather's hair color and his great grandfather's nose.
Well, C++ was originally called "C with classes" so, yeah, OOP is important to it. However, it's not that bad. It can get bad if you don't completely understand the building blocks.
You must understand functions and probably structs. A class, in it's basic state, is little more than a struct with both variables and functions. (Function pointers, actually. ) A class ultimately adds a bunch of functionality, like constructors and implicit self referencing. However, if you know C well enough you could reasonably build you own class mechanism, which is essentially what was done initially.
Don't let OOP get you down. The idea appeals to people who've already written a lot of programs and can see how such methodologies will help them. If you're just learning to program, the advantage of a lot of things you're shown to do will probably not be immediately apparent. In the future, though, it might.
The most important quality a programmer can have is a high tolerance for frustration. Good luck.
EDIT: somehow knew Martyr2 would be me, but not by much.
This post has been edited by baavgai: 26 Jul, 2008 - 02:44 PM
OOP is just Object-oriented programming as opposed to Procedural programming. One of the advantages of OOP is code re-usability. As an example, I'm assuming you've used the math library by now, so instead of writing the code that calculates powers every time you need to, you can include code that someone else has already written and call on pow. Now this clearly isn't all there is to OOP so you should just keep learning C++ the way you are and remember that OOP is more a way of coding than anything else.
Best of luck to you and remember when in doubt googikki it
Thanks both of you, But to the first reply (Marty) I want to thank how much time you did to write out some help for me, thanks for that. But could you explain more about the "Private", "Public" and "Protected", Ime unsure how to use them.
Say I write a OOP for a ball, why is the color stringed in private, then you do something with it for public. Thanks for any advice back on this marty.
Also thanks to the second person who replied, I can see how programmers need high tollerence, I think I can handle the frustration though, After all ime still a beginner.
Thanks guys for help
QUOTE(F!st!cuffs @ 26 Jul, 2008 - 03:44 PM)
OOP is just Object-oriented programming as opposed to Procedural programming. One of the advantages of OOP is code re-usability. As an example, I'm assuming you've used the math library by now, so instead of writing the code that calculates powers every time you need to, you can include code that someone else has already written and call on pow. Now this clearly isn't all there is to OOP so you should just keep learning C++ the way you are and remember that OOP is more a way of coding than anything else.
Best of luck to you and remember when in doubt googikki it
Ime always using google, but then feel guilty trailing off to other tutorials for not following the book I paid for, cause different websites teach c++ in different ways kinda and could confuse me...?
This post has been edited by xAlanxEnglandx: 26 Jul, 2008 - 02:54 PM
Yes and no. The answer depends largely on your approach to it, and on the quality of the material from which you use to learn about it.
Object-oriented programming sounds complicated on the face of it, since it introduces a new way of thinking about program design, very different to the way you might think about program design in a purely procedural language, but the bottom line of OOP/OOD is that its simply nothing more than a new way to organise your code
To begin with, consider a slightly simpler topic of Object-based programming. Object-based programming is about gathering together related data and functions/procedures (usually referred to as 'Methods') into a logical block (an idea which forms the basis of OOP, though OOP extends on the idea considerably). If you've studied any programming language, you ought to be familiar with the idea of a struct or record - which, at the simplest level, is just a set of closely related data, for example, a 2d Coordinate might be represented in C as a struct, containing 2 double-precision members x and y
In a procedural environment, you'd possibly write a function which calculated the distance of that coordinate from its origin. In order to call that function, you would pass the struct object/variable into the procedure's argument list. for example (C++) -
cpp
#include <iostream>
struct coord { double x,y; };
double distance(coord point) { double ans = 0; // calculate ans return ans; }
int main() { coord c; c.x = 3.0; c.y = 4.0; std::cout << distance( c ); }
In an object-oriented or object-based environment, you may re-structure your code so that the function 'belongs' to the struct. You no longer need to pass the object into the function's parenthesis when you call it, because each object is automatically tied to the function - this would give you a different calling convention.
cpp
#include <iostream>
struct coord { double x,y; double distance() { double ans = 0; // calculate ans return ans; } };
The differences between the two examples are minor, but the significant change is that the distance function has been far more tightly related to the coord type. it gives the function a much stronger identity from a design perspective - rather than being a "loose" function which just dangles around on its own.
If you can't see the advantage of this yet, don't worry - Just consider that the majority of advances in programming are generally to do with allowing programmers to break problems up into smaller, manageable, reusable chunks, whilst also allowing code to be better understood by other programmers.
Thanks for your time to write out the help Bench, Thanks to everyone for the advice...
So basically its nothing more than a new way to organise my code, as Bench stated, But if so, why dont tutorials just teach "this way" in the first place?
Well to know how to organize brand new code you are writing, you have to understand the "paradigm" and the way of thinking behind it. It will also help you to understand what is going on behind the code and when errors appear to understand why they are errors. It is code organization, but that organization comes with rules. Just like you can't organize sheep to be a mineral. You have to understand the idea of an animal so you can correctly place that sheep in the animal kingdom.
This part will come with the experience.
As for the public, private, and protected they are referred to as "scopes". Basically who has access to them (aka permissions). Private members are to only be used by the class itself. Protected means that the class and any classes that inherit from it can see it, no one else. Public means it is open for other classes to use and see.
Think of it like your house. Your prized jewelry is yours... it is private. Your tv is yours but you let your kids watch it but not anybody off the street. It is protected. But if you take your tv and put it out on the street for anyone to see and change the channels on, it is public. In all three cases the object is yours, but one is exclusively yours, one is shared with your descendants, and the other is accessed by the general public.
In our example, color is protected so ball and tennis can both see it (this is the tv you share with kids), size is private to tennis (this is the tennis ball's jewelry) and the constructors and properties are public (this is so that other classes and create them... change the channel and use the tv).
This post has been edited by Martyr2: 26 Jul, 2008 - 03:14 PM
Well to know how to organize brand new code you are writing, you have to understand the "paradigm" and the way of thinking behind it. It will also help you to understand what is going on behind the code and when errors appear to understand why they are errors. It is code organization, but that organization comes with rules. Just like you can't organize sheep to be a mineral. You have to understand the idea of an animal so you can correctly place that sheep in the animal kingdom.
This part will come with the experience.
As for the public, private, and protected they are referred to as "scopes". Basically who has access to them (aka permissions). Private members are to only be used by the class itself. Protected means that the class and any classes that inherit from it can see it, no one else. Public means it is open for other classes to use and see.
Think of it like your house. Your prized jewelry is yours... it is private. Your tv is yours but you let your kids watch it but not anybody off the street. It is protected. But if you take your tv and put it out on the street for anyone to see and change the channels on, it is public. In all three cases the object is yours, but one is exclusively yours, one is shared with your descendants, and the other is accessed by the general public.
In our example, color is protected so ball and tennis can both see it (this is the tv you share with kids), size is private to tennis (this is the tennis ball's jewelry) and the constructors and properties are public (this is so that other classes and create them... change the channel and use the tv).
So Marty, Basically when Ime using Private, Protected and Public, I just use common sense to the object weather the properties of the object is Private, Protected or public to the object ime coding too.
This post has been edited by xAlanxEnglandx: 26 Jul, 2008 - 03:36 PM
Thanks for your time to write out the help Bench, Thanks to everyone for the advice...
So basically its nothing more than a new way to organise my code, as Bench stated, But if so, why dont tutorials just teach "this way" in the first place?
The example i've given is one of Object-based programming which is the foundation upon which OOP is built, except without any of the more advanced OO concepts such as inheritance, polymorphism, and the access specifiers which Martyr2 has mentioned. These are where the real power within OOP lies - But you can jump over those hurdles when you reach them
Why most OO tutorials don't begin this way is down to the tutorial. I suspect many you'll come across already assume a certain level of knowledge which some new programmers won't have, while others may just take an alternative approach of addressing all the general OO design issues first. As with alot of things, the key to learning is perseverance until you can piece together a complete picture from the 'jigsaw' of information that you'll find