Which one is best approach class or structs? In c++ [duplicate] - c++

This question already has answers here:
When should you use a class vs a struct in C++? [duplicate]
(27 answers)
Closed 4 years ago.
Suppose you have to develop an application for XYZ bank with the following features.
1)The application must be secure
2)For transactions, proper interfaces will be provided to the customers.
3)The application must be reusable.
4)The application must be efficient in terms of speed and memory usage.
You can use either structs or class in order to achieve the above mentioned features. So which programming construct (class or struct) will you select for application development.
Please help me out here. Thanks

struct and class are nearly equivalent in C++ (you can have member functions, constructors & destructors, data members in both). More precisely,
struct Sometype {
/// some code here
};
is equivalent to
class Sometype {
public:
/// some code here
};
So the runtime efficiency is the same (since public: is an annotation for the compiler which is lost, as most type information, at runtime; be aware of type erasure).
You really should take days to read some good book about C++ programming, then look into some C++ reference site, then read (or at least refer to) some C++ standard like n3337 (for C++11; for later standards, find them by yourself).
Learn about the rule of five and about standard containers and smart pointers.

Related

Why does keyword struct in C++ differ from keyword struct in C? [duplicate]

This question already has an answer here:
Why does struct design to be expanded leading it much like class?
(1 answer)
Closed 4 years ago.
From a design perspective, one could have added the class keyword to C++ to realize the desired OO principles like encapsulation, inheritance, polymorphism as well as constructors and destructors etc ... but at the same time leave the keyword struct just like it is defined in C and thus be able to a) compile legacy C code and b) define simple PODs.
I cannot see the reason for making a struct behave (almost) like a class in C++.
Or in other words: Why didn't they just leave the C struct keyword alone in C++?
Any hints ?
I cannot see the reason for making a struct behave (almost) like a class in C++.
One of the reason - provide ability to inherit from a struct adding constructor/destructor etc and still it can be passed to C functions as C structure.
and thus be able to a) compile legacy C code
But the way classes are specified, you can compile legacy C struct definitions in C++ (except for potential backward incompatibilities that aren't related to structs, such as the newly added keywords).
b) define simple PODs.
The ability to define simple PODs wasn't prevented by the OOP features that C++ classes introduced. You can define POD classes in C++.
Both a) and b) were achieved without separating the meaning of struct and class, so you have not demonstrated an argument for doing so.
The concept of object oriented class is an extension of the concept of composite datatype, which is what C structs are. There was no necessity to complicate the language by adding a separate definition for C structs since C++ classes are sufficient for that purpose. Simplicity is a great trait to strive for in a language - especially when the language is so very lacking in that regard.

When do we need to use a structure in a pure C++ program? Is structure at all required in a pure C++ program? [duplicate]

This question already has answers here:
When should you use a class vs a struct in C++? [duplicate]
(27 answers)
C++ - struct vs. class [duplicate]
(5 answers)
Closed 9 years ago.
When do we need to use a structure in a pure C++ program? Is structure at all required in a pure C++ program?
I understand that structure is the only way in a C program to encapsulate members and function pointers. But, if I use C++ to code my complete module, is it 100% alright if I stay away from structures and use a Class instead? Am I missing something?
(I googled but my question is very specific - is structure useless in pure C++ code)
=== EDITED ===
My question is not about the difference or commonalities between a structure and a class, but, do we need "struct" at all in C++, when you can 100% do away with a class. Is structure not redundant construct in C++, except for backward compatibility with C? I understand the difference between them w.r.t. the access specifiers but there are only syntactic differences.
The struct has been kept in C++ for backward compatibility with C. By default the struct members are public but the class members are private. You can replace class and struct with each other and get the same result if the type of members are pre-defined.

Speed of data access in Structs vs Objects, C++ [duplicate]

