Recent Changes - Search:

Home Page

Student Resources

Connect with us:

Full Length Courses

Short Courses

Course Archive Information

Class Wikis

Socials

Various tips:

PmWiki

Edit SideBar

End Navigation

Chapter5

5.1.1

Image 1:

This image shows three circles interlinked to each other, in each of the circles is the written text letters, signs and numbers.

5.1.2

Image 1:

This image shows three coloured circles, green, orange and blue respectively with the words flour in the green circle, milk in the orange and spices in the blue. Each circle is inside a bowl with an arrow pointing downwards from the bottom of the bowl to the word cake.

5.1.3

Image 1:

This image shows three columns of rectangles. The first column has one rectangle which is coloured purple with the word vehicles written in white text, the second column has four orange rectangles with the words written from top to bottom as follows: land vehicles, water vehicles, air vehicles and space vehicles, each of these rectangles are attached to the purple rectangle by a straight orange line. The third column has three blue rectangles with words written from top to bottom as follows: wheeled vehicles, tracked vehicles and hovercrafts, each of the blue rectangles are also attached to the first orange rectangle by a straight blue line.

5.1.4

Image 1:

This image shows three columns of rectangles. The first column has one rectangle which is coloured purple with the word animals written in white text, the second column has five orange rectangles with the words written from top to bottom as follows: mammals, reptiles, birds, fish, and amphibians, each of these rectangles are attached to the purple rectangle by a straight orange line. The third column has two blue rectangles with the words wild animals and domesticated animals written in them respectively, each of these rectangles are also attached to the first orange rectangle by a blue straight line.

5.1.5

Image 1:

This image shows sixteen thumbnail pictures of different types of fruit, arranged in rows of four. the rows contain the following fruits: Row 1: cherries, peaches, plums, and rock melon. Row 2: kiwi-fruit, blueberries, watermelon, and honey-dew melon. Row 3: blood orange, figs, mandarin, and strawberries. Row 4: apple, persimmon, red grapes, and blackberries.

5.1.6

Image 1:

This image shows the family tree of the Royal Pedigree of England.

5.1.7

Image 1:

This image is showing a 1950s pink and white Cadillac car.

5.1.8

Image 1:

This image shows an example of how to define a class and create an object in C++, this has been written out below.

Class OurClass {};

5.1.9

Image 1:

This is another example of how to define a class and create an object in C++, this has also been written out below.

OurClass our_object;

5.2.1

Image 1:

This image shows a single stack of Australian 1 dollar coins in a column.

5.2.2

Image 1:

This image shows an example of a stack (also known as a LIFO (Last In First Out)) which has been written in C++ below.

int stack[100];

5.2.3

Image 1:

This image shows an example of a stack pointer, which has been written out in C++ below.

int SP = 0;

5.2.4

Image 1:

This image shows an example of a new stack function called push, which has been written out in C++ below.

void push(int value){
	stack[SP++] = value;
}

5.2.5

Image 1:

This image shows an example of a new stack function called pop, which has been written out in C++ below.

int pop(void){
	return stack[--SP];
}

5.2.6

Image 1:

This image shows a stack program, which is written out below in C++.

#include <iostream>

using  namespace std;

int stack[100];
int SP = 0;

void push(int value) {
	stack[SP++] = value;
}

int pop(void){
	return stack[--SP];
}

int main(void) {
	push(3);
	push(2);
	push(1);
	cout<<pop()<<endl;
	cout<<pop()<<endl;
	cout<<pop()<<endl;
	return 0;
}

5.2.7

Image 1:

This image shows four stacks of Australian one dollar coins, with each stack being bigger than the previous stack of gold coins.

5.2.8

Image 1:

This image shows an example of a program using a vector as the stack's storage and the int variable as the stack pointer. This has been written out below in C++.

class Stack{
	int stackstore[100];
	int SP;
	};

5.2.9

Image 1:

This image shows an example of a program using a vector as the stack's storage and the int variable as the stack pointer, which has been updated. this has been written out below in C++.

class Stack{
private:
	int stackstore[100];
	int SP;
	};

5.2.10

Image 1:

This image shows another example of a program using a vector as the stack's storage and the int variable as the stack pointer, which has been updated. this has been written out below in C++.

class Stack{
private:
	int stackstore[100];
	int SP;
public:
	void push (int value);
	int pop(void){
		return stackstore[--SP];
	}
};

5.2.11

Image 1:

This image shows another example of a program using a vector as the stack's storage and the int variable as the stack pointer, which has been updated, this has been written out below in C++.

class Stack{
private:
	int stackstore[100];
	int SP;
public:
	void push (int value);
	int pop(void){
		return stackstore[--SP];
	}
};

void stack::push(int value){
	stackstore[sp++] = value;
}

5.2.12

Image 1:

This image shows another example of a program using a vector as the stack's storage and the int variable as the stack pointer, which has been updated, this is written out below in C++.

class Stack{
private:
	int stackstore[100];
	int SP;
public:
	Stack(void) {SP = 0;}
	void push (int value);
	int pop(void){
		return stackstore[--SP];
	}
};

void stack::push(int value){
	stackstore[sp++] = value;
}

5.2.13

Image 1:

This image shows another example of a program using a vector as the stack's storage and the int variable as the stack pointer, which has been updated. This has been written out below in C++.

#include<iostream>

using namespace std;

int main(void){
	Stack little_stack, another_stack, funny_stack;

	Little_stack.push(1);
	another_stack.push(little_stack.pop() + 1);
	funny_stack.push(another_stack.pop() + 2);
	cout<<funny_stack.pop()<<endl;
	return 0;
}

5.2.14

Image 1:

This image shows an example of creating a subclass for the stack class which has been written out below in C++.

class AddingStack : Stack {
};

5.2.15

Image 1:

This image shows another example of a program using a vector as the stack's storage and the int variable as the stack pointer, which has been updated. This has been written out below.

class AddingStack : Stack{
private:
	int sum;

public:
	void push (int value);
	int pop(void);
};

5.2.16

Image 1:

This image shows an example of the push function in C++, which is written out below.

void AddingStack::push(int value){
	sum += value;
	Stack += value;
	Stack::push(value);
}

5.2.17

Image 1:

This image shows a new pop function, which has been written out below in C++.

int AddingStack::pop(void){
	int val = Stack::pop():
	sum -= val;
	return val;
}

5.2.18

Image 1:

This image shows how to define a new function called getSum, which has been written out below in C++.

int AddingStack::getSum(void){
	return sum;
}

5.2.19

Image 1:

This image shows how to create a new class object called AddingStack, and initialise the super class constructor. this has been written out below in C++.

AddingStack::AddingStack(void): Stack(){
	Sum = 0;
}

5.2.20

Image 1:

This image shows the final version of the updated program using a vector as the stack's storage and the int variable as the stack pointer. This has been written out below in C++.

class AddingStack : Stack{
private:
		int sum;
Public:
		AddingStack(void);
		void push(int value);
		int pop(void);
		int getsum(void);
};
AddingStack::AddingStack(void) : Stack() {
	Sum = 0;
}
void AddingStack::push(int value){
	sum += value;
	Stack::push(value);
}
int AddingStack::pop(void){
	int val = Stack::pop();
	sum -= val;
	return val;
}
int AddingStack::getsum(void){
	return sum;
}

5.2.21

Image 1:

This is an image of a complete program that will output 2 numbers, 45 and zero. The program has been written out below in C++.

#include <iostream>

using namespace std;

int main(void){
	AddingStack Super_stack;

	for(int i = 1; i < 10; i++)
		super_stack.push(i);
	cout<<super_stack.getsum()<<endl;
	for(int i = 1; i < 10;  i++) 
		super_stack.pop();
	cout<<super_stack.getSum()<<endl;
	return 0;
}

5.3.1

Image 1:

This image shows an example of class components, these have been written out below in C++.

class Class{
	int value;
	void setVal(int value);
	int getVal(void);
};

5.3.2

Image 1:

This image shows an example of Access specifiers, these have been written out below in C++.

class Class{
public:
	void setVal(int value);
	int getVal(void);
private:
	int value;
};

5.3.3

Image 1:

