"Should we use classes instead of structures in C++ ? "
It doesn't make a difference, as both struct and class are equal, despite the default access level. And yes, struct is part of the backwards compatibility with C, but is allowed to behave like class.
What I tend to do is used struct for interfaces & PODs, and class for representing a physical object.
#include<iostream>
usingnamespace std ;
struct Value1{
private :
int a;
public :
void setA(int b){
a = b ;
}
int getA(){
return a ;
}
};
class Value2{
private :
int a ;
public :
int getA(){
return a ;
}
void setA(int b){
a = b ;
}
};
int main(){
Value1 value ;
value.setA(34);
cout << "The value using structure is " << value.getA() << endl;
Value2 obj;
obj.setA(20);
cout << "The value using class is " << obj.getA() << endl;
return 0 ;
}