Classes and structures

This is a discussion on Classes and structures within the C++ Programming forums, part of the General Programming Boards category; It doesn't really matter because in C++ a struct has all those nice features that a class has. It's really ...

  1. #16
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    It doesn't really matter because in C++ a struct has all those nice features that a class has. It's really just a matter of opiniopn. The reason structs still exist is for backwards compatability.

    Usually people use structs when they want to use the struct solely for holding data without functions (though you'll find that that rarely happens in practice because if you are storing data then you are using it for something.

  2. #17
    Registered User
    Join Date
    Jul 2002
    Posts
    34
    The only reason I'm doing that is because I'm currently taking a course in C++ and the instructor requires this in a project! Personally I think using them together is not beneficial *shurg *(at this point)...but what do I know I've only been doing this for 3 months. Thanks for the help! MCorn

  3. #18
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    I stand corrected. I'm so used to looking at legacy C code (that's the only place I see structs anymore) that I just went with it. Good call guys.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  4. #19
    Registered User Xaviar Khan's Avatar
    Join Date
    Sep 2001
    Posts
    10
    Datainjector,

    Classes are majorly different then structs. They appear the same but classes have the ability to contain their functions inside them, where structures do not. The entire point of a class is encapsulation or it's portability. If you are making outside refrences (as required by structures) you would be breaking that encapsulation right off the get.


    hth
    X
    Xaviar Khan
    Programmer/Owner
    Shards of Destiny (SoD)
    ---------------------------------------
    Reach us with any MUD client at ShardsofDestiny.org
    port 9000.
    +------------------------------------+
    | Html/Java support added, |
    | 'www.ShardsofDestiny.org' |
    +------------------------------------+

  5. #20
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Sorry, Xaviar Khan. I repeat, the ONLY difference between structs and classes in C++ is the default member access.
    Copy, compiler, and run the following program. If I haven't screwed up someplace it will run just fine, functions and all in the struct.

    Code:
    #include <iostream>
    
    struct example
    {
      example(int);
      void increment();
      int getNum() const;
    
      private:
         int num;
    }
    
    example::example(int input) : num(input) {}
    
    example::increment();
    {
       num += 24;
    }
    
    int example::getNum()
    {
      return num;
    }
    
    struct example2 : public example
    {
       void increment();
    }
    
    void example2::increment()
    {
       num += 1;
    }
    
    int main()
    {
       example ex(2);
       cout << ex.getNum();
       ex.increment();
       cout << ex.getNum();
    
      example2 ex2(2);
      ex2.increment();
      cout << ex2.getNum();
    
       return 0;
    }

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about classes and structures.
    By RealityFusion in forum C++ Programming
    Replies: 19
    Last Post: 08-30-2005, 03:54 PM
  2. Replies: 1
    Last Post: 06-17-2005, 07:31 AM
  3. Classes and Structures.
    By jrahhali in forum C++ Programming
    Replies: 6
    Last Post: 03-28-2004, 04:03 PM
  4. Structures, Unions and Classes
    By Makoy in forum C++ Programming
    Replies: 2
    Last Post: 02-23-2004, 01:57 PM
  5. structures fooloing to look like classes
    By samsam1 in forum C++ Programming
    Replies: 4
    Last Post: 01-18-2003, 10:43 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21