This image shows an example of creating an object, this has been written out below in C++.

Class the_object;

5.3.4

Image 1:

This image shows an example of overriding component names in C++, this has been written out below.

class Class{
public:
	void setVal(int value) {
	         Class::value = value;
	}
	int getVal(void);
	private:
		int value;
};

5.3.5

Image 1:

This image shows an example of a pointer, which has been written out below in C++.

class Class{
public:
	void setVal(int value) {	 
		this -> value = value;
	}
	int getval(void);
private:
	int value;
};

5.3.6

Image 1:

This image shows qualifying component names in C++, these have been written out below.

class Class{
public:
	void setVal(int value) {	 
		this -> value = value;
	}
	int getval(void);
private:
	int value;
};

int Class::getVal(void) {
	return value;
}

5.3.7

Image 1:

This image shows another example of qualifying component names that have been updated, these are written out below in C++.

class Class{
public:
	void setVal(int value) {this -> value = value; }
	void setVal(void) {value = -2;}
	int getVal(void) {return value;}
private:
	int value;
};

5.3.8

Image 1:

This image shows an example of constructors in C++, this has been written out below.

class Class{
public:
	class(void) {this -> value = -1; }
	void setVal(int value) {this -> value = value;}
	int getVal(void) {return value;}
private:
	int value;
};

5.3.9

Image 1:

This image shows an example of overloading constructor names, which has written out below in C++.

class Class{
public:
	class(void) {this -> value = -1; }
	class(int val) setVal(int value) {this -> value = val;}
	void setVal(int value) {this -> value = value;}
	int getVal(void) {return value;}
private:
	int value;
};

5.3.10

Image 1:

This image shows another example of overloading constructor names in C++, which is written out below.

class Class{
public:
	class(int val) {this -> value = val; }
	void setVal(int value) {this -> value = value;}	
	int getVal(void) {return value;}
private:
	int value;
};

5.3.11

Image 1:

This image shows an example of copying constructors in C++. This has been written out below.

#include <iostream>
using namespace std;
class Class1{
public: 
	Class1(int val) {this -> value = val;}
	Class1(Class1 const &source) {value = source.value + 100;)
	int value;
};
class Class2{
public:
	Class2(int val) {this -> value = val;}
	int value;
};
int main(void) {
	Class1 object11(100), object12 = object11;
	Class2 object21(200), object22 = object21;
	cout<<object12.value<<endl;
	cout<<object22.value<<endl;
	return 0;
}

5.3.12

Image 1:

This image shows an example of memory leaks in C++, which has been written out below.

#include <iostream>
using namespace std;
class Class{
public:
	Class(int val){
		value = new int[val];
		cout<<”Allocation (“<< val << “) done.”<<endl;
	}
	int *value;
};
void MakeALeak(void) {
	Class object(1000);
}
int main(void) {
	MakeALeak();
	return 0;
}

5.3.13

Image 1:

Shows an example of Destructors in C++, which has been written out below.

#include <iostream>  
 using namespace std;
class Class{
public:
	Class(int val) {
		value = new int[val];
		cout<<”Allocation (“<< val << “) done.”<<endl;
	}
	~Class(void){
		delete [] value;
		cout<<”Deletion done.”<<endl;
	}
	int *value;
};
Void MakeALeak(void) {
	Class object(1000);
}
int main(void) {
	MakeALeak();
	return 0;
 }

5.4.1

Image 1:

This image shows an example of the auto keyword. The code for this example has been written out below.

#include <iostream>
using namespace std; 
void fun(void){
	auto int var = 99;
	cout<<”var = ”<<++var<<endl;
}
int main(void){
	for(int i = 0; i < 5; i++)
		fun();
	return 0;
}

5.4.2

Image 1:

This image shows another example of the auto keyword, the code has been written out below.

#include <iostream>
using namespace std; 
void fun(void){
	static int var = 99;
	cout<<”var = “<<++var<<endl;
}
int main(void) {
	for(int i = 0; i < 5; i++)
		fun();
	return 0;
}

5.4.3

Image 1: this image shows an example of classes in C++; the example code has been written out below.

  1. include <iostream>

