classes
class
struct and class
All these fields are public, i.e. can be changed by the user.
Because of this, we can’t enforce certain behaviors in structs, like avoiding a negative age.
In class:
- users can access the
public - users cannot access the
private
define a class
using .h file!
.h: define functions.cpp: implement logic
.h file:1
2
3
4
5
6
7
8
9
10
11
12class Student {
private:
std::string name;
std::string state;
int age;
public:
/// constructor for our student
Student(std::string name, std::string state, int age);
std::string getName();
std::string getState();
int getAge();
}.cppfile:List initialization constructor(C++11)1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/// implement constructor
Student::Student(std::string name, std::string state, int age) {
this->name = name;
this->state = state;
this->age = age;
}
std::string Student::getName() {
return this->name;
}
std::string Student::getState() {
return this->state;
}
int Student::getAge() {
return this->age;
}destructor:1
2
3/// implement constructor
Student::Student(std::string name, std::string state, int age) name{name}, state{state}, age{age} {}All containers in STL are classes!1
2
3
4Student::~Student() {
/// free/deallocate any data here
delete [] my_array; /// for illustration
}
Container adapters
the container that Packaging a larger range of containers into a smaller range of that
![[Pasted image 20240616210816.png]]
Inheritance
like what mentioned in 61B
implementation in C++:
.h file
1 | class Shape { |
difference between container adapters
- Subclasses inherit from base class functionality
- Container adapters provide the interface for several classes and act as a template parameter.
template classes
this
1 | std::string Point::getColor(){ |
and
1 | std::string Point::getColor(){ |
is the same, but when initializing them?
1 | void Point::setX(int x) |
will crush!
In the setX function we’re changing the member-variable value of x based on some passed in value.
In getX we’re not modify anything, we’re just getting some value.
implementation of template in C++
template: to make the type a parameter
1 | template <typename T> |
while doing this, you also need to #include "Container.cpp" into the .h file.
const interface
Objects that are const can only interact with the const-interface.
The const interface is simply the functions that are const/do not modify the object/instance of the class.
overall example: IntArray
.h file:
1 | class IntArray { |
.cpp file:
1 | IntArray::IntArray(size_t size) : _size(size), _array(new int[size]); |
main:
1 |
|
problem? const IntArray& arr! our at function didn’t provide ways for const!
1 | int& at(size_t index) const { |
Casting: The process of converting types, there are many ways to do this.
const_cast:const_cast<target-type> ( expression )
We can use const_cast to cast away const-ness.
So why is this useful
1 | const int& findItem(int value) const { /// one-liners ftw :) |
- Cast so that it is pointing to a non-const object
- Call the non-const version of the function (this is legal now)
- Then cast the non-const return from the function call to a const versio
