Thread: what is class?

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    4

    Unhappy what is class?

    Can anyone teach me how to use class? Also, what's the purpose of it??

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    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:
    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;
    }
    You have to get a beginning C++ book to learn more about the details of classes, or study an online tutorial.
    Last edited by 7stud; 04-20-2003 at 03:04 PM.

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    4
    Then, what's the differences between records and class?

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    As far as I know, there is no such thing in C++ as a "record".

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    162
    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.

  6. #6
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330

    ~

    (s)he's thinking pascal records
    "He who makes a beast of himself, gets rid of the pain of being a man." Dr. Johnson

  7. #7
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    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.
    That is not true.....
    The ONLY difference in c++ between class and struct is default access specifier.... look...
    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
    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.
    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

  8. #8
    Registered User
    Join Date
    Feb 2003
    Posts
    162
    Oops...did not know that...learn somethin new everday...

  9. #9
    Registered User
    Join Date
    Dec 2002
    Posts
    56
    Originally posted by Stoned_Coder
    The ONLY difference in c++ between class and struct is default access specifier.... look...
    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?

  10. #10
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    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

  11. #11
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    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.

  12. #12
    Registered User
    Join Date
    Apr 2003
    Posts
    23
    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.

  13. #13
    Student Forever! bookworm's Avatar
    Join Date
    Apr 2003
    Posts
    132

    Question

    Pls tell me the use of any program which uses classes,like calculator,some game??

  14. #14
    Open to suggestions Brighteyes's Avatar
    Join Date
    Mar 2003
    Posts
    204
    >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.

  15. #15
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Originally posted by bookworm
    Pls tell me the use of any program which uses classes,like calculator,some game??
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class design problem
    By h3ro in forum C++ Programming
    Replies: 10
    Last Post: 12-19-2008, 09:10 AM
  2. Two conceptual questions
    By AntiScience in forum C++ Programming
    Replies: 3
    Last Post: 11-01-2007, 11:36 AM
  3. Defining derivated class problem
    By mikahell in forum C++ Programming
    Replies: 9
    Last Post: 08-22-2007, 02:46 PM
  4. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM