Whats the purpose of "struct" and "class"? I know one is public and the other is private. Though what does that mean?

+2 Bob huginstien · April 27, 2015
I have seen structs able to give definitions to a single item like for instance cat down there . 

struct Cat 
{    
     string color;
     string type;
     int age;
        etc. }; 

and what does a class look like? 

is there some guidelines when and where to use these items, and what explicitly can go into them  ?  I have read from my book and looked up online, but the definitions are too vague. Please help, thank you!

Post a Reply

Replies

Oldest  Newest  Rating
0 Bob huginstien · April 27, 2015
Thank you everyone!
+2 Dol Lod · April 27, 2015
For structs, without explicitly labelling private variables and functions, everything with the struct is implicitly declared public.

For classes, without explicitly labelling public variables and functions, everything within the struct is implicitly declared private.

Structs are nice when you just want a block of data to work with b/c they can be made really simple, like making a struct of 3 floats. You could use an array, but the struct provides a bit more modification.

Classes are nice when you want at least one variable to be private. Classes could be be more closely considered to resemble having a user interface for some user who wants to only use the class while a developer of the class has full control of the implementation of the methods.

Honestly, it doesn't make much of a difference b/c you typically do want to use public: and private: anyway explicitly within the class or struct for clarification. 

This interpretation is restricted explicitly to C++. I can't speak for other languages. 
+1 Mukul Chauhan · April 27, 2015
Also you can inherit the properties of one class to another but not in case of structures :p
0 Bob huginstien · April 27, 2015
Thank you for all your replies. I have a little more clarity now. 
+1 Number Double07 · April 27, 2015
One main difference between classes and structs is you can call a function within a class.  Also, class is object-oriented.  
+2 Mukul Chauhan · April 27, 2015
class cat
{
 private:
   string color;
   string type;
   int age;
   etc..
public:  
   void getdata()                                                                                        //function to get data from user
         {
            // whatever you wanna do with above vars you declared.
         }  
   void display()                                                                                         //function to display data
          {
             //display
          }
};



in your structure

struct cat a;    

you will now access variables using a.color and a.type etc..


but in class you cannot access variables using class objects.. you can only access the public part using the objects

in class

cat a;
a.getdata();
a.display();

a.color;                     // will give you an error
+1 Tatrasiel R · April 27, 2015
I was going to write a whole bunch of stuff but I think this makes sense for what you're needing.

http://stackoverflow.com/questions/54585/when-should-you-use-a-class-vs-a-struct-in-c

 and this talks about the difference in class types

http://stackoverflow.com/questions/860339/difference-between-private-public-and-protected-inheritance
  • 1

C++

106,931 followers
About

Used in many types of software including music players, video games, and many large scale applications.

Links
Moderators
Bucky Roberts Administrator