MyClass a;
a.data_a = 1;
a.data_b = 0.35;
This code will throw compilation errors!
---
# Scope Protection
By default, `struct` has `public` visibility and `class` has `private` visibility.
- We can specify the visibility of class members via `public`, `private`, and `protected` modifiers.
- We define "zones" of visibility in C++ rather than individual values.
- Start with public: most interesting part for users of class
---
# Scope Protection (Cont.)
```cpp
class MyClass
{
// This value is private.
int x;
public:
// The following are public.
some_method() { }
float n;
protected:
// The following are protected.
string _str;
private:
// Private again.
int _val;
};
```
Often, in C++, private and protected members (attributes and functions) will have an underscore before their name.
---
# Defining Attributes
- Attributes are the values that go along with our objects.
- By convention, attributes are define as private members.
```cpp
class MyClass
{
private:
// Object (instance) values.
float _x; // uninitialised value
float _y = 0.5f; // Initialised value
const _string name; // Constant value
// Class (static) values.
static _int n;
};
```
---
# Defining Methods
- Methods are the modifier or accessor of the object
- **Note** Generally, the naming convention for variables, methods and functions is *snake_case*.
```cpp
class MyClass
{
public:
void do_something()
{
// Do something, that may change any member variables of this object.
}
//getters
float get_x(){return _x;}
int get_n(){return _n;}
//setters
void set_x(float x){_x=x;}
void set_n(int n){_n=n;}
private:
float _x;
int _n;
};
```
---
# Defining Constructors
- Constructors define how an object is instantiated.
- We can use them to control initialisation of an object
- We can have multiple constructors for a class, using different parameters
- **Note** - always initialise all your variables, either when declaring them or in a constructor
- **Note** - add a print statement in a constructor, and observe all the times they're called!
```cpp
class MyClass
{
public:
// Default constructor
MyClass() { }
// Parameterised constructor
MyClass(float x, float y)
: _x(x), _y(y) // Sets object attributes
{
}
};
```
---
# Defining Destructors
- Destructors determine how an object is destroyed when it goes out of scope.
- A destructor is called:
- whenever an object goes out of scope (i.e. defined between curly brackets).
- When the object is manually deleted (e.g. replacing a variable, deleting a pointer)
- A destructor looks like a constructor with a tilde (~) in front of it, and no parameters
- A class can only have one destructor.
- **Note** - add a print statement in a destructor, and observe all the times they're called!
---
# Destructors in C++
```cpp
class MyClass
{
public:
// Default Destructor
~MyClass()
{
// Free up resources.
}
};
```
---
# Headers!
Remember header files?
**Header files for definitions and Source files for implementation**
```cpp
//my_class.hpp
class MyClass
{
public:
// Default constructor
MyClass();
// Parameterised constructor
MyClass(float x, float y);
private:
float _x, _y;
};
```
```cpp
//my_class.cpp
#include