Thread: Different between Class and Struct

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    116

    Question Different between Class and Struct

    When I see at template, there's word name "Class". and I don't know what different purpose of Class and struct:
    for example:
    Code:
    class avatar{
    public:
         int City;
         int address;
    }
    struct avatar{
          int City;
          int address;
    }
    I don't know what different purpose of two type of declare, above. Except that, at "class", there some different points I have known like "Method". (but if something you can use by "Method", you also can use it on struct by another way)

    So, who can help me please.
    thanks

  2. #2
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    You need to sit down and learn C++, these aren't quick topics to breeze through. Start with our C++ tutorials and work your way through to the end. I wouldn't necessarily skip over things just because you have been programming in Pascal. Look through the lessons, read quicker through the topics you know already, but still read through.

    As for using the STL (Standard Template Library) you can get by without getting into the exacts of template implementation in C++, however I wouldn't necessarily recommend it.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by hqt
    When I see at template, there's word name "Class".
    You should learn about template programming to understand this. Note that C++ keywords are case sensitive, so it is class, not Class.

    Quote Originally Posted by hqt
    I don't know what different purpose of Class and struct:
    They serve the same purpose, unless distinguished by convention, e.g., the struct keyword might be reserved to define aggregate structs that have no member functions and only public member variables. There are differences with respect to access control, but these should be evident if you were only to follow proper instructional material as AndrewHunter advised.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    based on your example above, the two are functionally identical.

    the difference is default access level. with a struct, everything is implicitly public unless you specify that it should be private or protected, whereas a class is just the opposite - everything is private unless explicitly made public.

    aside from that fact, there is no difference that I am aware of.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. struct vs class
    By jack_carver in forum C++ Programming
    Replies: 13
    Last Post: 12-01-2009, 12:09 AM
  2. struct or class?
    By George2 in forum C++ Programming
    Replies: 24
    Last Post: 12-05-2007, 11:55 AM
  3. Making a (atom) class in a (struct) class makes big errors!
    By Yarin in forum Windows Programming
    Replies: 4
    Last Post: 09-11-2007, 07:18 PM
  4. Class Or Struct?
    By appleGuy in forum C++ Programming
    Replies: 6
    Last Post: 06-27-2007, 11:06 PM
  5. Using Struct and Class?
    By bob2509 in forum C++ Programming
    Replies: 4
    Last Post: 10-01-2002, 10:49 PM