Hi all,
I have a confusion, can anybody clear it?
i want to know actually is it possible to inherit structure in c++ or can we perform polymorphic operations on structure in c++.
Hi all, can anyone clear my confusion why we do not use structure in c++ in place of class? Structure also provide us inheritance and polymorphism and can also be used in place of class then why we prefer class in place of structure.
There must be some benefit using it.What are those?
The default accessibility for a class is private while a structure is public. Classes can define behavior and attributes while structures only define attributes. And classes are passed through reference while structures are passed through value.
Thanks, I accept it but structures can also be used to define behaviour in c++. I accept this thing that classes can be refrenced but structure do not.
Is there any other important difference between these?
A structure is just data. It can hold a pointer to a function but you don't define behavior in them.
A class defines data and King Coder said and in it you define "functions" or methods that encapsulate the behavior of the resulting objects.
I suppose that you could define a data only class, but I don't know how practical that would be unless it was embedded in another class. But then you'd probably use a structure.
The way to really tell the difference is that you can define a class to do something to something else. A structure can only have things done to it.
__________________
"Things are difficult only while you don't understand them."
A structure is just data. It can hold a pointer to a function but you don't define behavior in them.
A class defines data and King Coder said and in it you define "functions" or methods that encapsulate the behavior of the resulting objects.
I suppose that you could define a data only class, but I don't know how practical that would be unless it was embedded in another class. But then you'd probably use a structure.
The way to really tell the difference is that you can define a class to do something to something else. A structure can only have things done to it.
not true... a structure and class function exactly the same except for the default access. structure being public by default, and class being private by default.
The default accessibility for a class is private while a structure is public. Classes can define behavior and attributes while structures only define attributes. And classes are passed through reference while structures are passed through value.
Structures can still be used in C++.
You mentioned earlier that classes are passed by reference, while structures are passed by value. You might be thinking of C# or Java (I don't think Java has structs though) because everything in C++ is passed by value into a function unless otherwise noted with an ampersand (making it pass by reference).