From the course: C Essential Training

Unlock the full course today

Join today to access over 23,200 courses taught by industry experts.

Function members

Function members

- [Instructor] C classes can have member functions that act as methods for the class. This is class.cpp from chapter seven of the exercise files. This is a very simple class with one data member. The data member c1val is private by default, that's by design because it's considered best practice to encapsulate object data and to use member functions to access the object data. So there's a couple of member functions, setVal and getVal, which set the value of the private data member and get it respectively. Down here in main we create an object. We use setVal to set the value of the object and we use getVal to get the value of the object and we can build and run this. And you see that our value is 47 as expected. There's an important effect that happens when we const qualify an object. So here we have an object, 01, which is not const qualified. So it's considered mutable as we say. So if I want to create a separate one…

Contents