class and struct

This is a discussion on class and struct within the C++ Programming forums, part of the General Programming Boards category; I'm just starting to program and I'm trying to figure out what the difference in a struct and a class ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    5

    class and struct

    I'm just starting to program and I'm trying to figure out what the difference in a struct and a class is? I've tried google, but I'm still confused. Could someone give me a hand?

  2. #2
    Registered User
    Join Date
    Feb 2005
    Posts
    14
    Struct makes more sense.
    A struct holds data only
    classes hold data AND functions

  3. #3
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    By default a structures members are public and a class's members are private. Class's support the concept of inheritance, yada, yada, yada.

    Take a look at this tutorial
    Last edited by andyhunter; 02-11-2005 at 09:43 PM.
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  4. #4
    Registered User
    Join Date
    Feb 2005
    Posts
    5
    Thanks for the responses....I supposed I shouldn't even bother to ask about what you mean by ineritance yet...

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    As far as you are concerned, there is no difference. There is one tiny difference, but don't worry about it now. In C++, you use classes.

  6. #6
    Registered User
    Join Date
    Feb 2005
    Posts
    5
    Ok cool.

    So with classes then I put all my variables in one place instead of at the top of my program?....

  7. #7
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    Did you read the tutorial? That is a very vague question.
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  8. #8
    Registered User
    Join Date
    Feb 2005
    Posts
    5
    Nevermind, I'm dumb. I just found the answer in the tutorial. \

    Thanks andyhunter!

  9. #9
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    So with classes then I put all my variables in one place instead of at the top of my program?....
    You aren't making sense, but that's part of being a beginner. Maybe provide an example of what you mean.
    Last edited by 7stud; 02-11-2005 at 09:57 PM.

  10. #10
    Registered User
    Join Date
    Feb 2005
    Posts
    5
    No I have it all figured out now i think....I was confusing how classes worked with global variables. If I use classes then I don't have to have global variables right?


    thanks for all of the help!!!!!!

  11. #11
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Quote Originally Posted by andyhunter
    By default a structures members are public and a class's members are private. Class's support the concept of inheritance, yada, yada, yada.
    ...so do structs. There is no yada, yada, yada.
    Code:
    #include<iostream>
    using namespace std;
    
    struct A
    {
    	int x;
    };
    
    struct B: public A
    {
    	int y;
    };
    
    int main()
    {
    	B b;
    	b.x = 10;
    	b.y = 20;
    
    	cout<<b.x<<endl<<b.y<<endl;
    
    	return 0;
    }

  12. #12
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    Whoa, whoa, whoa. Look at that, and here I though the whole inheritance thing was brought about with C++. So the new C std does allow for inheritance.
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  13. #13
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    If I use classes then I don't have to have global variables right?
    I would hazard to guess that you shouldn't be using global variables at all as a beginner. Classes are much different than variables of type int, float, string, etc.

    I posted a simple example of inheritance above. Basically, if one class inherits another class, it has all it's variables and functions.

  14. #14
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    So the new C std does allow for inheritance.
    I doubt it. Structs in C++ are classes with public access by default, i.e. they do everything classes do including member functions, constructors and inheritance. In C, they hold only variables/other structs.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 02:14 AM
  2. returning class and struct members
    By simone.marras in forum C++ Programming
    Replies: 17
    Last Post: 03-16-2009, 11:10 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 06:18 AM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM

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