Array of Pointers to Pointers

Can we create arrays of pointers to integers?

Can we create arrays of pointers to objects?

Dynamically Allocating Array of Pointers to Ints

// step 1: initialize the pointer
int** arr2p; // a pointer to a pointer-to-int
arr2p = new int*[4] // created an array of 4 pointer-to-ints

// step2: create integers for each element of the pointer array
for (int i = 0; i < 4; i++]{
	arr2p[i] = new int; // each element in arr2p is pointing to the new ints
}

// step 3: we assign some arbitrary integers 
for (int i = 0; i < 4; i++){
	*arr2p[i] = 1;
}

// step 4: we free the individual pointers in the arr2p
for (int i = 0; i < 4; i++){
	delete arr2p[i];
	arr2p[i] = nullPtr;
}

// step 5: we free our array
delete []arr2p;
arr2p = 

Dynamically Allocating Array of Objects

class student{
	public:
		string name;
		int ID;
		student(){
			name = "";
			ID = 0;
		}
		~student(){
			cout << "Destructed" << endl;
		}
}

int main(){
	student *arr = new student[3]; // constructor called 3 times
	delete []arr; // calling the destructor
	return 0;
}

Dynamically Allocating Array of Pointers to Objects

// step 1: initialize the array
student **arr2p = new student*[3]; // arr2p points to an array of pointer-to-students

// step 2: fill up the array so each element points to a student object
for (int i = 0; i < 3; i++){
	arr2p[i] = new student; // new student objects are made using constructor
	arr2p[i]->ID = i + 1; // this will set the ID of student
}

// step 3: we free what the pointers in the array are pointing to
for (int i = 0; i < 3; i++){
	delete arr2p[i];
	arr2p[i] = nullPtr;
}

// step4: we free the array
delete []arr2p;
arr2p = nullPtr;

C++ File I/O

Standard I/O

#include <iostream>
using namespace std;

int main(){
	int x;
	cout << "Enter a value" << endl;
	cin >> x;
	cout << x;
	return 0;
}

File I/O: Output to a file

There are multiple objects defined in this. We will use 2 of them:

  1. ifstream, which is for input files (reading files from the computer)
  2. ofstream, which is for output files (writing to text files etc.)
#include <fstream>
using namespace std;

int main(){
	ofstream outFile("myFile.txt"); // opens myFile.txt file (created in directory)
	
	string name = "we are ECE244";
	
	outFile << name << endl;; // we write the string to the outfile
	
	outFile.close(); // closes myFile.txt
}