answersLogoWhite

0


Best Answer

The definition of the structure in C is limited to within the module and cannot be initialized outside its scope. Where as in C++ you can initialize the objects anywhere within the boundaries of the project.

User Avatar

Wiki User

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

Wiki User

12y ago

The only similarity between the two is that they both encapsulate member variables.

The difference is that structure members are public by default, while class members are private by default. Moreover, a class encapsulates not only member variables, but also the functions that may act upon those variables, the member methods. Finally, unlike structures, classes can alter their member accessibility (both variables and methods) from private to protected or public on a member-by-member basis.

When objects are instantiated, the accessibility of its class members determines how much or how little of the interface is exposed outside of the class. This helps to ensure an object's data remains in a valid state at all times. The data is the responsibility of the object that encapsulates that data, not the user of the object.

By contrast, a structure's members may be invalidated at any time by any function. Thus onus is upon the user to ensure the data remains valid at all times.

This answer is:
User Avatar

User Avatar

Wiki User

7y ago

In C++, class members are private by default while struct members are public by default. Apart from that they both work exactly the same way and are completely interchangeable.

Note that a C++ struct is not the same as a C struct. A struct in C only has member variables, it does not and cannot have member methods. Moreover, all the member variables are public; they cannot be declared private or protected as they can with a C++ struct.

In C++ we typically use a struct for a POD (plain old data), where the data has no invariant, and use a class when we require a user-defined type that behaves as intuitively as any built-in type. To maintain compatibility with C code, a C++ struct must not have any member methods and must use trivial construction and assignment (e.g.,, member-wise initialisation and copy assignment). However, if used solely in C++, a struct may have more complex construction and assignment methods, but in the interests of consistency should not maintain any invariant.

This answer is:
User Avatar

User Avatar

Wiki User

6y ago

The C++ class and struct keyword operate exactly the same way. The only difference between the two is that a struct's members are public by default, while class members are private by default. The default access can be overridden simply by specifying an alternate access, thus the following class and struct are exactly the same:

struct mystruct {

int m_data; // public by default

};

class myclass {

public:

int m_data; // public by specification

};

Programmers are free to use either the struct or class keyword when declaring classes (both are in fact regarded as classes regardless of which keyword is employed). By convention, programmers use the class keyword to instantiate fully-encapsulated C++ objects and reserve the struct keyword for plain-old data (POD) structures -- public data classes with no methods and trivial construction.

Note that C does not have any built-in concept of object-oriented programming, thus when passing objects from C++ to C, you must pass a POD. Conversely, when passing a C struct to C++, you can either treat the struct as a POD or, preferably, use the struct to construct an encapsulated object or assign the struct to an existing object (the object's class requires a suitable constructor and assignment operator overload in order to achieve this).

If portability between C and C++ is not an issue, then you can use either keyword to define classes, but it's generally best to stick to the established convention and use struct purely for POD structures.

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

A structure in C can only contain data elements and all the elements are public. A class in C++ can contain both data elements and functions along with the ability for those items to be public/protected/private.

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

Structure members are public by default, class members are private by default. That is the one and only difference between the two. Other than that they are both treated as being classes.

struct S

{

int m_data; // public by default

};

class C

{

int m_data; // private by default

};

int main()

{

S s;

C c;

s.m_data = 42; // PASS: S::m_data is public by default.

c.m_data = 42; // FAIL : C::m_data is private by default.

}

To make C the same as S, you must override the default access specifier:

class C

{

public: // override default access specifier

int m_data;

};

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

A C struct has member variables only (no member methods). All members are strictly public.

A C++ class can have both member variables and methods, and access to each member can be public, protected or private.

Note that a C++ struct is not the same as a C struct. A C++ struct is exactly the same as a C++ class, other than the fact that class members are private by default while struct members are public by default. Other than that there is no distinction between the two: both are regarded as classes regardless of which keyword is used. By convention, the struct keyword is used when dealing with C-style structs (usually for portability with C code), while class is used when dealing with fully-encapsulated objects. However, it's only a convention not a standard. Programmers are free to use whichever keyword they feel appropriate.

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

The only difference is that structures use public access by default while classes use private access by default:

class foo{

int data; // private access (by default)

};

struct bar {

int data; // public access (by default)

};

You can, of course, override the default access by specifying public, protected or private access as required.

Aside from that, there is no difference whatsoever: a C++ struct is no different to a C++ class, and both are regarded as being classes.

Note that a C++ struct is not the same as a C struct. However, the default public access allows C structs to be used as the foundation for C++ classes without the need to make any modification. The compiler will automatically generate a member-wise default constructor, copy constructor, assignment operator and destructor but the class will otherwise behave just like a C struct.

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

A C struct can contain only data; a C++ class can contain both data and functions.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Difference between c plus plus class and c structures?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What is the difference between an attribute and a behavior in c plus plus?

An attribute is a class member variable while a behaviour is a class member method.


What is the difference between public and private in c plus plus?

A private member can only be accessed by other methods of the same class, while a public member can be accessed by methods of any class or by non class code.


What is the difference between automatons and kripke structures?

They are veery similar - instead of atomic propositions automatons have letters and they appear on the edges rather than in the states. Plus automatons have accepting states which kripke structures don't.


What is the difference between class data type and basic type in c plus plus .How can someone know which one is class type and which one is basic type.?

Basic types (primitive data types) have no methods associated with them.


What is the difference between private stafford and plus student loans?

What is the difference between private stafford and plus student loans?


What is the difference between a class and a union in c plus plus?

The data members of a class are each allocated separate memory addresses. A union's members are all mapped to the same address. If you change the value of one union member, you change them all.


Defference between c plus plus and c?

these are difference in between c and c++: a) C is a SPL and C++ is a OOP. b) C has not concept of object but C++ has this feature. c) C has not 'class' name data type but C++ has.


What is the difference between pointers in c and c plus plus?

Nothing.


What is the difference between be plus ing and get plus ing?

There are no such terms in C++.


What is the difference between statements in c plus plus charconstp and char constp?

There is no difference. Both statements are invalid.


What are the difference between public and private inheritance in c plus plus?

Public member, fields, methods etc can be accessed from outside of the class. While private members etc can accessed only within the class even "child" classes do not have access to private members, fields etc.


What is the difference between Nike plus and Nike plus Active?

They have different names