English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

3 answers

The struct and a class can be denoted the same way in some context but the only difference between "class" and "struct" is that a struct defaults to having public members (both data members and function members) and a struct defaults to public inheritance. A class defaults to private members and private inheritance.

That is the only difference. This difference can be circumvented by explicitly specifying "public", private", or "protected" so a struct can be made to act like a class in every way and vice versa.

By convention, most programmer's use "struct" for data types that have no member functions and that do not use inheritance. They use "class" for data types with member functions and inheritance. However, this is not necessary or even universallly accepted.

Here is an excerpt from Bjarne Stroustrup's Book
"The Design and Evolution of C++" page 76
Explaining why he chose struct/class as his main design to do the C++ Language!
=========================================
My intent was to have a single concept: a single set of layout rules, a single set of lookup rules, a single set of resolution rules etc. Maybe we could have lived with two set[s] of rules, but a single concept provides a smoother integration of features and simpler implimentations. I was convinced that if _struct_ came to mean "C and compatibility to users" and _class_ to mean "C++ and advanced features," the community would fall into two distinct camps that would soon stop communicating. Being able to use as many or as few language features as needed when designing a class was an important idea to me. Only a single concept would support my ideas of a smooth and gradual transition from "traditional C-style programming," through data abstraction, to object-oriented programming. Only a single concept would support the notion of "you only pay for what you use" ideal.
=========================================

For example, I use structs to denote a collection of objects instead of creating a class for that purpose, I could just make a struct that has 5 fields of different datatypes and treat it as a property like any other datatype. You could do the same with class but it is easier and better written in structs.

2006-07-03 15:53:47 · answer #1 · answered by ? 6 · 2 0

classes have many advantages over structs. often, classes are used to encapsulate (protect) data. for example, consider the following struct

typedef struct mystruct
{
int x;
int y;
};

now consider this class:

class myClass
{
int x;
int y;
};

the difference between the two is that when you declare a mystruct object, the data it contains can be modified by any function. in the case of myClass, since the data is private by default, they cannot be modified by any function except member or friend functions.

there are many more differences, such as overloading, inheritance, etc, but I won't discuss those

2006-07-03 16:02:24 · answer #2 · answered by cybahdawg 2 · 0 0

Coming from a regular background i might tend to confirm gadgets as information with the two variables and purposes. This in fact ability each and each merchandise is in and of itself a regular software in case you go with it to be. It additionally ability in a given software that's way less significant to declare variables first. You declare variables interior gadgets. a number of them may be obtainable outdoors the article. hence you declare some variables once you have declared "techniques" (purposes). and of direction in, for occasion, c++ the line "for (int i=0;i<7;i++) is totally legal and smart. How complicated that's is generally neglected. i've got by no ability made the completed transition to merchandise-oriented from regular and don't opt to.

2016-12-08 15:26:32 · answer #3 · answered by Anonymous · 0 0

fedest.com, questions and answers