using namespace std; class Class{ public:

	int val;
	void print(void) {cout<<val<<endl;}

} int main(void){

	Class::val = 0;
 	Class::print();
	return 0;
	}

5.4.4

Image 1:

This image shows the static components of a class; the example code is written out below.

#include <iostream>
using namespace std; 
class Class{
public:
	static int Static;
	int NonStatic;
	void print(void) {
		cout<<”Static = “<<++Static<<”,NonStatic = “<<NonStatic<<endl;
	}
};
int Class::Static = 0;
int main(void) {
	Class instance1, instance2;
	instance1.NonStatic = 10;
	instance2.NonStatic = 20;
	instance1.print();
	instance2.print();
	return 0;
	}

5.4.5

Image 1:

This image shows an example of static class variables in C++. The example code is written out below.

#include <iostream>
using namespace std; 
class Class{
public:
	static int Counter;
	Class(void) {++Counter;};
	~Class(void) {
		--Counter;
		if(Counter == 0) cout<<”Bye, bye!”<<endl;
	};
	void HowMany(void) {cout<<Counter<<”instances”<<endl;}
}
int Class::Counter = 0;
int main(void) {
	Class a;
	Class b;
	cout<<Class::Counter<<” instances so far “<<endl;
	Class c;
	Class d;
	d.HowMany();
	return 0;
	}

5.4.6

Image 1:

This image shows another example of static class variables in C++. The example code has been written out below.

#include <iostream>
using namespace std; 
class Class{
	static int Counter;
public:  
	Class(void) {
		++Counter;
	};
	~Class(void) {--Counter; if(Counter == 0)
			cout<<”Bye, bye!”<<endl;
	};
	void HowMany(void) {cout<<Counter<<” instances”<<endl;}
};
int Class::Counter = 0;
int main(void) {
	Class a;
	Class b;
	b.HowMany();
	Class c;
	Class d;
	d.HowMany();
	return 0;
	}

5.4.7

Image 1:

This image shows another example of static class variables in C++. The example code has been written out below.

#include <iostream>
using namespace std; 
class Class{
	static int Counter;
public:  
	Class(void) {
		++Counter;
	};
	~Class(void) {
		--Counter;
		if(Counter == 0)
		cout<<”Bye, bye!”<<endl;
	};
	static void HowMany(void) {cout<<Counter<<”instances”<<endl;}
};
int Class::Counter = 0;
int main(void) {
	Class a;
	Class b;
	b.HowMany();
	Class c;
	Class d;
	d.HowMany();
	return 0;
	}

5.4.8

Image 1:

This image shows a table of static and non-static components, which is currently empty. The table has been written out in list form below.

Table contents:

Accessed component: Static Non-static

Accessing component: Static Non-static

Results: nothing here yet.

End of table.

5.4.9

Image 1:

This image shows an example of a static function invoking another static function. The code has been written out below.

#include <iostream>

using namespace std;

class Test{
public:
	static void: funS1(void) {cout<<”static”<<endl;}
	static void: funS2(void) {funS1(); }
};

int main(void) {
	Test object;
	Test::funS2();
	object.funS2();
	return 0;
 }

5.4.10

Image 1:

This image shows a table of static and non-static components, which has been updated; the table has been written out in list form below.

Table contents:

Accessed component: Static Non-static

Accessing component: Static Non-static

Results: Static accessed component and static accessing component: OK.

End of table.

5.4.11

Image 1:

This image shows an example of a static function trying to invoke a non-static function. The code is written out below.

#include <iostream>
using namespace std;
class Test{
public:
	void funN1(void) {cout<<”Non-static”<<endl;}
	static void: funS1(void) {funN1(); }
};
int main(void) {
	Test object;
	Test::funS1();
	object.funS1();
	return 0;
 }

5.4.12

Image 1:

This image shows a table of static and non-static components, which has been updated; the table has been written out in list form below.

Table contents:

Accessed component: Static Non-static

Accessing component: Static Non-static

Results: Static accessed component and static accessing component: OK. Non-static accessed component and static accessing component: Failure.

End of table.

5.4.13

Image 1:

This image is showing an example of a non-static function trying to invoke a static function. The code has been written out below.

