answersLogoWhite

0


Best Answer

In C, a struct is simply a type that can hold several sub-objects.

In C++, struct is almost the same as "class". It can have member functions, parent structs and classes, etc. The only difference between struct and class is that the members of class are by default private, while the members of struct are by default public. Thus, a standard C struct is also a good C++ struct - simply one that has no member functions and no parents.

User Avatar

Wiki User

βˆ™ 13y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

βˆ™ 11y ago

The internal members of a struct are the same for both C and C++. However, a C struct has no member methods and is entirely public. A C++ struct, on the other hand, is public by default but otherwise works exactly the same as a class.

Aside from object-oriented nature of the C++ struct, the major difference between the two is in the declaration.In C, you could declare a struct in a variety of ways:

  1. struct foo {...}; struct foo bar;
  2. struct foo {...} bar;
  3. typedef struct foo {...} bar;

The above three lines are all functionally equivalent (although you can't use all three in the same namespace, of course). However, the explicit use of typedef is the preferred method in C, thus allowing new instances of the structure to be created without the need to continually use the struct keyword:

typedef struct foo {...} bar;
foo another_bar;
foo yet_another_bar;

In C++ the use of typedef is not necessary, so the declarations can use the more simplified form:

struct foo {...} bar;
foo another_bar;
foo yet_another_bar;

Note that typedef is not implied, it's simply not required. You can still use a typedef if you want, but it's existence is mostly for backward compatibility with C. In fact, the only time a typedef is ever required is when the tag name or instance name clash with a function name in the same namespace. Although its ultimately better to avoid name clashes altogether, a typedef will resolve the clash.
This answer is:
User Avatar

User Avatar

Wiki User

βˆ™ 8y ago

A struct in C is simply a data record, much like a database record but with member variables instead of fields. They essentially allow a group of related datums to be treated as a single entity. They are often used as the return value from a function because a function can only return a single value. By returning a struct a function can effectively return several values at once. Functions can also be overloaded to either accept multiple parameters or a single struct. Structs also make it possible to implement more complex data structures, such as linked lists and trees.

A struct in C++ is exactly the same as a class in C++. The only real difference between the two is that struct members are public by default, whilst class members are private by default. Other than that, anything you can do with a class you can also do with a struct. However, most C++ programmers use them exactly as they would in C (with no member methods and a purely-public interface to the member variables), and derive a class from a struct whenever they need to instantiate full-blown objects from the struct. This convention is common within Microsoft Windows software, where a CRect class is derived from a RECT struct. While this convention allows far greater interoperability with C programs, it's hard to justify the use of non-encapsulated structs within an object-oriented programming environment, especially when you are then forced to provide two separate implementations to cater for what is essentially the same data type.

This answer is:
User Avatar

User Avatar

Wiki User

βˆ™ 7y ago

In C a struct is used to define a "plain old data" structure (a POD) but in C++ a struct is simply an alternative way of defining a class. That is, the struct and class keywords are interchangeable in C++. The only real difference is that struct members are public by default while class members are private by default. As such, the following declarations are equivalent:

// C or C++

struct X {

int data; // public access by default

};

// C++ only

class X {

public:

int data; // explicitly public

};

The same applies to inheritance in C++. A struct uses public inheritance by default while a class uses private inheritance by default. Given the following base class...

// C or C++

struct B {/*...*/};

...the following definitions are equivalent:

// C++ only

struct D : B {/* ...*/ }; // Public inheritance by default

class D : public B {/* .... */}; // Explicit public inheritance

By convention we use a struct in C++ just as we would a struct in C, to store plain old data that is publicly accessible, requires trivial construction and is compliant with C code. For example:

// C and C++

struct Point {

int x;

int y;

};

This structure has no invariant (all integer values are valid for both x and y) so there's really no need for any unnecessary constructors or assignment operator overloads. The structure supports trivial construction and member-wise initialisation in C++, just as it does in C.

This answer is:
User Avatar

User Avatar

Sameera Banu

Lvl 5
βˆ™ 1y ago

If you are asking any of the above questions, well you have come to the right place. In this tutorial, you will learn the difference between C structures and C++ structures. First, let’s see the comparison chart for C and C++ structures, then I will discuss the difference with some programming examples.

visit our page : livewiretambaram .com/

This answer is:
User Avatar

User Avatar

Wiki User

βˆ™ 9y ago

There is no difference. C and C are the same language.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is the difference between struct in c and c plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is the name of the structure type in C plus plus?

The same as in C, struct.


What is the difference between destructors in c plus plus and c sharp?

In C# only class instances can have a destructor, whereas both class and struct instances can have a destructor in C++. While syntactically similar, a C++ destructor executes exactly as written, whereas a C# destructor merely provides the body of the try clause of the class' finalize method.


Difference between void and devoid in c plus plus?

There is no such thing as devoid in C++.


What is a class in c plus plus how does it compare with structure in c?

A struct in C is a POD (plain old data) type. A class in C++ can also be a POD for backward compatibility with C code, but more typically combines data with methods that operate upon the data, including constructors and initialisers. Classes in C++ can be declared using either the struct or class keyword. By convention you will typically use struct to define POD data types and class to define more complex data types. The only practical difference between a struct and a class is that classes use private access and inheritance by default while struct uses public access and inheritance by default. Thus the following two class definitions are functionally the same: struct A { A():m_data(0) {} // public access by default private: int m_data; }; class B { int m_data; // private access by default public: B():m_data(0) {} };


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

There are no such terms in C++.

Related questions

Similarities between C plus plus struct and class?

The only difference between a struct and a class in C++ is that struct members are public by default while class members are private by default. Other than that they act and behave in exactly the same way and are both used to define classes. By convention, struct is used exactly as it would be used in C, with all public members and no member functions, to provide backward compatibility with C-style code.


Sample code of typedefstruct in c plus plus?

You don't need a typedef to declare a struct in C++. A struct is declared exactly the same way that you would declare a class, the only difference being that struct members are public by default, while class members are private by default. Aside from that the class and struct keywords are completely interchangeable.


What is the name of the structure type in C plus plus?

The same as in C, struct.


What is the difference between a structure in C and a structure in C-plus-plus?

Not much. C++ structures are the same, i.e., still declared with "struct".. Are you sure that you didn't mean to ask: "What's the difference between a structure and a class?" Cause, that's the main (DRUMROLL PLEASE) power of C++, among other things. I think there is a difference in them. In struct of C there are no Member functions allowed .( you can use function pointer but not the definition in the structure ). But in C++ you have that flexibility . A C++ struct is exactly the same as a C++ class, except that struct members are public by default while class members are private by default. Access to individual members can be overridden in the declaration thus either can be used for the same purpose. C++ programmers use classes to define most object types and reserve the struct type for backward-compatibility with C.A C struct has member variables but has no methods. Accessibility cannot be defined, thus all members are effectively public at all times.


Demonstrate single inheritance in C plus plus?

struct base1 { // ... }; struct base2 { // ... }; struct derived1 : public base1 // single inheritance { // ... }; struct derived2 : public base1, public base2 // multiple inheritance { // ... };


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

Nothing.


Write a c plus plus programme to illustrate single inheritance?

struct A {}; // base class struct B : A {} // derived class (single inheritance).


What is the difference between struct and union in c file management?

The main difference is in how the data structures are stored. In a union, all of the elements are stored in one location. A structure stores each of its elements in a separate memory location.


What is the difference between destructors in c plus plus and c sharp?

In C# only class instances can have a destructor, whereas both class and struct instances can have a destructor in C++. While syntactically similar, a C++ destructor executes exactly as written, whereas a C# destructor merely provides the body of the try clause of the class' finalize method.


Difference between void and devoid in c plus plus?

There is no such thing as devoid in C++.


Can you give an example of a structure in C plus plus?

struct point { int x; int y; };


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

There are no such terms in C++.