Recent Changes - Search:

Welcome to CAVI, the Cisco Academy for the Vision Impaired.

Course Fees Linux Wiki HTML Wiki Documentation Index PmWiki FAQ

Edit SideBar

Chapter4

4.1.1

Image 1:

This is an image of a signpost with multiple directions which have been written out below.

Sign post directions: arr [2], ptarr [r], ptr, ptr [3] [2], arr [8] [1], arr, array and pointer.

4.1.2

Image 1:

This image shows a diagram of a pointer array which has been described below.

There are six rows of boxes. The first row has two rectangles, one is coloured blue and has the text int ** in black, the second is orange with the text int *, then there are three small red squares with the text int written in each one. Between the two rectangles in the first row there is a space with an arrow pointing from the first rectangle to the second one and an arrow pointing from the second rectangle to the three small red squares. There are another five rows replicated directly underneath the first orange rectangle.

4.1.3

Image 1:

This image shows an example of a ptrarr variable which has been written out below.

int  **ptrarr;

4.1.4

Image 1:

This image shows the declaration of a ptrarr variable as well as an array of pointers. This code has been written out below.

ptrarr = new int * [rows];

4.1.5

Image 1:

This image shows an example of storing and allocating memory in a pointer array; the code is written out below.

for(int r = 0; r < rows; r++)
   ptarr[r] = new int[columns];

4.1.6

Image 1:

This image shows an example of a ptrarr variable declaration, which is written out below.

Ptrarr [r] [c] = 0;

4.1.7

Image 1:

This image shows a diagram of how a ptrarr variable works. This has been described below.

There are six rows of boxes. The first row has two rectangles, one is coloured blue with the text ptrarr in white, the second is orange with no text, and then there are three small squares again with no text. There is a space between the two rectangles with an arrow pointing from the first rectangle to the second one and an arrow pointing from the second rectangle to the three small squares. There are another two rows replicated directly underneath the first orange rectangle, with the third row containing a blue rectangle with the text ptrarr[2] and the second small square also being blue with the text ptrarr[2][1] the other three rows are the same as the orange rectangle and the small orange squares.

4.1.8

Image 1:

This image shows a table of triangular matrices which have been written out below.

Value – means variable or pointer in the square Blank – means no variable or pointer in the square

ValueValueValueBlankBlankBlankBlank
BlankValueValueValueBlankBlankBlank
BlankValueValueValueValueBlankBlank
BlankValueValueValueValueValueBlank
BlankValueValueValueValueValueValue

4.1.9

Image 1:

This image shows an example of code for a triangle array which has been written out below.

#include <iostream>
using namespace std;

int main(void)
{
	int row = 5, cols = 5;
	int **arr;
	// allocate and initialize the array
	arr = new int * [rows];
	for(int r = 0; r < rows; r++){
	      arr[r] = new int[r + 1];
	      for(int c = 0; c <= r; c++)
	             arr[r][c] = (r + 1) * 10 + c + 1;
	}
	//print the array
	for(int r = 0; r < rows; r++){
	       for(int c = 0; c <= r; c++)
		cout<<arr [r][c]<<” “;
	cout<<endl;
	}
	// free the array
	for(int r = 0; r < rows; r++)
		delete [] arr [r];
	delete [] arr;
	return 0;
}

4.2.1

Image 1:

This image shows code for converting data types, this is written out below.

Long data = 1;

4.2.2

Image 1:

This image shows an example of code for implicit conversions. This has been written out below.

int Int = 1;
short Short = 2;
long Long = 3;
float Float = 4.0;
double Double = 5.0;

int f(int x){
	return x;
}

//example no.1
Int = Int + Short;
//example no.2
If(Double)
Double--;
//example no.3
Float = 1;
//example no.4
f(Float);
//example no.5
float g(void) {
	return -1;
}

4.2.3

Image 1:

This image shows an example of code for explicit conversions. This has been written out below.

#include<iostream>
using namespace std;
int main(void){
	float f = 3.21;
	double d = 1.23;
	int k = int(f) + (int)d;
	cout<<k<<endl;
	return 0;
}

4.2.4

Image 1:

This image shows an example of code for conversion of data types. This has been written out below.

