Alex Walker

C++ Scope Resolution Operator

Scope resolution operator (::)

The scope resolution operator is used to qualify identifiers within classes and namespaces. You can use the scope resolution operator to explicitly qualify an identifier in cases where the identifier is not visible in the current scope or where the identifier is in the global scope but is hidden by an identifier in the current scope.

Example

Here is some code that demonstrates the usage of the scope resolution operator

scope_operator.cpp

   
/*
	on os x compile with
	clang++ scope_operator.cpp -o scope_operator

	on windows compile with
	cl /EHsc scope_operator.cpp

	on linux compile with
	g++ scope_operator.cpp -o scope
*/

#include <iostream>

void Buy(std::string thing)
{
	std::cout << "Buying " << thing << std::endl;
}

void Read(std::string thing)
{
	std::cout << "Reading " << thing << std::endl;
}

void Read()
{
	Read("something");
}

namespace Advert
{
	void Read(std::string thing)
	{
		std::cout << "Reading an Advert in ";
		std::cout << thing << std::endl;
	}
}

namespace Book
{
	void Read()
	{
		// call the Read function in the global namespace
		::Read("a Book");
	}
}

namespace Magazine
{
	void Read()
	{
		// call the Read function in the global namespace
		::Read("a Magazine");
		
		// call the Read function in the Advert namespace
		Advert::Read("a Magazine");
		
		// call the Buy function in the global namespace
		Buy("a Magazine");
	}
}

namespace Newspaper
{
	void Read(std::string thing)
	{
		std::cout << "Reading " << thing;
		std::cout << " in the Newspaper";
		std::cout << std::endl;
	}

	void Read()
	{
		// call the Read function in the global namespace
		::Read("a Newspaper");
		
		// call the Read function in the Newspaper namespace
		Read("an article");
		
		// call the Read function in the Advert namespace
		Advert::Read("a Newspaper");
	}
}

int main()
{
	// call the Read function from the global namespace
	Read();
	
	// call the Read function from the Book namespace
	Book::Read();
	
	// call the Read function from the Magazine namespace
	Magazine::Read();
	
	// call the Read function from the Newspaper namespace
	Newspaper::Read();
}

Compile the program with the following command

    clang++ scope_operator.cpp -o scope_operator

Execute the program using the following command

    ./scope_operator

The output of the program should be something like the following

    Reading something
    Reading a Book
    Reading a Magazine
    Reading an Advert in a Magazine
    Buying a Magazine
    Reading a Newspaper
    Reading an article in the Newspaper
    Reading an Advert in a Newspaper

Breakdown

Three functions are defined in the global scope. The Buy function and two versions of the Read function. The first version of the Read function takes a paramter that it uses to generate it’s output, the second verion is parameterless and is used to call the first version with a default parameter. These functions are visible to the namespaces that follow.

   
void Buy(std::string thing)
{
	std::cout << "Buying " << thing << std::endl;
}

void Read(std::string thing)
{
	std::cout << "Reading " << thing << std::endl;
}

void Read()
{
	Read("something");
}

The Advert namespace has a Read function that hides the Read functions from the global namespace.

   
namespace Advert
{
	void Read(std::string thing)
	{
		std::cout << "Reading an Advert in ";
		std::cout << thing << std::endl;
	}
}

The Book namespace has a Read function that hides the Read functions from the global namespace. It must use the scope resolution operator to refer to the Read function in the global namespace.

   
namespace Book
{
	void Read()
	{
		// call the Read function in the global namespace
		::Read("a Book");
	}
}

The Magazine namespace has a Read function that hides the Read functions from the global namespace. It must use the scope resolution operator to refer to the Read function in the global namespace. It needs to call the Read function in the Advert namespace, so it qualifies the Read function using the namespace name and the scope resolution operator. It then calls the Buy function from the global namespace, it doesn’t need to qualify the Buy function because it is not hidden by anything.

   
namespace Magazine
{
	void Read()
	{
		// call the Read function in the global namespace
		::Read("a Magazine");
		
		// call the Read function in the Advert namespace
		Advert::Read("a Magazine");
		
		// call the Buy function in the global namespace
		Buy("a Magazine");
	}
}

The Newspaper namespace has two Read functions that hides the Read functions from the global namespace. It must use the scope resolution operator to refer to the Read functions from the global namespace. When calling it’s own Read function, it doesn’t need to qualify it. It needs to call the Read function in the Advert namespace, so it qualifies the Read function using the namespace name and the scope resolution operator. It then calls the Buy function from the global namespace, it doesn’t need to qualify the Buy function because it is not hidden by anything.

   
namespace Newspaper
{
	void Read(std::string thing)
	{
		std::cout << "Reading " << thing;
		std::cout << " in the Newspaper";
		std::cout << std::endl;
	}

	void Read()
	{
		// call the Read function in the global namespace
		::Read("a Newspaper");
		
		// call the Read function in the Newspaper namespace
		Read("an article");
		
		// call the Read function in the Advert namespace
		Advert::Read("a Newspaper");
	}
}

When called from the global namespace, the Read function in the global namespace does not need to be qualified.

   
	// call the Read function from the global namespace
	Read();

The Read functions from the Book, Magazine and Newspaper namespaces must be fully qualified, because they are not visible in the global namespace.

   
	// call the Read function from the Book namespace
	Book::Read();
	
	// call the Read function from the Magazine namespace
	Magazine::Read();
	
	// call the Read function from the Newspaper namespace
	Newspaper::Read();
Syntax highlighting in VIM on OS X

You may find that syntax highlighting is not enabled by default in vim

image

Open a terminal and type the following followed by enter

    vim ~/.vimrc

Press i to enter insert mode and enter the following

    filetype plugin indent on
    syntax on

Press escape to leave insert mode and then type :wq followed by enter to save and quit

image

The next time you open a file in vim, syntax highlighting should be applied to it

image

Using VIM on OS X

Open a terminal window

You can do this by pressing the following keyboard combination

+ Space

Spotlight will appear

image

Type “Terminal” into spotlight and press enter

image

A terminal window will open

image

Type “vim” and press enter

image

C/C++ Structure reference and Structure de-reference operators

Structure reference operator (.)

The structure reference operator is used to refer to a member of an object. The structure reference operator is a period character(.). The behaviour of the structure reference operator can be described as element selection by reference.

The following line of code

   
// invoke member b of object a
a.b();

Structure de-reference operator (->)

The structure de-reference operator is used to refer to a member of an object via a pointer to that object. The structure de-reference operator is denoted with a hyphen and a greater than character (->), appearing to be an arrow pointing to the right. The behaviour of the structure de-reference operator can be described as element selection through a pointer.

   
// invoke member b of object pointed to by a
a->b();

Example

Here is some code that demonstrates the usage of the structure reference and structure de-reference operators

structure_pointers.cpp

/*
	on os x compile with
	clang++ structure_pointers.cpp -o structure_pointers

	on windows compile with
	cl /EHsc structure_pointers.cpp

	on linux compile with
	g++ structure_pointers.cpp -o structure_pointers
*/

#include <iostream>

struct car {
  std::string name;
  std::string registration_number;
  int top_speed;
  float cost;
};

int main()
{
	// declare a car
	car my_car;
	
	// set the members of the car using the structure reference operator
	my_car.name = "Model X";
	my_car.top_speed = 130;
	my_car.cost = 80000;
	my_car.registration_number = "TESLA";

	// access the members of the car using the structure reference operator
	std::cout << "Access the members using the structure reference operator";
	std::cout << std::endl;
	std::cout << "car name: " << my_car.name << std::endl;
	std::cout << "car price: " << my_car.cost << std::endl;
	
	// obtain a pointer to the car
	car * pointer = &my_car;
	
	// use the structure de reference operator to change the values of
	// the car members
	std::cout << std::endl;
	std::cout << "Change the members via the structure de-reference operator";
	std::cout << std::endl;
	pointer->name = "Model Y";
	pointer->cost = 85000;

	// display the members of the
	std::cout << std::endl;
	std::cout << "Access the members using the structure de-reference operator";
	std::cout << std::endl;
	std::cout << "car name: " << pointer->name << std::endl;
	std::cout << "car price: " << pointer->cost << std::endl;
}

Compile the program with the following command

    clang++ structure_pointers.cpp -o structure_pointers

Execute the program using the following command

    ./structure_pointers

The output of the program should be something like the following

    Access the members using the structure reference operator
    car name: Model X
    car price: 80000

    Change the members via the structure de-reference operator

    Access the members using the structure de-reference operator
    car name: Model Y
    car price: 85000

Breakdown

The car structure is defined.

struct car {
  std::string name;
  std::string registration_number;
  int top_speed;
  float cost;
};

An instance of the car structure is declared as my_car

	// declare a car
	car my_car;

The my_car variable’s members are set to their intial values.

	// set the members of the car using the structure reference operator
	my_car.name = "Model X";
	my_car.top_speed = 130;
	my_car.cost = 80000;
	my_car.registration_number = "TESLA";

The structure reference operator is used to access and display the members of my_car

	std::cout << "car name: " << my_car.name << std::endl;
	std::cout << "car price: " << my_car.cost << std::endl;

A pointer variable is declared and initialised with the address of the my_car variable

	// obtain a pointer to the car
	car * pointer = &my_car;

The structure dereference operator is used to refer to the members of the my_car variable and change their values.

	pointer->name = "Model Y";
	pointer->cost = 85000;

The structure dereference operator is used to access and display the members of my_car via the pointer variable

	std::cout << "car name: " << pointer->name << std::endl;
	std::cout << "car price: " << pointer->cost << std::endl;
C/C++ Reference and Dereference operators

Reference operator (&)

The reference operator is used to obtain the address of a variable. The reference operator is an ampersand symbol (&). The reference operator is also known as the “address of” operator.

Variables that are used to store the address of other variables are known as pointers.

De-reference operator (*)

The dereference operator (or indirection operator) is used to access the value of a variable referenced by a pointer. The dereference operator is an asterisk symbol (*). The dereference operator is also known as the “value of” operator.

A variable referenced with & can be de-referenced with *.

Example

Here is some code that demonstrates the usage of the reference and de-reference operators

pointers.cpp

   
/*
	on os x compile with (install xcode and command line tools)
	clang++ pointers.cpp -o pointers

	on windows compile with (install visual studio)
	cl /EHsc pointers.cpp

	on linux compile with (apt-get install g++)
	g++ pointers.cpp -o pointers
*/

#include <iostream>

int main()
{
	// assign a value to the variable
	int variable = 75;

	// get the address of the variable (a reference/pointer to the variable)
	int * pointer = &variable;

	// display the value of the variable
	std::cout << "The variable value is " << variable << std::endl;

	// display the address of the variable
	std::cout << "The address of the variable is " << pointer << std::endl;

	// change the value of the variable using the pointer
	*pointer = 45;
	std::cout << "Changed the value using a reference to its address" << std::endl;

	// display the value of the variable
	std::cout << "The value of the variable is " << variable << std::endl;

	// display the value of the dereferenced variable
	std::cout << "The value via the pointer is " << *pointer << std::endl;
}

Compile the program with the following command

    clang++ pointers.cpp -o pointers

Execute the program using the following command

    ./pointers

The output of the program should be something like the following

    The variable value is 75
    The address of the variable is 0x7fff5dd61bac
    Changed the value using a reference to its address
    The value of the variable is 45
    The value via the pointer is 45

Breakdown

The following line is used to declare and intialise a variable of type int

	// assign a value to the variable
	int variable = 75;

The next step obtains a reference to the previously declared variable and stores it in a pointer variable. Notice the use of the * operator when declaring the pointer variable, and the use of the & operator when obtaining the reference to the other variable.

  // get the address of the variable (a reference/pointer to the variable)
  int * pointer = &variable;

The next two statements display the value of the variable and the value of the pointer variable.

	// display the value of the variable
	std::cout << "The variable value is " << variable << std::endl;
	
	// display the address of the variable
	std::cout << "The address of the variable is " << pointer << std::endl;

The pointer variable can be used to change the value of the variable to which it points. The following statement is used to do this, the * operator is used to indicate that the value being changed is the variable that the pointer points to, not the pointer itself.

	// change the value of the variable using the pointer
	*pointer = 45;
	std::cout << "Changed the value using a reference to its address" << std::endl;

Lastly the value of the variable is displayed, firstly by displaying the value of the actual variable, the last statement displays the value that the pointer points to using the * operator.

  // display the value of the variable
  std::cout << "The value of the variable is " << variable << std::endl;
 
  // display the value of the dereferenced variable
  std::cout << "The value via the pointer is " << *pointer << std::endl;

captureEntirePageScreenshot(filename, kwargs)

captureEntirePageScreenshot(filename, kwargs)

captureEntirePageScreenshotAndWait(filename, kwargs)

Arguments:
  • filename - the path to the file to persist the screenshot as. No filename extension will be appended by default. Directories will not be created if they do not exist, and an exception will be thrown, possibly by native code.
  • kwargs - a kwargs string that modifies the way the screenshot is captured. Example: “background=#CCFFDD” . Currently valid options: backgroundthe background CSS for the HTML document. This may be useful to set for capturing screenshots of less-than-ideal layouts, for example where absolute positioning causes the calculation of the canvas dimension to fail and a black background is exposed (possibly obscuring black text).

Saves the entire contents of the current window canvas to a PNG file. Contrast this with the captureScreenshot command, which captures the contents of the OS viewport (i.e. whatever is currently being displayed on the monitor), and is implemented in the RC only.

Currently this only works in Firefox when running in chrome mode, and in IE non-HTA using the EXPERIMENTAL “Snapsie” utility.

The Firefox implementation is mostly borrowed from the Screengrab! Firefox extension. Please see http://www.screengrab.org and http://snapsie.sourceforge.net/ for details.

Selenium Documentation

Generated from: isElementPresent(locator)

assertElementPresent(locator)

verifyElementPresent(locator)

waitForElementPresent(locator)

Arguments:

Returns:true if the element is present, false otherwise

Verifies that the specified element is somewhere on the page.

Selenium Documentation

answerOnNextPrompt

answerOnNextPrompt(answer)

Arguments:
  • answer - the answer to give in response to the prompt pop-up

Instructs Selenium to return the specified answer string in response to the next JavaScript prompt [window.prompt()].

Selenium Documentation

altKeyUp

altKeyUp()

altKeyUpAndWait()

Release the alt key.

Selenium Documentation

altKeyDown

altKeyDown()

altKeyDownAndWait()

Press the alt key and hold it down until doAltUp() is called or a new page is loaded.

Selenium Documentation