This question already has answers here:
Performances of Structs vs Classes
(7 answers)
Closed 9 years ago.
I was wondering if there is a difference in the running time of accessing data from a Struct and accessing data from an Class in C++. In my current understanding, a Struct is a class but with no member functions. I have some code which runs much more slowly with objects than with structs, which seems to suggest that there is a difference, but this confuses me as my intuition tells me that there shouldnt be a large difference.
As per " What are the differences between struct and class in C++? ", quoting from C++ FAQ
struct and class are otherwise functionally equivalent
(aside from minor private/public/protected defaults).
So there's no difference.
It is all the same, whether it is a struct or a class (I assume that it what you mean by object, since any instance is technically an object, be that a primitive, a struct or a class), the compiler does the same offset calculation to access members. There isn't really that much of a difference between the two - they may look different in terms of syntax, but the C++ call someObject.someMethod() is equivalent to the C version SomeObject_someMethod(&someObject) which is what a member call really is under the table. But this only concerns member methods, the access speed to members of classes and structs is practically identical.
I don't know why you would have that problem of different running times.
Structs and Classes, I'm assuming thats what you mean by objects, are virtually the same other than structs defaulting to public and classes defaulting to private.
So to sum it up there should be no difference functionally or speed wise
EDIT: the reason you may be experiencing structs running faster is because people tend to use structs for POD's whereas classes have more functionality, however this is based on style not actual the functionality of structs and classes
At runtime execution there is no difference in speed of execution.
you can refer similar threads.
Performances of Structs vs Classes
Class vs Struct for data only?
in C++ there are 3 kind of objects that belong to the class type:
struct
class
unions
they are functionally equivalent to each other, so you should expand more and tell more about your implementation.

C++ Structs - Pointless? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What are the differences between struct and class in C++
I'm just trying to figure out if using 'C structs' in C++ are essentially useless or not. Do you gain anything by using them (opposed to simply creating another class)?
In C the point of structs is obvious, simply contiguous allocations of grouped data and a nice way to access said data, in C++ I feel the role becomes a little more vague.
Seeing as that you can have functions which are members of structs, instance variables, and visibility labels, the only real difference that I see between structs and classes in C++ is that struct members default to public while class members default to private. The way I see them, they can actually both be implemented with the same exact underlying system.
So am I missing something here as to the purpose of structs in C++? Or have they kind of lost their purpose, as I feel they have in C++?
In my opinion, Structures doesn't give you anything that can't be implemented by a class.
But, Structures are kept in C++ in order to have backward compatibility with c
Classes and structures in C++ are almost the same, the only things that separates them is that the default visibility of a class is private while in a struct it's public.
The reason to keep having structures in C++ was probably to keep the language compatible with C, so it would be easier to port C code to C++, or for programmers to make that transition.
As for the usage of structures contra classes, I personally use them just like would in C, to group related variables together in a single "object".
Undoubtedly the reason they are there is for compatibility with C. But you are right, although there are small differences between class and struct in C++ there is nothing you can do with structs that you cannot do with classes (and vice versa).
Personally I use structs only when the same declaration would be legal in C, to emphasize the C-like nature of whatever it is I'm doing.
They are implemented "with the same exact underlying system". In fact, you can actually declare a type using class keyword and then define it using struct keyword. Basically, they are exactly the same aside from the conceptual difference you already mentioned: default access rights.
I don't see though why would one call them "useless". The usage of the keyword became a matter of personal preference and/or coding standard. Like "use struct fro POD types", or "use struct for types with no incapsulation/access control".
With the same degree of success one can declare the built-in -> operator "useless" because of its equivalence to *+. combination.
You can have the same behavior as in C, nobody forces you to use the additional "class" features. So if you understand their purpose in C, it should be understandable in C++ as well (the "C-like" portion at least).
As Joachim Pileborg already pointed out, classes and structures are almost the same thing in C++.
I would, however, recommend to use struct for "dumb" data holders with public members and class for properly encapsulated classes as a style convention.

Structures vs objects C++ [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What are the differences between struct and class in C++
C++ - struct vs. class
How are structures different from objects and classes in C++? Is there any performance benefit to using objects or structures?
Structures and classes are identical; the language standard uses the term "class" to refer to both. The only difference between defining a class using the struct or the class keyword is the default accessibility of members and base classes; there is no difference in their runtime behaviour or performance.
An "object" is a run-time instance of a type. In C++, the term is used for instances of any type, including classes and fundamental types.
main thing is by default variables are private in a class and by default they are public in structures. also you can google this and find a million and one topics on this. for example http://blog.stevedoria.net/20050913/differences-between-cpp-classes-and-structs