L07 Constructors, Destructors, Dynamic Memory Allocation

#ece244

constructors

Recall from L06:

A constructor is a function that always gets called when a new object of a class is created.

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
Creating a pointer to an object does not create it

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
}
Function overload

Multiple functions with the same name but different parameters

You cannot switch the order of the parameters in the class declarations and constructors

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

Destructor:

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
Benefit of destructor

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

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.

To deallocate memory stored on the heap, we use delete.