OOP
Classes and Structs
In C++ unlike C structs are the same as a class the only difference is that classes default are private and structs default are public.
class A {
int x;
};Another example would be the complex numbers
class Complex {
double re, im;
public:
//ctor
Complex (double re, double im) {…}
Complex sum (Complex b) {…}
};
int main () {
Complex a, b, c;
};Inline implementation
Every function and ctor is defined in the class or struct
output
outputOutline implementation
The functions and ctors are defined outside the class
output
outputIn the outline implementation first we must delcare the prototype of each function in the class. Observe line 8
Then outside the class or struct we define the function
Notice the the order, return_type class_name::func_name
Note: Inline and outline implementations can be mixed such that one function is defined one way while another in the second way.
Multiple Files using Outline Implementation
TABS Complex.hpp
Complex.cpp
main.cpp
Static
Static members of a class are not associated with the objects of the class. Use the keyword static to define it static.
Static data member
Static member function
Static data member
Static data members are not associated with any object. They exist even if no objects of the class have been defined. There is only one instance of the static data member in the entire program.
Static data members must be delared in the class but defined (given a value) outside the class
It is illegal to define in the class itself such as
The only time we are permitted to define in the class itself is if the variable is const. So the following is permitted
As said befoe they exist even if no objects have been defined. So the following is correct
output
outputStatic member functions
Static member functions are not associated with any object. When called, they have no this pointer.
Static member functions cannot be virtual or const
If line 16 wouldn't be commented out, this would compile. We have stated before that a static function doesn't have this pointer
Arrangement of Classes in Memory
Here is the following in C++
stack
heap
a.re
a.im
b.re
b.im
TODO exam questions
Constructors
MyClass acallsMyClass()the no-arg ctor.MyClass b {5}callsMyClass(int i)ctor.MyClass c {1.0, 0.0}callsMyClass( double x, double y )
No constructor
If no ctor is defined the member fields will be filled with random data.
output
outputNote: If the no-arg ctor would exist Point p1 would call it.
No arg-constructor
If no arg-ctor is defined in the following code the code won't compile.
Point p1 will look and won't find a no-arg ctor such as Point(). The only available ctor is Point(int, int) which is unsuitable.
Functor
A functor (or function object) is a C++ class that acts like a function. This will be explained in greater detail later on.
The code above will not compile because Point p1() will look for a functor. Instead we can replace Point p1() with Point p1{} and the code will call the no-arg ctor and print out x=88
If we were to change the main function to this it would compile.
output
outputArray of Objects
MyClass acallsMyClass()5 timesMyClass b b[5] {11, 22}callsMyClass(int i)2 times andMyClass()3 timesMyClass c[5] {{11,22}, 33}callsMyClass(double x, double y);then callsMyClass(int i)and finally callMyClass()twice
Destructors
Use: Called for:
A stack object – when it goes out of scope.
A heap object – when it is explicitly deleted.
To create a destructor use the tilda symbol before class name ~MyClass
Common Errors:
Forgetting to write a destructor. Might cause a memory leak.
Calling a destructor twice. Possible reason - shallow copy.
Lets look at a memory leak using valgrind
Memory Allocation and Deallocation
In C we used malloc to allocate memory and free to deallocate memory. The problem with using malloc and free in C++ is when we start to use objects.
malloc doesn't call the constructor of the object and doesn't initialize data members either. The same goes for free which doesn't call the destuctor and internal data member are not freed.
In C++ we use the new and delete operators. new will both: 1. Allocate memory 2. Calls constructor
The delete operator will both: 1. Call destructor 2. Free memory
Memory Deallocation of Arrays
Note: using
delete awithout[]may cause a segmentation fault or memory leak
Memory De/Allocation of Objects
Here the destructor
will deallocate for us when the main functions reaches the end of its scope
If we were to replace the main functions will the following:
Or in other word we place the pointer a on the heap then we must deallocate it as well e.g. delete a;
Remember: anything allocated must be also deallocated
We could also do the following:
Memory De/Allocation an Array of Objects without Default Ctor
To top it all off, lets create a array of objects on the heap and not call the default ctor as above
Here we create an array of n pointers to MyClass, no constuctors where called.
Next we will call our fill ctor (with 2 params) and initilize each one with a different size and num to fill in. 0. size of 10 and fill in 3's 1. size of 20 and fill in 6's 2. size of 30 and fill in 9's
Now comes deallocation, each IntArray is freed individually
And finally, we free the pointer to n IntArrays.
Last updated