-
Classes
Im currently reading a book called "Teach yourself C++ in 24 hours." Im on hour 7, and am not getting these classes. I dont understand entirely how they work, or why you would want to use them. Would it be a bad idea for me to skip over the chapter on classes? Or should i really know them before i continue? thanks.
-
Yes-definitely -especially if you want to either become a programmer or major in CS.
However, if this is just a hobby and you have no interest whatsever then you could skip it. Otherwise YES.
-
hehe i was afraid of that, thanks though, i appreciate it much.
-
If you skip them, you might as well stop learning C++. You'll need to know them. They are used for object-oriented purposes. Be sure to understand them before you continue. It will only lead to future confusion. Or you could skip, and do it the hard way.
-
Classes are a very important part of C++, they are used everywhere. It can take a while to understand their sintax, but after that it shouldn't be too bad.
>> I dont understand entirely how they work, or why you would want to use them.
Let's say you want to store information for various rectangles, you can devise a class to store the data and functions to get/set the data.
Code:
#include <iostream>
using namespace std;
class Rectangle
{
public:
Rectangle() { width = 0; height = 0; } /* constructor */
void set_width(double w) { width = w; }
void set_height(double h) { height = h; }
double get_width() { return width; }
double get_height() { return height; }
double area() { return width*height; }
private:
double width, height,
}; /* don't forget the semicolon */
int main()
{
Rectangle my_rect;
my_rect.set_width(5);
my_rect.set_height(4);
cout << "Width: " << my_rect.get_width() << '\n';
cout << "Height: " << my_rect.get_height() << '\n';
cout << "Area: " << my_rect.area() << endl;
return 0;
}
As you can imagine, classes can get quite more complex, but these are the basics.
-
"I dont understand entirely how they work, or why you would want to use them."
Do you think it might have anything to do with the book? The Sam's books aren't worth the paper they're printed on. The title should have been your first clue.
-
You could probably learn more from bad internet tuts. I use to use Sams, it sucks ;)
-
are you familiar with structured programming.. If not first lear that.. then move on to the theory of OOP's concept.. Then start classes...
-
I have that book, and yes its not that great but its decent enough.
The problem is the classes arnt discussed via practical applicatiions the whole damn book is about cats and crap.
basicly a CLASS is kinda like a template.
I like to think of classes like MOLDS for clay or things that get poured and then harden. Basicly you create a mold and alteration tools like scrapers, paint brushes, etc. etc that will change or alter the orignal mold for custom look. Classes have tools called methods that allow you to set and access your slight modifications to an overal general mold.
My explination probably isnt that good, but i assure you classes are something you must get a grasp on to become a solid programmer (and eventually get a job)
-
Ok, the book isnt helping me grasp it that well, does anyone have any links that explain classes well? Im gunna try the tutorial on this site right now. :D
-
well think of a class as a class room..with desks being functions and the people in the classroom being variables. a class is basically a way of grouping them together..supposed you just put an entire school (desks and kids) into a giant hall...it's less structured as opposed to putting them into classrooms
classes basically group a bunch of functions and variables together so they can be used and accessed as one. it helps with organizing.
like if you have a bike. one bike can be a class, with member functions being ride() and putdownstand(). you can have an array of these classes, like lets say
class bike {
//stuff here
void ride();
}
bike mybikeshop[50]; //i have 50 bikes
and now i can access each bike individually much more easily, rather than having something like
void ridebikeone()
void ridebiketwo()
...
hope that made sense
-
wookie's analagy is nice, but leaves out the difference between public and private members...
public members would be like the attendance office - they know where people are in the school, and visitors ( main() ) would go to the attendance office (a function in the public part of the class) to see/change/etc. the people in the classroom (stuff in the private part of the class). the people in the classroom can't be touched directly by anything except for what's in the public part. the public part is, well, public.
in other words, the class protects data in the private area, and that data can only be accessed by functions in the public area, which can be called from main().
-
thanks guys im starting to get it now :D