CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    Join Date
    Mar 2009
    Posts
    82

    classes question

    Hello! I got one quick question. For what tasks will I use classes? Why classes and not structures?
    Please give me example, where I can solve the task only with classes and not structures.

    Thanks in advances.

  2. #2
    Join Date
    Nov 2006
    Location
    Australia
    Posts
    1,569

    Re: classes question

    You'll use classes for the OOP part of C++. As for classes vs structs, see this.
    Good judgment is gained from experience. Experience is gained from bad judgment.
    Cosy Little Game | SDL | GM script | VLD | Syntax Hlt | Can you help me with my homework assignment?

  3. #3
    Join Date
    Dec 2008
    Posts
    56

    Re: classes question

    struct and class are identical in every way, except that when declaring methods and/or data members in a class, by default, they are treated as private members of the class. In a structure, by default, all methods and/or data are public.

    Naturally, you can change the permission levels of methods and data members using the appropriate label of "public:", "protected:", or "private:".

    Many s/w developers that program in C++ tend to use a class vs. a struct for modeling their objects. When only data members need to be encapsulated is a struct used. But that is just based on developer's preference.

  4. #4
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    Re: classes question

    The difference between class and struct is only the access method.
    Thanks for your help.

  5. #5
    Join Date
    Mar 2009
    Posts
    82

    Re: classes question

    Thanks for the posts. And can I ask you what will I need the private: for? Is there any specific reason?

    Thank you.

  6. #6
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: classes question

    Quote Originally Posted by StGuru
    And can I ask you what will I need the private: for? Is there any specific reason?
    You might want to read an introductory book like Accelerated C++ that will explain both the syntax and concepts to you, step by step.

    I could try and explain to you, but do you know what are member functions?
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  7. #7
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: classes question

    Quote Originally Posted by StGuru View Post
    Thanks for the posts. And can I ask you what will I need the private: for? Is there any specific reason?

    Thank you.
    Mostly to ensure the safety and proper use of your class. As a really simple example, say your class has a data member that can only store a value between 1 and 10. Making that member private ensures that other users of that class can't set an inappropriate value. You'd make a method to set the value public and have it reject inappropriate values.

  8. #8
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: classes question

    Think of it this way. You've got this big, complicated algorithm. It's frankly ugly, and relies on all sorts of assumptions about the input data. Trying to incorporate something like that into a larger bit of code would be pretty daunting-----who knows what might break it.

    But, if you can make all that functionality class-private, and throw a simple, clean, easy-to-understand interface on the class.....suddenly all the messiness is hidden from the rest of the code. It's abstracted away, and all you have to worry about is validating arguments to the interface. It makes everything a lot easier.

  9. #9
    Join Date
    Jan 2009
    Posts
    19

    Re: classes question

    Most of the people talk differentiate classes and structure on basis of setting varriables etc as public or private by default.

    the thing here to understand is that procedures can be calles , used whatever in class but it can't be used in structures.

    Representation of data might seem easy in structure but when it comes to utilizing there are lot of limitations and use of classes can really help you alot in the long run

  10. #10
    Join Date
    Jun 2006
    Location
    M31
    Posts
    885

    Re: classes question

    Quote Originally Posted by Abhishek Chauhan View Post
    Most of the people talk differentiate classes and structure on basis of setting varriables etc as public or private by default.

    the thing here to understand is that procedures can be calles , used whatever in class but it can't be used in structures.

    Representation of data might seem easy in structure but when it comes to utilizing there are lot of limitations and use of classes can really help you alot in the long run
    What are you talking about? There's nothing* that the struct class-key can't do when compared to the class class-key.

    *The only exception, of course, is the special treatment of the class keyword when it comes to template parameters.

  11. #11
    Join Date
    Jan 2009
    Posts
    19

    Re: classes question

    1. In classes the default scope is private whereas in structures u have public.equir

    2.Also u can define the scope according to is requiremeuctunts in claasses.

    3. In classes u can create fn nd methods which is not so in d case of structures.

    4.You cn also do operator overloading in case of classes which is not so in d case of structures.

    5.Various other object - oriented features are found in classes and not in structures wherin u can create objects which is not possible in structures.

  12. #12
    Join Date
    Jun 2006
    Location
    M31
    Posts
    885

    Re: classes question

    Quote Originally Posted by Abhishek Chauhan View Post
    1. In classes the default scope is private whereas in structures u have public.equir

    2.Also u can define the scope according to is requiremeuctunts in claasses.

    3. In classes u can create fn nd methods which is not so in d case of structures.

    4.You cn also do operator overloading in case of classes which is not so in d case of structures.

    5.Various other object - oriented features are found in classes and not in structures wherin u can create objects which is not possible in structures.
    1. Correct. Don't forget about inheritance as well.
    2. What are you trying to say?
    3. Wrong.
    4. Wrong.
    5. Wrong.

    In short, see my previous post.

  13. #13
    Join Date
    Jan 2008
    Location
    California, USA
    Posts
    822

    Re: classes question

    Hello Abhishek Chauhan

    I think you understood OP's confusion but not what's been said here.
    I do too think that when the OP asked "why structure" in place of "class",
    he was actually thinking about the struct in C, and not the struct in C++

    In C++, the keyword struct and class are used interchangeably,
    the difference of which is what's been said by Plasmator and others.

    For example, some use the keyword struct for POD type,
    so it really is a matter of perference.
    You could do a simple foo class using struct and compile with C++ compiler (not C compiler).
    to see this for yourself.

  14. #14
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: classes question

    Quote Originally Posted by Abhishek Chauhan View Post
    1. In classes the default scope is private whereas in structures u have public.equir

    2.Also u can define the scope according to is requiremeuctunts in claasses.

    3. In classes u can create fn nd methods which is not so in d case of structures.

    4.You cn also do operator overloading in case of classes which is not so in d case of structures.

    5.Various other object - oriented features are found in classes and not in structures wherin u can create objects which is not possible in structures.
    Spell check. Use it. It's hard to figure out what you're trying to say.

  15. #15
    Join Date
    Mar 2009
    Posts
    82

    Re: classes question

    I tried, and also I can use the public and private in the struct. If struct = class what are both for?

Page 1 of 2 12 LastLast

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured