Can anyone teach me how to use class? Also, what's the purpose of it??
Can anyone teach me how to use class? Also, what's the purpose of it??
Classes allow you to define your own types of variables. Things in real life can rarely be described by one variable, like length or weight, so it can be convenient to define a variable that has more than one element like this:
You have to get a beginning C++ book to learn more about the details of classes, or study an online tutorial.Code:#include <string> #include <iostream> using namespace std; class Car { public: int weight; double length; string color; }; int main() { Car my_car; my_car.color="red"; my_car.length=20.5; my_car.weight=1200; cout<<"My car is "<<my_car.color<<endl; return 0; }
Last edited by 7stud; 04-20-2003 at 03:04 PM.
Then, what's the differences between records and class?
As far as I know, there is no such thing in C++ as a "record".
I think he's thinking of a structure...Classes can have their own functions as well, as where structures can only be a group of variables.
(s)he's thinking pascal records
"He who makes a beast of himself, gets rid of the pain of being a man." Dr. Johnson
That is not true.....I think he's thinking of a structure...Classes can have their own functions as well, as where structures can only be a group of variables.
The ONLY difference in c++ between class and struct is default access specifier.... look...
That is the difference between struct and class in c++. structs in c are slightly different but even to say that structs in c couldnt have member functions is pretty wrong because they can, only you have to do it thru pointers to functions rather than functions themselves so the syntax is much more clumsy but it is possible.Code:struct A { int visable_; // this is public int get_visable(); // so is this and look its also a func. }; class B { int inivisable; // now this is private int get_invisable(); // and so is this }; struct C : A {}; // This is PUBLIC inheritance class D : B {};// and this is private inheritance
Free the weed!! Class B to class C is not good enough!!
And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi
Oops...did not know that...learn somethin new everday...
Why is it then that classes are used almost exclusively (or at least far more preferably) to structures in C++? For example, many people (including myself) override the default specifier to list public functions and data first. Why don't they just use struct's? Is it just convention?Originally posted by Stoned_Coder
The ONLY difference in c++ between class and struct is default access specifier.... look...
if stroustrup had his way then there would be no structs in c++ only classes but structs were retained for backwards compatability with c more than anything and they were also expanded for c++ to act like classes. A struct can be made to act like a class just by using access specifiers properly but they are very popular at the moment for things like abstract base classes and policy classes but classes could equally be used there is just ever so slightly less typing involved using struct.
Free the weed!! Class B to class C is not good enough!!
And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi
because classes is a c++ thing, and to keep it strictly c++, classes are preferred. but structs are used in cases such as linked lists, when you want to group variables under an object (although similar to a class other than specifier, it is mostly used for this).
But structs in C were different a bit, but in C++, they are essentially the same, just classes are preferred.
edit: this post was probably stupid. Stoned_Coder beat me anyways.
Last edited by alpha; 04-20-2003 at 09:11 PM.
Well, to keep things simple to yourself and not screwing your own mind Use Class when you build reusable operational object, and Struct for data object only.
Pls tell me the use of any program which uses classes,like calculator,some game??
>Pls tell me the use of any program which uses classes,like calculator,some game??
If it's written in C++ it very likely that classes are used.
p.s. What the alphabet would look like without q and r.
Any program can potentially use classes. A lot of people use them religiously for every project and will never use regular functions (only member functions). Any gui application would be a type of program where most people can quickly see as a use, and another extremely strong example is games. Any type of program where you can thing of aspects of the programs as "objects," then you can use a class. When taking the purely object-oriented approach to programming, ALL aspects of a program can be broken down into objects. It's actually more difficult to come up with a program that shouldn't use classes rather than ones that can.Originally posted by bookworm
Pls tell me the use of any program which uses classes,like calculator,some game??