L07 Constructors, Destructors, Dynamic Memory Allocation
constructors
A constructor is a function that always gets called when a new object of a class is created.
- Must be public
- No return type (not
double, notvoid, etc.) - Name is exactly the same as class name
- Parameters do not need to be passed through the constructor, but can be if needed
Example: no parameters passed
//student.cpp
#include "Student.h"
Student::Student() {
ID = 0;
name = "";
}
// when main.cpp creates a Student object, the constructor is called
E.g. Student &Z; does not initialize the object, it just creates a pointer meant to point to an object, but the object itself has not yet been specified. Thus, the constructor is not called.
z = &x then assigns the object x to the pointer z, so z points to x.
example: class with multiple constructors
If we want to have a constructor that actually performs logic, we can create a second constructor that has parameters in the class.
//student.h
class Student {
private:
int ID; string name;
public:
Student(); // default constructor
Student(int id); // 2nd constructor
Student(int id, string name);
}
//student.cpp
#include "student.h"
Student::Student() {
ID = 0;
name = "";
}
Student::Student(int id) {
ID = id;
name = "";
}
Student::Student(int id, string n) {
ID = id;
name = n;
}
//main.cpp
#include <iostream>
#include "student.h"
using namespace std;
int main(void) {
Student x; // no paramaters passed, so only default constructor used
Student y(781); // 2nd constructor used
Student z(203, "ookla"); // 3rd constructor used
}
Multiple functions with the same name but different parameters
This is how C++ knows what variables you are passing through.
very important!
If the default constructor is not implemented, but other constructor(s) are, then you can't use Student x (default constructor) as it is not going to be given by default.
lifetime of an object
- Constructor is called to initialize the object data members - called when the object is created
- Destructor is called when:
- Object goes out of scope
- Object is freed if it was dynamically allocated
Destructor:
- given to all classes by default
- automatically called when the object should be erased from memory
- empty if not defined
- do not have any parameters passed to them; there is only one destructor for a class
- must be public to be callable outside of the class
example
// student.h
class Student {
private:
int ID; string name;
public:
Student();
~Student(); // this is a destructor; always nemed after the class name
}
//student.cpp
#include "Student.h"
Student::Student() {
cout << "Constructor" << endl;
}
Student::~Student() {
cout << "Destructor" << endl;
}
// main.cpp
#include "Student.h"
#include <iostream>
using namespace std;
int main(void) {
Student x; // constructor called
if (Student y) {
// body of if statement
} // once body of if statement is completed, destructor of y is called
return 0;
} // scope of x ends, so destructor automatically called
if a class dynamically allocates space in memory, destructor can free up that memory when the object is no longer used, preventing memory leaks
recall: dynamic memory allocation
- Stack stores all local variables of functions
- Heap stores all dynamically allocated data
See: APS105 L19 - Dynamic Memory for review of dynamically allocated memory in C.
Instead of using malloc like in C, we use new to dynamically allocate memory. This returns the address of the member stored on heap.
p = new int;allocatespto the heap, andpreturns that address in memory*p = 20;dereferencespand assigns the value of 20 to that address
To deallocate memory stored on the heap, we use delete.