#include <iostream>

using namespace std;

class Test{
public:
	static void funS1(void) {cout<<”static”<<endl;}
	static void: funN1(void) {funS1(); }
};
int main(void) {
	Test object;
	object.funN1();
}

5.4.14

Image 1:

This image shows a table of static and non-static components, which has been updated; the table has been written out in list form below.

Table contents:

Accessed component: Static Non-static

Accessing component: Static Non-static

Results: Static accessed component and static accessing component: OK. Non-static accessed component and static accessing component: Failure. Static accessed component and non-static accessing component: OK.

End of table.

5.4.15

Image 1:

This image shows the completed table of static and non-static components; the table has been written out in list form below.

Table contents:

Accessed component: Static Non-static

Accessing component: Static Non-static

Results: Static accessed component and static accessing component: OK. Non-static accessed component and static accessing component: Failure. Static accessed component and non-static accessing component: OK. Non-static accessed component and non-static accessing component: OK.

End of table.

5.5.1

Image 1:

This image shows an example of pointers to objects in C++; the example code has been written out below.

#include <iostream>
using namespace std;
class Class {
public:
	Class(void) {
		cout<<”Object constructed!”<<endl;
	}
	~Class(void) {
		cout<<”Object destructed!”<<endl;
}
};
int main(void) {
	Class *ptr;
	ptr = new Class();
	delete ptr;
	return 0;
	}

5.5.2

Image 1:

This image shows an example of pointers to fields in C++; the example has been written out below.

#include <iostream>
using namespace std;
class Class {
public:
	Class(void) {
		cout<<”Object constructed!”<<endl;
	}
	~Class(void) {
		cout<<”Object destructed!”<<endl;
}
int value;
};
int main(void) {
	Class *ptr;
	ptr = new Class;
	ptr -> value = 0;
	cout << ++(ptr -> value)<<endl;
	delete ptr;
	return 0;
};

5.5.3

Image 1:

This image shows an example of pointers to functions in C++; the code has been written out below.

#include <iostream>
using namespace std;
class Class {
public:
	Class(void) {
		cout<<”Object constructed!”<<endl;
	}
	~Class(void) {
		cout<<”Object destructed!”<<endl;
}
void IncAndPrint(void) {
	cout<<”value = “<<++value<<endl;
}
int value;
};
int main(void) {
	Class *ptr;
	ptr = new Class;
	ptr -> value = 1;
	ptr -> IncAndPrint();
	delete ptr;
	return 0;
};

5.5.4

Image 1:

This image shows an example of selecting a constructor in C++; the code has been written out below.

