answersLogoWhite

0

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

12y ago

What else can I help you with?

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.


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).


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.


Structure in c plus plus?

Structures are the same as classes in C++. The only difference is that structure members are public by default while class members are private by default, but both define a class of object and both are initialised via a class constructor initialisation list. struct my_object { my_object(const int data): m_data(data) {} private: int m_data; }; The above struct is no different to the following class: class my_object { int m_data; public: my_object(const int data): m_data(data) {} };

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


What class address is 192.168.1.21?

Class C.


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.


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.


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. };