#include<iostream>
using namespace std;
int main(void){
	short s = 32767;
	int i = s;
	if(i == s)
		cout<<”equal”<<endl;
	else
		cout<<”not equal”<<endl;
	return 0;
}

4.2.5

Image 1:

This image shows another example of code for conversion of data types. This has been written out below.

#include<iostream>
using namespace std;
int main(void){
	int i = 2147483647;
	short s = i;
	if(i == s)
		cout<<”equal”<<endl;
	else
		cout<<”not equal”<<endl;
	return 0;
}

4.2.6

Image 1:

This image shows another example of code for data type conversion. This has been written out below.

#include<iostream>
using namespace std;
int main(void){
	float f = 1234.5678;
	double d = f;
	if(d == f)
		cout<<”equal”<<endl;
	else
		cout<<”not equal”<<endl;
	return 0;
}

4.2.7

Image 1:

This image shows another example of code for data type conversion, which has been written out below.

#include<iostream>
using namespace std;
int main(void){
	double d = 123456.789012;
	float f = d;
	if(d == f)
		cout<<”equal”<<endl;
	else
		cout<<”not equal”<<endl;
	return 0;
}

4.2.8

Image 1:

This image shows another example of code for data type conversion. This has been written out below.

#include<iostream>
using namespace std;
int main(void){
	float f = 123.456;
	float g = 1e100;
	int i = f;
	int j = g;

	cout<<i<<endl;
	cout<<j<<endl;
	return 0;
}

4.2.9

Image 1:

This image shows an example of code for promoting data types. This has been written out below.

#include<iostream>
using namespace std;
int main(void){
	int Int = 2;
	char Char = 3;
	short Short = 4;
	float Float = 5.6;

	Int = Short + Char + Float;
	cout<<Int<<endl;
	return 0;
}

4.3.1

Image 1:

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

#include <string>
string PetName;

4.3.2

Image 1:

This image shows an initialized string variable in C++, which is written out below.

string PetName = “Lassie”;

4.3.3

Image 1:

This image shows another example of an initialized string variable in C++, which has been written out below.

string PetName(“Lassie”);

4.3.4

Image 1:

This image shows another example of an initialized string variable in C++, which has been written out below.

string IsHome = PetName;
string HasReturned(PetName);

4.3.5

Image 1:

This image shows the string operator plus sign in C++, which has been written out below.

#include <iostream>
#include <string>

using namespace std;

int main(void){
	string TheGood = “jekyII”, TheBad = “Hyde”;
	cout<<TheGood + “ & ” + TheBad<<endl;
	cout<<TheBad + “ & “ + TheGood<<endl;
return 0;
}

4.3.6

Image 1:

This image shows another example of the string operator plus sign in C++, which has been written out below.

#include <iostream>
#include <string>

using namespace std;

int main(void){
	string String;
	String = “A” + “B”;
	String = String + “C”;
	String = “B” + String;
	cout<<String<<endl;
return 0;
}

4.3.7

Image 1:

This example shows the string operator plus and equal signs in C++. This has been written out below.

#include <iostream>
#include <string>

using namespace std;

int main(void){
	string TheQuestion = “To Be”;
	TheQuestion += “or not to be”;
	cout<<TheQuestion<<endl;
return 0;
}

4.3.8

Image 1:

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

#include <iostream>
#include <string>

using namespace std;

int main(void){
	string LineOfTypes;
	cin>>LineOfTypes;
	cout<<LineOfTypes<<endl;
return 0;
}

4.3.9

Image 1:

This image shows another example of inputting strings in C++, which has also been written out below.

#include <iostream>
#include <string>

using namespace std;

int main(void){
	string LineOfTypes;
	getline(cin,LineOfTypes);
	cout<<LineOfTypes<<endl;
return 0;
}

4.3.10

Image 1:

This image shows an example of comparing strings in C++, which is written out below.

#include <iostream>
#include <string>

using namespace std;

int main(void){
	string secret = “abracadabra”;
	string password;
	cout<<”enter password:”<<endl;
	getline(cin,password);
	if(secret == password)
		cout<<”Access granted”<<endl;
	else
		cout<<”Sorry”;
	return 0;
}

4.3.11

Image 1:

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

#include <iostream>
#include <string>

using namespace std;

int main(void){ 
	string str1, str2;
	cout<<”Enter 2 lines of text:”<<endl;
	getline(cin,str1);
	getline(cin,str2);
	cout<<”You’ve entered:”<<endl;
	if(str1 == str2)
		cout<<”\””<<str1<<”\” == \””<<str2<<”\””<<endl;
	else if(str1 > str2)
		cout<<”\””<<str1<<”\” > \””<<str2<<”\””<<endl;
	else
		cout<<”\””<<str2<<”\” > \””<<str1<<”\””<<endl;
	return 0;
}

4.3.12

Image 1:

This image shows an example of comparing strings in C++ using the objective approach. This has been written out below.

#include <iostream>
#include <string>

using namespace std;

int main(void){
string secret = “abracadabra”;
	string password;
	cout<<”Enter password:”<<endl;
	getline(cin,password);
	if(secret.compare(password) == 0)
		cout<<”Access granted”<<endl;
	else
		cout<<”Sorry”;
	return 0;
}

4.3.13

Image 1:

This image shows an example of comparing two stings in C++ using the objective approach. This has been written out below.

#include <iostream>
#include <string>

using namespace std;

int main(void){ 
	string str1, str2;
	cout<<”Enter 2 lines of text:”<<endl;
	getline(cin,str1);
	getline(cin,str2);
	cout<<”You’ve entered:”<<endl;
	if(str1.compare(str2) == 0)
		cout<<”\””<<str1<<”\” == \””<<str2<<”\””<<endl;
	else if(str1.compare(str2) > 0)
		cout<<”\””<<str1<<”\” > \””<<str2<<”\””<<endl;
	else
		cout<<”\””<<str2<<”\” < \””<<str1<<”\””<<endl;
	return 0;
}

4.4.1

Image 1:

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

#include <iostream>
#include <string>

using namespace std;

int main(void){ 
	string str1, str2;
	str1 = “ABCDEF”;
	str2 = str1.substr(1,1) + str1.substr(4) + str1.substr();
	cout<<str2<<endl;
	return 0;
}

4.4.2

Image 1:

This is an example of length of strings in C++, this has been written out below.

#include <iostream>
#include <string>

using namespace std;

int main(void){
	string str = “12345”;
	int pos = 1;
	cout<<str.substr(pos).substr(pos).substr(pos).size()<<endl;
	return 0;
}

4.4.3

Image 1:

This image shows another example of comparing strings in C++, which has been written out below.

#include <iostream>
#include <string>

using namespace std;

int main(void){
	string S = “ABC”;
	cout<<S.compare(1,1,”BC”) + S.compare(2,1,S,2,2)<<endl;
	return 0;
}

4.4.4

Image 1:

This image shows an example of finding substrings within strings in C++, this has been written out below.

#include <iostream>
#include <string>

using namespace std;

int main(void){
	string greeting = “My name is Bond, James Bond.”;
	string we_need_him = “james”;
	if(greeting.find(we_need_him) != string::npos)
		cout<<”OMG He’s here!”<<endl;
	else
		cout<<”it’s not him.”<<endl;
	int comma = greeting.find(‘,’);
	if(comma != string::npos)
		cout<<”Curious. He used a comma.”<<endl;
	return 0;
}

4.4.5

Image 1:

This image shows an example of how to determine the size of a string in C++. This has been written out below.

# include<string>
using namespace std;

void printInfo(string &s){
	cout<<”length =”<<s.length()<<endl;
	cout<<”capacity =”<<s.capacity()<<endl;
	cout<<”max size =”<<s.max_size()<<endl;
	cout<<”-----------“<<endl;
}

int main(void){
	string TheString = “content”;
	printInfo(TheString);
	for(int i = 0; i < 10; i++)
		TheString += TheString;
	printInfo(TheString);
	return 0;
}

4.4.6

Image 1:

This image shows an example of controlling the size of memory allocated for strings in C++. This has been written out below.

#include <iostream>
#include <string>

using namespace std;

void PrintInfo(string &s){
	cout<<”content =\””<<s<<”\””;
	cout<<”capacity =”<<s.capacity()<<endl;
	cout<<”----------“<<endl;
}

int main(void){
	string TheString = “content”;
	PrintInfo(TheString);
	TheString.reserve(100);
	PrintInfo(TheString);
	TheString.reserve(0);
	PrintInfo(TheString);
	Return 0;
}

4.4.7

Image 1:

This image shows how to control the content of a string in C++. This has been written out below.

#include <iostream>
#include <string>

using namespace std;

void PrintInfo(string &s){
	cout<<”content =\””<<s<<”\””;
	cout<<”capacity = ”<<s.capacity()<<endl;
	cout<<”is empty? “<<(s.empty() ? “yes” : “no”)<<endl;
	cout<<”-------------“<<endl;
}

int main(void){
	string TheString = “content”;
	PrintInfo(TheString);
	TheString.resize(50,’?’);
	PrintInfo(TheString);
	TheString.clear();
	PrintInfo(TheString);

	Return 0;
}

4.4.8

Image 1:

This image shows how to control the content of a string in C++. This has been written out below.

#include <iostream>
#include <string>

using namespace std;

int main(void){
	string TheString = “content”;
	for(int i = 0; i < TheString.length(); i++)
		TheString[i] = TheString[i] – ‘a’ + ‘A’;
	cout<<TheString<<endl;
	return 0;
}

4.5.1

Image 1:

This image shows a new function for substrings in C++, known as append. This has been written out below.

#include <iostream>
#include <string>

using namespace std;

int main(void){
	string TheString = “content”;
	string TheString;
	NewString.append(TheString);
	NewString.append(TheString,0,3);	
	NewString.append(2,’!’);
	cout<<NewString<<endl;
	return 0;
}

4.5.2

Image 1:

This image shows an example of appending a character in a string in C+. This has been written out below.

#include <iostream>
#include <string>

using namespace std;

int main(void){
	string TheString;
	for(char c = ‘A’; c <= ‘Z’; c++)
		TheString.push_back(c);
	cout<<TheString<<endl;
	return 0;
}

4.5.3

Image 1:

This image shows an example of inserting a string or a character into a string in C++. This has been written out below.

#include <iostream>
#include <string>

using namespace std;

int main(void){
	string quote = “Whyserious?”, anyword = “monsoon”;
	quote.insert(3,2,’ ‘).insert(4,anyword,3,2);
	cout<<quote<<endl;
	return 0;
}

4.5.4

Image 1:

This image shows how to assign a string of characters into a string in C++. This has been written out below.

#include <iostream>
#include <string>

using namespace std;

int main(void){
	string sky;
	sky.assign(80,’*’);
	cout<<sky<<endl;
	return 0;
}

4.5.5

Image 1:

This image shows an example of replacing a substring with another substring in C++. This has been written out below.

#include <iostream>
#include <string>

using namespace std;

int main(void){
	string ToDO = “I’ll think about that in one hour”;
	string Schedule = “today yesterday tomorrow”;

	ToDo.replace(22,12, Schedule, 16, 8);
	cout<<ToDo<<endl;
	return 0;
}

4.5.6

Image 1:

This image shows how to remove part of a sub string in C++. This has been written out below.

#include <iostream>
#include <string>

using namespace std;

int main(void){
	string WhereAreWe = “I’ve got a feeling we’re not in Kansas anymore”;

	WhereAreWe.erase(38,8).erase(25,4);
	cout<< WhereAreWe<<endl;
	return 0;
}

4.5.7

Image 1:

This image shows an example of exchanging the content of two strings in C++. This has been written out below.

#include <iostream>
#include <string>

using namespace std;

int main(void){
	string Drink = “A martini”;
	string Needs = “Shaken, not stirred”;

	cout<<Drink<<”.”<<Needs<<”.”<<endl;
	Drink.swap(Needs);
	cout<<Drink<<”.”<<Needs<<”.”<<endl;
	return 0;
}

4.6.1

Image 1:

This image shows a list of family names, these have been written out below.

Family Names: Hans, Mathew, WIlFried, Lisa, Ann, Camille, Akiko, Wolfgang, Katsu, Anaruk, Abdul, Juan, Pietro and Stephan.

4.6.2

Image 1:

This image shows a car’s license plate with the word namespace.

4.6.3

Image 1:

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

#include <iostream>

int main(void){
	cout<<”play it, Sam”<<endl;
	return 0;
}

4.6.4

Image 1:

This image shows another example of namespace in C++. This has been written out below.

#include <iostream>

int main(void){
	std::cout<<”Play As time goes by”<<std::endl;
	return 0;
}

4.6.5

Image 1:

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

#include <iostream>

using namespace std;

namespace Hogwarts{
	int Troll = 1;
}

namespace Mordor{
	int Troll = 2;
}

int main(void){
	cout<<Hogwarts::Troll<<” “<<Mordor::Troll<<endl;
	return 0;
}

4.6.6

Image 1:

This image shows an example of using a namespace in C++. This has been written out below.

#include <iostream>

using namespace std;

namespace Hogwarts{
	int Troll = 1;
}

namespace Mordor{
	int Troll = 2;
}

using namespace Hogwarts; 

int main(void){
	cout<<Troll<<” “<<Mordor::Troll<<endl;
	return 0;
}

4.6.7

Image 1:

This image shows another example of using a name space in C++. This has been written out below.

#include <iostream>
using namespace std;
namespace Hogwarts{
	int Troll = 1;
}
namespace Mordor{
	int Troll = 2;
}
int main(void){
	{
		using namespace Hogwarts;
		cout<<Troll<<” “;
	}
	{
		using namespace Mordor;
		cout<<Troll<<endl;
	}
	return 0;
}

4.6.8

Image 1:

This image shows how to expand a namespace in C++. This has been written out below.

#include <iostream>
using namespace std;
namespace Hogwarts{
	int Troll = 1;
}
namespace Mordor{
	int Troll = 2;
}
namespace Hogwarts{
	float Wizard = -0.5;
}
namespace Mordor{
	float Wizard = 0.5;
}
int main(void){
	cout<<Hogwarts::Troll<<” “<<Hogwarts::Wizard<<endl;
	cout<<Mordor::Troll<<” “<<Mordor::Wizard<<endl;
	return 0;
}

4.6.9

Image 1:

This image shows the use of an entity in C++. This has been written out below.

#include <iostream>
using namespace std;
namespace Hogwarts{
	int Troll = 1;
	float Wizard = -0.5;
}
namespace Mordor{
	int Troll = 2;
	float Wizard = 0.5;
}
using Mordor::Trolls;
using Hogwarts::Wizard;
int main(void){
	cout<<Hogwarts::Troll<<” “<<Wizard<<endl; 
 	cout<<Troll<<” “<<Mordor::Wizard<<endl;
	return 0;
}

4.6.10

Image 1:

This image shows an example of an unnamed namespace in C++. This has been written out below.

#include <iostream>
using namespace std;
namespace Hogwarts{
	int Troll = 1;
	float Wizard = -0.5;
}
namespace Mordor{
	int Troll = 2;
	float Wizard = 0.5;
}
using Mordor::Trolls;
using Hogwarts::Wizard;
int main(void){
	cout<<Troll<<” “<<Wizard<<endl; 
 	cout<<Mordor::Troll<<” “<<Mordor::Wizard<<endl;
	return 0;
}

4.6.11

Image 1:

This example shows an example of renaming a namespace in C++. This has been written out below.

#include <iostream>
using namespace std;
namespace What_A_Wonderful_Place_For_A_Young_Sorcerer{
	int Troll = 1;
	float Wizard = -0.5;
}
namespace Mordor{
	int Troll = 2;
	float Wizard = 0.5;
}
namespace What_A_Wonderful_Place_For_A_Young_Sorcerer;
int main(void){
	cout<<Hogwarts::Troll<< “ “<<
	What_A_Wonderful_Place_For_A_Young_Sorcerer::Wizard<<endl;
	cout<<Mordor::Troll<<” “<<Mordor::wizard<<endl;
	return 0;
}
Edit - History - Print - Recent Changes - Search
Page last modified on August 01, 2017, at 02:03 PM