+ 4
When is it appropriate to use struct instead of a class?
Structs and classes
6 Antworten
+ 5
Where any anonymous change in values of data don't have any adverse effects on program logic.
+ 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.
+ 2
Hie Devender
May you just put a simple example to demonstrate
+ 2
thanks really helped
0
when we want to use different data types it is easy to use structures than class.
0