answersLogoWhite

0


Best Answer
A C struct only has public member variables whereas a C++ class combines member variables with member functions; the methods that operate upon the data. Moreover, each member of a class can be assigned a different level of access from public, protected or private access, thus limiting the member's exposure. This allows classes to hide data and implementation details from outside of the class, exposing only as much as is necessary in order to use the class. Thus the class becomes entirely responsible for the integrity of its data, while its methods act as the gatekeepers to that data.


Note that in C++, a struct is exactly the same as a class, other than the fact that the members of a struct are public by default while members of a class are private by default, unless explicitly declared otherwise. Aside from that they operate in exactly the same way. In other words, a C++ struct is not the same as a C struct.



User Avatar

Wiki User

11y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

11y ago

There is no advantage because C has no classes. Only C++ has classes.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What are the advantages of a class in C over a structure in C?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is the advantages of storage class in c?

advantage of storage classes


What are the advantages of structure in c plus plus?

They are primarily included to retain compatibility with C. However, structures are public by default. So if you don't require the data hiding capability of a class, a struct is a suitable alternative. Also, when mixing C and C++ code, there may be a requirement to typecast a class to a struct and vice versa.


Basic structure of c n c plus plus?

The basic structure of a C or C++ program is built around types. A structure is a type. A function is a type. A class is a type. All of these types can be built from primitive (built-in) types and can be used to create ever-more complex types.


Definition of structure in c plus plus?

A structure is a collection of variables referenced under one name, providing a convenient means of keeping related information together. A structure declaration forms a template that may be used to create structure objects (that is, instances of a structure). The variables that make up the structure are called members. (Structure members are also commonly referred to as elements or fields).


What is the Need for structure in c plus plus?

There is no pressing need for structures in C++. Anything you can do with a structure you can also do with a class. The only real difference is that structure members are public by default, whereas class members are private by default, but they both work exactly the same way otherwise. The struct keyword is retained for backward compatibility with C, but a C++ struct is not the same as a C struct unless it is has no interface and all the member variables are implicitly public. Most C++ programmers will use structures to differentiate simple data structures (values) from fully-encapsulated classes (objects). The complexity of the data and its interface is usually the deciding factor in whether to use a class or a structure. For trivial classes the time and effort that goes into developing the interface is often unnecessary so a structure will often suffice. But if the class is to be distributed to third parties then it's better to provide a more robust interface, even for trivial classes, and to avoid C-style structures entirely.

Related questions

What is the advantages of storage class in c?

advantage of storage classes


Write a program to show array within class in c?

class is defined in c++ my dear so in C there is no structure of class.for c++ class private { int x[10],y; cout


Why classb is preferred over class c modulation?

That depends on the use. Each has advantages in different applications. Without more specifics on your application I cannot comment usefully.


What are the advantages of structure in c plus plus?

They are primarily included to retain compatibility with C. However, structures are public by default. So if you don't require the data hiding capability of a class, a struct is a suitable alternative. Also, when mixing C and C++ code, there may be a requirement to typecast a class to a struct and vice versa.


What are the differences between a C structure and C class?

The main difference between a class and a structure is that structures are always public whereas classes are private by default. Classes give greater control as the interface can be engineered such that only code that requires access to specific class members gains that access. Everything else can be hidden within the class itself. Note that C does not support classes, period. Classes are only supported by C++. However C++ also supports C structures for backward compatibility with C-style code.


What are the Advantages of c over c plus plus?

There are no advantages of C over C++ as such. Everything you can do in C you can also do in C++. However, by taking advantage of C++ object oriented programming, generic programming and template meta programming as well as C-style coding, you can produce more efficient machine code far more easily and more quickly than with C alone.


Why piping class C is not used?

Because piping class B was over populated, they needed a new piping class. For some reason they decided to skip C and go straight to piping class D


Basic structure of c n c plus plus?

The basic structure of a C or C++ program is built around types. A structure is a type. A function is a type. A class is a type. All of these types can be built from primitive (built-in) types and can be used to create ever-more complex types.


Definition of structure in c plus plus?

A structure is a collection of variables referenced under one name, providing a convenient means of keeping related information together. A structure declaration forms a template that may be used to create structure objects (that is, instances of a structure). The variables that make up the structure are called members. (Structure members are also commonly referred to as elements or fields).


The fields in a c program are by default private is true or not?

I guess you mean C++, not C.Data fields of a structure/union are public by default,those of a class are private by default.


What is the difference between structure and class with example?

Structures are public by default whereas classes are private by default. Other than that they are exactly the same. struct s { int num; // s.num is accessible outside of the structure }; class c { int num; // c.num is only accessible to the class itself. };


When class will be used and when structure will be used?

In class default members are private and in structure default members are public ,When ever you want to hide data from outside functions then you can use class.But in ANSI C we can hide data by using private access specifier.