We have seen the basic syntax for structs and unions, and the really basic syntax for the C++ class is the same.
struct sStudent {
string name;
int id;
};
class cStudent {
string name;
int id;
};
We are just getting started, and a good place to start is the difference between structs and classes mentioned earlier ...We declare an instance/variable of each type,
i.e. objects, as follows:
sStudent studentA;
cStudent studentB;
But if we try to set the id values ...
studentA.id = 123;
studentB.id = 124;
... the struct object will succeed but the class object will fail.
Members of a class are private by default, whereas they are public by default for a struct.
The access specifiers/modifiers for data and methods have three different types:
Public can be directly accessed from outside the object.
Private can only be directly accessed by internal methods.
Protected can be directly accessed by objects of subclasses, but not by arbitrary external objects.
We will look at protected later, but private and public allow us to capture the idea of encapsulation.
To encapsulate components is to hide them in a container:
Encapsulation is an example of making something a black box.
To access some of the hidden components an interface should be provided.
Often we want users of an object to only see the “surface” of the object.
class Classname {
public:
// assorted user-accessible functions,etc Interfaces
private:
// data (& functions) hidden from the user
};
This is one of our primary design considerations, effectively following the principle of least privilege.
If a data member is private we can properly control it through member functions :
A variable may have a limited range.
Using a private function we can ensure that it always stays within that range after manipulations.
A variable may require specific output formatting.
Again, we can have a public member function for displaying data which uses the appropriate format.
Data might sometimes be public, but usually only if it’s a const, likely a static const.
This makes sense for fixed values: – , e, ħ ...
Private is a compiler setting. It doesn’t actually stop you from interacting with the location...
class thing {
public:
int value1 = 5;
void display(){cout << value2 << endl;}
private:
int value2 = 77;
};
int main()
{
thing A;
cout << A.value1 << endl;
cout << A.value2 << endl;
cout << *(&A.value1 + 1) << endl;
A.display();
cin >> *(&(A.value1) + 1);
A.display();
return 0;
}
We can do something similar to change const.
class thing {
public:
int value1 = 5;
const int value2 = 77;
};
int main()
{
thing A;
cout << A.value1 << endl;
cout << A.value2 << endl;
cin >> *(&(A.value1) + 1);
cout << A.value2 << endl;
return 0;
}
Typically get functions return something without making changes to any values.
If that is the case the function can be const.
This is an instruction to the compiler to say our intent is that nothing is changed.
The syntax is, for example, ...
int function() const;
If you declare a function to be const, it can be called on any type of object.
A function that is non-const can only be called by non-const objects.
Here goes a complete example of a const function in the context of get for a class.