answersLogoWhite

0


Best Answer

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.

User Avatar

Wiki User

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

Wiki User

11y ago

Structures and classes are very similar, the only real difference being that structure members are public by default, while class members are private by default. There is no real difference in terms of performance between a class and a structure, so there are no real advantages in using a structure rather than a class. However, some functions (especially C functions) will only accept a structure, so there will be times when you need to typecast an object as a structure. So unless there is a specific need for a class, you might as well use a structure to avoid typecasting. However, in mission critical code where member integrity must be assured at all times, classes are much safer than structures, so typecasting between the two is a price worth paying.

This answer is:
User Avatar

User Avatar

Wiki User

8y ago

A structure is an aggregate of one or more data types. Unlike an array, where all elements must be of the same type, a structure's members need not be of the same type.

In C++, a structure (or struct) is a class where all members are public by default (class members are private by default). By convention, we use a structure to represent a "plain old data" type (a POD), just as we would in C. If we want to represent something more complex, we use a class. Unlike a structure in C, a structure in C++ can also have member methods, constructors and operators. However, such structures are not compatible with C and are (usually) best avoided.

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

A structure in C++ is a collection of data elements that the programmers decided were logically related to one another in some fashion, and that should be capable of being manipulated as one object, such as a record in a database.

Another way to look at this is that in C++, you also have classes. A structure is a class, but it has no methods, and its default access method is public instead of private.

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

You use a structure just as you would use a class in C++. The only real difference between the two is that a structure's members are public by default, while class members are private by default.

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

Structures are essentially the same as classes in C++. The only real difference is that structure members are public by default while class members are private by default. The struct keyword was inherited from C, but the two are not the same, since a C-style struct has no methods, constructors or operators, it can only contain public member data.

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

Although C++ was designed with OOP in mind, there is no regulation that says you must use OOP in C++. OOP is merely a programming aid, not a requirement of C++. While it's fair to say that if you don't use OOP at all then you might as well stick with ANSI C, many programmers, myself included, use a combination of C/C++ code even in the same program. Ultimately, if a structure is sufficient for your needs, then why go the extra mile designing a class interface? There's no point creating classes just because we can -- we only create them when there is a specific need for them.

This answer is:
User Avatar

User Avatar

Wiki User

9y ago

The struct keyword is primarily used to either declare a class that is defined in C code, or to define a C++ class that is backwardly-compatible with C code.

The only real difference between a struct and a class in C++ is that all members of a struct are public be default whereas all class members are private by default.

C code obviously has no understanding of classes, but it does understand C++ POD classes. A POD class is a plain old data type; a class that has public data members only. A POD may include constructors but they must be trivial -- they must not acquire resources or define invariants. A POD can also inherit from another POD, but it must use trivial inheritance.

A struct in C++ is also a class, but if we want a C++ class to be backwardly compatible with C we must use the struct keyword and define the class with POD-like behaviour. By the same token, if we want to use a struct defined in C, it will automatically have POD-like behaviour. We must also forward-declare the POD in C++ using the struct keyword.

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

Yes. They can be used to provide public structured data for backward compatibility with C, because C has no concept of classes.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What are the advantages of structure in c plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Basic control structure available in c plus plus?

The basic control structure in C++ is the if statement.


Advantages of manipulator in c plus plus?

theriyadhu


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

The same as in C, struct.


Do I need structure of turbo c plus plus?

Yes.


What is the price of the book data structure using c and c plus plus by tanenbaum?

225 Rs. after discount........


Advantages and use of fumctions in c plus plus?

Functions allow to make your program modular meaning consisting of little blocks. It's easier to read and update program which has a modular structure, then just row code in one piece.


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

struct point { int x; int y; };


What is the general structure of a C plus plus Language?

The central feature of any C++ program is classes which can be used to express ideas directly in 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.


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 the difference between c and c plus plus?

The fundamental difference is that in C++ object-oriented programming (OOP) was added. C is a procedural language (that means. top-down structure design), where as C++, which is an extension of C itself, is an object oriented language.


Advantages of C over C plus plus and java?

C can be faster than C++ programs, and definitely faster than Java, since Java is primarily interpreted. C is also somewhat less rigid in definitions as well, not as tightly structured as either C++ or Java can be.