#include <iostream>
using namespace std;
class Class {
public:
	Class(void) {cout<<”Object constructed (#1)”<<endl; }
	Class(int v) {value = v; cout<<”Object constructed (#2)” <<endl; }
	~Class(void) {cout<<”Object destructed! val = “<<value<<endl; }
	void IncAndPrint(void) {
		cout<<”value = “<<++value<<endl;
	}
	int value;
};
int main(void) {
	Class *ptr1, *ptr2;
	ptr1 = new Class;
	ptr2 = new Class(2);
	ptr1 -> value = 1;
	ptr1 -> IncAndPrint();
	ptr2 -> IncAndPrint();
	delete ptr2;
	delete ptr1;
	return 0;
};

5.5.5

Image 1:

This image shows an example of an array of pointers to objects in C++; the code has been written out below.

#include <iostream>
using namespace std;
class Array {
	int *values;
	int size;
public:
	Array(int siz) {
		Size = siz; values = new int[size];
		cout<<”Array of ”<<size<<” ints constructed.“<<endl;
	}
	~Array(void) {
		delete [];
		cout<<”Array of ”<<size<<” ints destructed.“<<endl; 
	}
	int Get(int ix) {return values[ix]; }
	void put(int ix, int val) {values[ix] = val; }
};
int main(void) {
	Array *arr = new Array(2);

for(int i = 0; i < 2; i++)
	arr -> Put(i, i + 100);
for(int i = 0; i < 2; i++)
	cout<<”#”<<i + 1<<”:”<<arr -> Get(i)<<endl;
delete arr;
return 0;
};

5.5.6

Image 1:

This image shows another example of an array of pointers to objects; the code has been written out below.

#include <iostream>
using namespace std;
class Array {
	int *values;
	int size;
public:
	Array(int siz) {
		Size = siz; values = new int[size];
		cout<<”Array of ”<<size<<” ints constructed.“<<endl;
	}
	~Array(void) {
		delete [] values;
		cout<<”Array of ”<<size<<” ints destructed.“<<endl; 
	}
	int Get(int ix) {return values[ix]; }
	void put(int ix, int val) {values[ix] = val; }
};
int main(void) {
	Array *arr[2] = {new Array(2), new Array(2) };

for(int i = 0; i < 2; i++)
	for(int j = 0; j < 2; j++)
		arr[i] -> Put(j,j + 10 + i);
for(int i = 0; i < 2; i++) {
	for(int j = 0; j < 2; j++) 
		cout<<”#”<<i + 1<<”:”<<arr[i] -> Get(j)<<”;”;
cout<<endl;
	}
	delete arr[0]; delete arr[1];
	return 0;
}

5.5.7

Image 1:

This image shows an example of objects inside objects in C++; the code has been written out below.

#include <iostream>
using namespace std;
class Element {
	int value;
public:
	int Get(void) {return value;}
	void Put(int val) {value = val;}
};
Class Collection {
	Element el1, el2;
Public:
	int Get(int elno) {return elo == 1 ? el1.Get() : el2.Get(); }
	int Put(int elno, int val) {if(enlo == 1) el1.Put(val); else el2.Put(val); }
};
int main(void) {
	Collection coll;

	for(int i = 1; I <= 2; i++)
		coll.Put(i, i + 1);
	for(int i = 1; I <= 2; i++)
		cout<<”Element #”<<i<<” = “<<coll.Get(i)<<endl;
	return 0;
}

5.5.8

Image 1:

This image shows another example of objects inside objects; the code is written out below.

#include <iostream>
using namespace std;
class Element {
	int value;
public:
	Element(void) {cout<<”Element constructed!”<<endl; }
int Get(void) {return value;}
	void Put(int val) {value = val;}
};
class Collection {
	Element el1, el2;
Public:
	Collection(void) {cout<<”Collection constructed!”<<endl; }
int Get(int elno) {return elo == 1 ? el1.Get() : el2.Get(); }
	int Put(int elno, int val) {if(enlo == 1) el1.Put(val); else el2.Put(val); }
};
int main(void) {
	collection coll;
	return 0;
}

5.5.9

Image 1:

This image shows another example of objects inside objects; The code has been written out below.

#include <iostream>
using namespace std;
class Element {
	int value;
public:
	Element(int val) {value = val; cout<<”Element(“<<val<<”)constructed!”<<endl;
}
int Get(void) {return value;}
	void Put(int val) {value = val;}
};
class Collection {
	Element el1, el2;
Public:
	Collection(void) {cout<<”Collection constructed!”<<endl; }
int Get(int elno) {return elo == 1 ? el1.Get() : el2.Get(); }
	int Put(int elno, int val) {if(enlo == 1) el1.Put(val); else el2.Put(val); }
};
int main(void) {
	Collection Coll;
	return 0;
	};

5.5.10

Image 1:

This image shows another example of objects inside objects; the code is written out below.

#include <iostream>
using namespace std;
class Element {
	int value;
public:
	Element(int val) {
		value = val; cout<<”Element(“<<val<<”)constructed!”<<endl;
	}
int Get(void) {return value; }
	void Put(int val) {value = val; }
};
Class Collection {
	Element el1, el2;
public:
	Collection(void): el2(2), el1(1) {cout<<”Collection Constructed!”<<endl; }
int Get(int elno) {return elo == 1 ? el1.Get() : el2.Get(); }
	int Put(int elno, int val) {if(enlo == 1) el1.Put(val); else el2.Put(val); }
};
int main(void) {
	Collection coll;
	return 0;
	}	
Edit - History - Print - Recent Changes - Search
Page last modified on August 09, 2017, at 03:38 PM