Thread: Classes

  1. #1
    Confused
    Join Date
    Nov 2002
    Location
    Warwick, UK
    Posts
    209

    Classes

    Can anyone explain simply what a class is, how it works, the Public / Private aspect of it, and its uses, or point out a simple tutorial on it ? The CProgramming tutorial doesn't make much sense to me. Thanks.

  2. #2
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    You are going to need a tutorial or a book...

    Do you know what a stucture is?
    Structures have variables like: Employee.Age and Employee.Name.

    Classes have the same types of "member variables" plus they have have "member functions" (aka methods) such as Employee.IncrimentAge()

    A private variable / function can be accessed by another function inside the class. Public variables / functions can be accessaed outside of the class.

    An "object" is a specific instance of a class. If Employee is a class, then Bob and Mary can be objects.

    [EDIT]
    The book "Teach Yourself C++ is 21 Days" by Jesse Liberty explains this shtuff simply and clearly. (I think its a great beginning book, although some of our members don't like it.)
    Last edited by DougDbug; 05-01-2003 at 11:06 AM.

  3. #3
    Confused
    Join Date
    Nov 2002
    Location
    Warwick, UK
    Posts
    209
    Thanks. Yes, I know about structures, are classes basically more advanced structures that can cope with functions too, and have these protection filters ?

  4. #4
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    Yes. I think you understood what I was trying to say. I'll leave it to others to give more explainations and other perspectives.

    I just noticed that you are in Moscow... Your English seemed "American" to me! So, maybe it's not so easy to get a copy of "Teach Yourself C++ In 21 Days." I don't know if you can order from Amazon.com... Maybe you don't even want an English language book.

    And sorry I used slang... It's not even real slang... Shtuff is a made-up word that is a combination of "stuff" (which is real slang that means "things") and a bad word that also means "stuff"

  5. #5
    Confused
    Join Date
    Nov 2002
    Location
    Warwick, UK
    Posts
    209
    Don't worry about my languages, I speak fluently English, French and Spanish, and I'm learning Russian over here. I've lived in the US for 4 years and in England for 3, so slang is no problem...

    I have a copy of Teach Yourself C++ in 24 Hours, the older version of 21 hours I think. However it's in this thing that looks like Old French ( if others speak French and buy guides / books of this type in French, you'll know what I mean ) that they use in all teaching books to sound more intelligent. I'm reading about Quantum Physics and Relativity right now in the same style of language, and it's going slow

    Can anyone else give me examples or whatever ? I understand the basics of it, but I'm stuck on why you need Private, when you could simply not give the user access to that variable ( don't ask cin >> a; when you don't want "a" modified )...

  6. #6
    Funniest man in this seat minesweeper's Avatar
    Join Date
    Mar 2002
    Posts
    798
    >>Don't worry about my languages, I speak fluently English, French and Spanish, and I'm learning Russian over here. I've lived in the US for 4 years and in England for 3, so slang is no problem... <<

    Wow, respect due here me thinks, I could never get the hang of foreign languages.

    Anyway, I couldn't get the search feature to work just now but spend 15 mins or so searching the C++ board and look for a post by PolymorphicOOP in a thread describing the differences between structs and classes. He wrote an excellent (and huge) post all about it.

  7. #7
    Confused
    Join Date
    Nov 2002
    Location
    Warwick, UK
    Posts
    209
    Thanks a lot, I'll do that.

    I have one more question though. Say I have this code :

    Code:
    #include <iostream.h>
    
    char password;
    
    int main()
    {
    cout << "Password ?";
    cin >> password;
    
    if(password=="hello")
    {
    dothis();
    }
    else
    {
    cout << "Completely wrong !";
    }
    
    return 0;
    }

    What difference does it make if I do this instead :

    Code:
    #include <iostream.h>
    
    class pword
    {
    public: 
    char passtrigger;
    
    private:
    char realpassword = "hello";
    }
    
    pword password;
    
    int main()
    {
    cout << "Enter a password";
    cin >> password.passtrigger;
    
    if(password.passtrigger == password.realpassword)
    {
    dothis();
    }
    else
    {
    cout >> "Wrong !";
    }
    
    return 0;
    }

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    char realpassword = "hello";

    First, you can't do that: you can't initialize variables in a class declaration.

    There isn't any difference in what you're doing. Procedural programming can accomplish some things simpler than object oriented programming. I've read some stuff were people think the whole OOP method has be over sold.
    Last edited by 7stud; 05-02-2003 at 02:16 AM.

  9. #9
    Confused
    Join Date
    Nov 2002
    Location
    Warwick, UK
    Posts
    209
    char realpassword = "hello";

    Yeah, thanks, I saw that once I tried to compile it. Basically, what I see here is that classes just seem to complicate things, you need to have a method in public to change a private variable, etc..

  10. #10
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    "I understand the basics of it, but I'm stuck on why you need Private, when you could simply not give the user access to that variable ( don't ask cin >> a; when you don't want "a" modified "

    Your looking for the culprit in the wrong place. It's the programmer/user that can cause your class to fail. Think of your class as a black box and the functions as the interface to what's inside the black box. If you don't make the data members of the class private, either unwittingly or intentionally a programmer using your class could change the data members causing your black box to fail.

    If you define a class like this:
    Code:
    class foo
    {
    public:
         int number;
         int multiply_by(int factor)
         {
              return (number*factor);
         }
         foo()//constructor
         {
             number=3;
         } 
    };
    A programmer can use your class in his program to multiply the data member 3 by some factor he wants. Remember you are advertising your class(or black box) as being able to take a factor and multiply the data member 3 by that factor. So, if I do this in my program:

    foo a;
    ...
    some_function(a);
    ...
    int my_var = a.multiply_by(10);

    I expect my_var to equal 30. But what if another person working on the same project as me, a hacker, or even I, accidentally put some code in the function that does this:

    a.number = 8;

    That would screw everything up, and notice I never needed to ask the user cin>>number for it to get screwed up.

    "you need to have a method in public to change a private variable"

    That's a powerful tool because you can define HOW your data members can be changed or used.

  11. #11
    Confused
    Join Date
    Nov 2002
    Location
    Warwick, UK
    Posts
    209
    Ah, I think I'm starting to see it, thanks. It basically protects your code against your own mistakes... Thanks.

    Can you have functions in structures ?

    Also, while I'm here, does anyone know of a decompiler that will decompile into C++ ? I know of WinDasm, but that decompiles to Assembly...

  12. #12
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    "Can you have functions in structures ?"

    In C++, the only difference between structures and classes is that for structures the default attribute is public, and for classes it's private.

    I think in C, structures cannot have functions.

  13. #13
    Confused
    Join Date
    Nov 2002
    Location
    Warwick, UK
    Posts
    209
    Alright, thanks a lot guys.

  14. #14
    Registered User
    Join Date
    Nov 2002
    Posts
    126
    A class is the implementation of an object in C++. If you are interested in what objects are and how to use the object-oriented approach, just search for "object oriented programming" on gogle or somethin

    In C++, the only difference between structures and classes is that for structures the default attribute is public, and for classes it's private.
    Ohhh I don't know about that. Ever heard of a "struct hierarchy"? Neither have I... structures don't facilitate object oriented concepts like inheritance and polymorphism like classes do.

  15. #15
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    Originally posted by Korhedron
    Also, while I'm here, does anyone know of a decompiler that will decompile into C++ ? I know of WinDasm, but that decompiles to Assembly...
    I don't think you can decompile into anything back to c++.

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