Thread: Classes. Need help with em!

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    3

    Question Classes. Need help with em!

    Im advancing faster then I thought in C++ basics. Though Im at a sticking point. Im confused with classes and their uses with permissions, etc. Im also confused with the main syntax. Tutorials have only confused me more with the type of classes and what their used for. Can any give me a "Talk to me like a 3 year old" explaination?

  2. #2
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317
    If you really want to know create a test class
    declare some items public private and protected.
    create a single member function and modify it to try to access the public memeber
    then modify to access the private memeber
    then modify to access the protected member.
    Create a global function not part of the class and try to do the same.
    Make the global function a friend function of the class
    Create a second class (Not a derived class) with a member function and repeate the above steps
    Make the second class a Friend Class to the first and repeat the above

    now create a derived class and again repeat the above.
    Write down your results of your EXPERIMENTS. Then if you still have questions ask.
    Have fun!
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

  3. #3
    Registered User
    Join Date
    Nov 2004
    Posts
    3
    Hrm? This is all I understood of what you said:
    declare some items public private and protected.
    create a single member function and modify it to try to access the public memeber
    then modify to access the private memeber
    then modify to access the protected member.
    Create a global function not part of the class and try to do the same.
    The stuff I didnt understand:
    Make the global function a friend function of the class
    Create a second class (Not a derived class) with a member function and repeate the above steps
    Make the second class a Friend Class to the first and repeat the above
    Explain how you make a function a friend of a class, and how do you create a second class with a member function.
    Lol Im sorry if I seem like a total newb.

  4. #4
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317
    [Edit]
    Oops sorry about the typos Fixed them I suck at typing.
    [/Edit]

    You have a class definition
    Code:
    TestClass1.h //
    class TestClass1
    {
    private:
    int private_var;
    protected:
    int protected_var;
    public:
    int public_var;
    //to make a function or another class or a another classmember function a friend simply 
    //use the friend keyword
     
    friend int anotherfunction();
    friend Class2; 
    friend Class3::class3memberfunction();
     
    int test1_member_Function(); //memberfunction you can also make functions private or protected just list them under the appropriate section
     
    TestClass1(); //constructor
    ~TestClass1(); //destructor
    };
    now for the definition of the class member functions
    Code:
    TestClass1.cpp
    #include "TestClass1.h"
    TestClass1::TestClass1() //definition for constructor 
    {
    }
    TestClass1::~TestClass1() //defintion for destructor
    {
    }
    int TestClass1::test1_member_Function() //definition of test member fucntion
    {
    return private_var; //change this to the others to see what happens
    }
    This is a single class if you want to create a second class just duplicate this code but Replace the TestClass1 classname with whatever other name TestClass2 perhaps. One final note include both headers of the classes in your main.cpp
    Last edited by manofsteel972; 11-15-2004 at 12:57 AM.
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    friend functions, etc... are usually something you don't learn right away when learning C++ classes. If you don't know what they are, just practice what you have seen from the above code. Later, when you're reading more tutorials, and you see the friend function, come back and take a look at this code, and it will make more sense.

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Here's my two cents worth.

    C++ has a limited, but robust set of primitive data types, like int, double, char, arrays, pointers, etc. Lot's of times we want to do things which are a bit more sophisticated/complicated than that. The language protocol allows us to declare our own data types using any of several syntaxes, including typedef, like in C; struct, also available in C, but an expanded version of the C style; and something completely impossible in C, called classes (there may be some other ways to create user defined types like unions, etc. but I'll ignore those). With each class there will be one or more members, like variables or functions, or else why bother creating your own type in the first place? Sometimes you don't want other people monkeying around with one or the other of the class members so you can declare them private. Other members you want only closely related entities, like derived classes, to have access to, so they can be declared protected, and other members you don't care who can access, so they are declared public. It does take practice, and thought aforehand, to know what to declare how. There is no way to give any hard and fast rules.

    Interestingly, classes and C++ structs can even be derived from other classes/structs. Sometimes you want to give different levels of access to base class members by the derived class members than at other times. Therefore, the derived classes can themselves be declared with the same levels of access that any member can be--private, protected and public. Again, the only way to learn how to telll what's the best access for a given class is experience. Like any new language you learn, things will be more cumbersome to begin with, but if you work long enough writing your own code, and read a variety of other peoples code, and interact with other people who have more experience than you do, you will eventually feel more comfortable.


    Sometimes you want an outside, unrelated entity like a single function or a class that isn't derived from the given class, access to private or protected members that they otherwise wouldn't have. Or maybe you want only a given function of a derived class to have access to a private member of a base class and all the other members of the base class can access all the protected and public methods of the base class. That's where use of the keyword friend comes in.

    So the key is to remember that there are different access rights for different occassions and the more you use them the more comfortable you will be with them.

  7. #7
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Elad's description was quite nice, so I only have a couple of things to say:

    1) The distinction between structs and classes in C++ is simply this:
    Code:
    struct A {
       int foo; // No access specifier provided, so this is public.
    };
    
    class B {
       int foo; // No access specifier provided, so this is private.
    };
    So, what you can do with one, you can do with the other.

    2) The most straight-forward use of classes is, as Elad pointed out, user-defined data types. This means, that you are clustering pieces of data that are logically connected to each other with the set of possible/useful operations on them (some of which may be friends, but that can be saved for later). There are other uses, but this is, as I said, the most direct application, and it does underlie other uses as well. Note that you do not want to overdo the set of operations (functions) you give a class. Some operations on it logically belong elsewhere, in those cases, put them where they belong, but again, this it is more important to get used to the basics first.

    Cheers
    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.

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