+ 4

When is it appropriate to use struct instead of a class?

Structs and classes

29th Mar 2017, 6:15 AM
Michael Musora
Michael Musora - avatar
6 Answers
+ 5
Where any anonymous change in values of data don't have any adverse effects on program logic.
29th Mar 2017, 7:45 AM
ā¤ĻāĨ‡ā¤ĩāĨ‡ā¤‚ā¤ĻāĨā¤° ā¤Žā¤šā¤žā¤œā¤¨ (Devender)
ā¤ĻāĨ‡ā¤ĩāĨ‡ā¤‚ā¤ĻāĨā¤° ā¤Žā¤šā¤žā¤œā¤¨ (Devender) - avatar
+ 4
Let us first define the terms class and structure A class in C++ can be defined as a collection of related member variables and methods encapsulated as a single unit. A structure can be referred to as a user defined data type which allows to combine data items of different kinds. Structures are used to represent a record, Major differences 1) By default, members of a struct are public and same of a class are private Example struc sfoo { char status; // status is public }; int main(){ sfoo t; t.status = 'y'; //Okay since access is public. } class cfoo { char status; // status is private }; int main(){ cfoo t; t.x = 'y'; //compiler error: access to t::status is not allowed } 2. Class comes in where purpose is of data abstraction and further inheritance where as structures are meant for data grouping. 3. Structures are best suited for handling smaller amounts of data and classes for handling large amounts of data more securely. 4. Structures are more of value type where as class are of reference type.
29th Mar 2017, 11:29 AM
ā¤ĻāĨ‡ā¤ĩāĨ‡ā¤‚ā¤ĻāĨā¤° ā¤Žā¤šā¤žā¤œā¤¨ (Devender)
ā¤ĻāĨ‡ā¤ĩāĨ‡ā¤‚ā¤ĻāĨā¤° ā¤Žā¤šā¤žā¤œā¤¨ (Devender) - avatar
+ 2
Hie Devender May you just put a simple example to demonstrate
29th Mar 2017, 8:11 AM
Michael Musora
Michael Musora - avatar
+ 2
thanks really helped
30th Mar 2017, 11:37 AM
Michael Musora
Michael Musora - avatar
0
when we want to use different data types it is easy to use structures than class.
30th Jul 2020, 3:46 PM
Ambati Veda priya
Ambati Veda priya - avatar
0
30th Jul 2020, 3:46 PM
Ambati Veda priya
Ambati Veda priya - avatar