Thread: Classes

  1. #1
    Registered User yodoblec's Avatar
    Join Date
    Jun 2003

    Unhappy Classes

    I'm having trouble understanding classes. Could someone please help me out with them. I mean just a way to make the tutorials seem easier to understand, anything will do.

  2. #2
    Rad gcn_zelda's Avatar
    Join Date
    Mar 2003
    A class is like a structure that has functions
    declare a class like this

    class [class name]
                Member Functions
    The variables can only be accessed through the member functions
    declare member functions
    [class name]::[function name](paramaters)
    use class
    int main()
           //create an instance of class
           [class name] variable;
    that's the basics, i guess

  3. #3
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    In addition to the above:

    There are three levels of access in classes (by classes, I mean classes or structs):
    public, private, and protected

    Any symbol (typedefs, functions, variables, member classes, etc.) that are declared public can be accessed by anything. Those symbols which are declared as private can only be accessed by the class itslef, and classes/functions declared to be 'friends' of the class (not even derived classes have access to these). Any symbol that is protected can be accessed by friends and derived classes.

    Constructors (that have the same name as the class, and any arguments you choose) are initializers. They are responsible for creating an instance of the class, and constructing its 'this' pointer. Desctrutors (same name, prefixed by '~') take no arguments, and destroy the object.

    Neither the constructors nor the destructor need to be public, and as it is possible to have a static build function in a class, as well as have the class commit suicide (delete the 'this' pointer). These are rarely used, however.

    One other thing. The difference between a class and a struct in C++ is simply that if you don't provide an access specifier (private, protected, public), then a class defaults to private, and a struct defaults to public.

    This is a rather rapid (and incomplete) overview, but a start, I suppose.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  4. #4
    Veni Vidi Vice
    Join Date
    Aug 2001
    A class is a user-defined datatype. Itīs through classes you can create ADT (abstract datatypes). ADT are used to make computerlanguage more humanlanguage (in simple terms). I will illustrate this with an example

    Lets say you want to code a program that handles infomation about employes in a company. Without classes
    //Simulate an employe-object
    //Code snippet 1
    char *name = "Smith";
    int age = 28;
    double salery = 40000.0;
    with classes
    class employee
    //Code snippet 2
    //Methods should be public
    employee(char *pName = "Smith", int pAge = 28, double pSalery = 40000.0);
    //Datamembers should be private
    char *mname;
    int mage;
    double msalery;
    Now of you study the code snippet 1 you can see that the information is scattered (it nothing that keeps all infomation together). This can (will) lead that the code is gonna be hard to read and maintain, especially when a program grows in size. But when using classes (code snippet 2) you actually encapsulate all necessary infomation to a single unit. But code snippet 2 only declares (dont allocates memory) what an employee is. If you want an employee-object (an instance) you have to define one.
    employee Eric;
    and to acces the elements of an employee-object
    std::cout << "Employee name is: " << Eric.mname << std::endl;
    std::cout << " and is " << Eric.mage << " years old" << std::endl;
    std::cout << "and has a salery " << Eric.msalery << std::endl;
    Classes is a big subject and donīt feel stupid if you donīt understand everything I said. It is hard.

    I personlly like books more than tutorials because they have more depth.
    01000111011011110110111101100100 011101000110100001101001011011100110011101110011 01100100011011110110111001110100 01100011011011110110110101100101 01100101011000010111100101110011 0110100101101110 01101100011010010110011001100101
    Good things donīt come easy in life!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you Initialize all classes once with New?
    By peacerosetx in forum C++ Programming
    Replies: 12
    Last Post: 07-02-2008, 10:47 AM
  2. Multiple Inheritance - Size of Classes?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 11-26-2004, 09:04 AM
  3. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  4. Exporting VC++ classes for use with VB
    By Helix in forum Windows Programming
    Replies: 2
    Last Post: 12-29-2003, 05:38 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM