Understanding Arrays, Pointers, Operators, and Friends in C++

Kristoffer Hebert
2 min readNov 3, 2018

--

Object Oriented design in C++ with Kristoffer Hebert

Overview

Arrays are a fixed length list of indexes paired with values. Pointers are the memory addresses of variables. Operators tell the C++ compiler to manipulate values in a specific way. Class Friends have access to private and protected attributes of other Classes.

What are Arrays

In C++, Arrays are a collection of index value pairs with a fixed length. You access values by selecting a valid index. Indexes in C++ Arrays begin at zero.

Array Examples

string groceryList = [5];
groceryList[0] = “Tomatoes”;
// Another way to intialize Arrays
string groceryList[] = { "Tomatoes", "Potatoes", "Carrots" };
// This errors out, because index is out of bounds of Array
string groceryList[2] = {};
cout << groceryList[4] << endl;

What are Pointers

A Pointer is an operator and variable that contains the address of another variable value stored in memory. It’s like a shortcut on your computer desktop that points to a file located elsewhere.

Pointer Examples

string message = “Tacos are delicious”;// * operator creates a shortcut to the value
string *shortcut;
// & operator gets the memory location of a variable
shortcut = &message;

// Returns memory address of 0x738d1e8a2370
cout << shortcut << endl;
// Returns Tacos are delicious
cout << *shortcut << endl;

What are Operators

Operators are symbols that tell the C++ compiler to perform specific manipulations on a value. There are various types of operators, such as Arithmetic, Logical, Relational, and Assignment. This is not an exhaustive list, but you can look them up on cppreference.com

Examples of Operators

Arithmetic
+ Addition
- Subtraction
* Multiply
/ Divide

Logical
! Inverse of a boolean
|| Use first true value left to right
&& If both are true turn true

Relational
== Means equal to
!= Means does not equal
> Greater Than
< Less Than

Assignment
= Assign the value to
+= Add value and assign that value to
*= Times the value and assign that value to
/= Divide the value and assign that value to

What are Friends

Friends in C++ are Classes that have access to private and protected attributes of other Classes.

Friend Syntax

class PersonOne {
friend class PersonTwo;
...
}

C++ Friend Examples

Key Takeaways

Friend Classes means another Class has access to it’s private and protected attributes. An Array is a fixed length collection of index value pairs. A Pointer is a variable that contains the memory address of another variable. Operators tell the C++ compiler to manipulate values in a specific way.

--

--