Struct & Class >> Difference ?

This is a discussion on Struct & Class >> Difference ? within the C++ Programming forums, part of the General Programming Boards category; What's the difference between a struct and a class ? http://mahurshi.tripod.com/mainframes.htm...

  1. #1
    Unregistered
    Guest

    Exclamation Struct & Class >> Difference ?

    What's the difference between a struct and a class ?




    http://mahurshi.tripod.com/mainframes.htm

  2. #2
    Registered User raimo's Avatar
    Join Date
    Jun 2002
    Posts
    107
    class members are private by default and struct members are public by default.

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    3
    Classes can also be put in a hierchy so classses that are made a public to other classes will inherite their varibles and functions.

  4. #4
    Unregistered
    Guest

    Unhappy err...

    I am a newbie...
    Sorry, I couldn't understand what you're saying..

    could you please explain in detailed ?

    when is it advantageous to use classes or structs >?


    http://mahurshi.tripod.com/mainframes.htm

  5. #5
    Registered User raimo's Avatar
    Join Date
    Jun 2002
    Posts
    107
    Classes can also be put in a hierchy so classses that are made a public to other classes will inherite their varibles and functions.
    And so can structs:
    Code:
    #include <iostream>
    
    struct Mouse{
      int tail;
    };
    
    struct Computermouse : public Mouse {
      int prize;
    };
    
    int main (){
      Computermouse mouse;
      mouse.tail=10;
      mouse.prize=130;
      std::cout<<mouse.tail<<" "<<mouse.prize<<std::endl;
      return 0;
    }
    Classes are preferred because the private variables provide encapsulation. Then you use methods for accessing variables.

  6. #6
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    By Quzah
    Code:
    class Car
    {
    public:
        SpeedUp( ) { engine.Accelerate( ); }
        SlowDown( ) { engine.Decelerate( ); }
        Crash( )
        {
            engine.GrindingHalt( );
            wheels.FallOff( );
            body.BendFrame( );
        }
    private:
        class Engine engine;
        class Wheels wheels;
        class Body body;
    };
    Then you do something like:

    Car mycar;
    mycar.SpeedUp( );
    mycar.SpeedUp( );
    mycar.SpeedUp( );
    mycar.SpeedUp( );
    mycar.Crash( );
    with structs, you would have to replace mycar.SpeedUp() with the actual code to speed up the 'car', and it wouldn't be in a nice neat little package.

  7. #7
    Registered User raimo's Avatar
    Join Date
    Jun 2002
    Posts
    107
    Following code compiles fine:
    Code:
    #include <iostream>
    
    struct Engine{
    private:
      int speed;
    public:
      void Accelerate(){speed++;}
      void Decelerate(){speed--;}
      void GrindingHalt(){int foo, bar;foo=bar=0;}
    };
    struct Wheels{
    private:
      bool on;
    public:
      void FallOff(){
        on=false;
      }
    };
    struct Body {
    private:
      bool foo;
    public:
      void BendFrame(){foo=false;std::cout<<"Frame foo"<<std::endl;}
    };
    struct Car
    {
    public:
      void SpeedUp( ) { engine.Accelerate( ); }
      void SlowDown( ) { engine.Decelerate( ); }
      void Crash( )
      {
        engine.GrindingHalt( );
        wheels.FallOff( );
        body.BendFrame( );
      }
    private:
      Engine engine;
      Wheels wheels;
      Body body;
    };
    int main(){
      Car mycar; 
      mycar.SpeedUp(); 
      mycar.SpeedUp(); 
      mycar.SpeedUp(); 
      mycar.SpeedUp(); 
      mycar.Crash(); 
      return 0;
    }
    ...which implies that you are wrong.

  8. #8
    Unregistered
    Guest

    Unhappy Help!!!!

    Can you guys make it simple please ?? I am totally confused now..

  9. #9
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    > class members are private by default and struct members are public by default.

    This is correct. In fact, in C++ classes and struct are EXACTLY the same except for that members of structs are public by default.

    This means, in:

    struct Test
    {
    int a;
    };

    the integer 'a' member is public. I.e. I could write:

    Test tl
    t.a = 5;

    If I were to write:

    class Test
    {
    int a;
    };

    The a member is private, i.e. it cannot be accessed outside the class. When it is said that it is private by default, it means that the 'visibility' of 'a' has not been specified, and therefore defaulted to private.

    To write a class which is equivilent to the struct example above, I would need to write:

    class Test
    {
    public:
    int a;
    };

    Here I have explicitly stated that int a should be public.
    OS: Windows XP
    Compilers: MinGW (Code::Blocks), BCB 5

    BigAngryDog.com

  10. #10
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563

    Wink Re: Help!!!!

    Originally posted by Unregistered
    Can you guys make it simple please ?? I am totally confused now..
    Simply the members in class could have one of 3 kind of properties. They are public, private and protected.

    Struc acts just as the class that all of its members with public property.


    This is what I remembered from my book.
    please let me know if there is anything wrong.
    Never end on learning~

  11. #11
    D3T
    D3T is offline
    Registered User
    Join Date
    Feb 2002
    Posts
    12
    Code:
    struct somestruct {
    int i = 1;
    int j = 3;
    }
    
    struct somestruct :: anotherstruct {
    int k = 2;
    int l = 4;
    anotherstruct { cout << i << k << j << l << ".";
    }
    thast probably wrong but if it were corrected would result in something like "1234", however if they were classes not structs, you would get an error saying sommat like i and j were undefined/private etc.
    [D3T]
    Borland Turbo C++ 3.0 For Dos

    *
    do { war(); } while (nations == hostile);

    ... The best way to prevent wars is to have them.

  12. #12
    Registered User raimo's Avatar
    Join Date
    Jun 2002
    Posts
    107
    Struc acts just as the class that all of its members with public property.
    No, you can use words public, private and protected also in structs and they work in exactly the same way than in classes.

    This implies that the following mean the same:
    class A_class{int variable1;};
    struct A_class{private: int variable1;};

  13. #13
    Banned frenchfry164's Avatar
    Join Date
    Oct 2001
    Posts
    1,552
    I never knew you could put different properties to structs (private, public, protected), I always thought only classes could. I guess we learn something everyday.

  14. #14
    Jeez
    Guest

    Talking And how many years did it take for you to learn that ?

    And how many years did it take for you to learn that ?
    How old are you now ?

  15. #15
    Banned frenchfry164's Avatar
    Join Date
    Oct 2001
    Posts
    1,552
    well I've been programming in various languages since I was 7, and I'm going to be 14 in 3 days.

    This is just one of those small things sometimes you don't ever learn until later, and you wish you would've learned it earlier.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting from C to C++
    By Taka in forum C++ Programming
    Replies: 5
    Last Post: 04-08-2009, 03:16 AM
  2. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  3. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM
  4. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 02:30 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 07:49 PM

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