C++ basics


Branching


Switch statements


Looping


Arrays


File input and output


Classes

// Basic getter function     // Example of formatting before returning data
float Student::GetGPA()      string Person::GetFullName()
{                            {
  return m_gpa;                return m_lastname + ", " + m_firstname;
}                            }
// Basic setter function                // Example of data validity check
void Student::SetGPA( float newGpa )    void Student::SetGPA( float newGpa )
{                                       {
  m_gpa = newGpa;                         if ( newGpa < 0 || newGpa > 4 )
}                                         {
                                            cout << "Error! Invalid GPA!" << endl;
                                            return; // Return without saving data
                                          }
                                          else
                                          {
                                            m_gpa = newGpa;
                                          }
                                        }

Pointers

// DECLARING POINTERS
int * ptrInt = nullptr;
string * ptrString = nullptr;


// SETTING POINTERS TO ADDRESSES
string name = "Bob";      // Normal variable
string * ptrName = &name; // Pointer


// DEREFERENCING POINTERS
cout << *ptrName; // Display contents that pointer is pointing to
cin >> *ptrName; // Overwrite contents that pointer is pointing to


// POINTERS TO OBJECTS
Student* newStudent = new Student;
newStudent->name = "Hikaru";
cout << "Student name: " << newStudent->name << endl;

// CHECKING IF POINTER IS SAFE
if ( ptrName != nullptr )
{
  // Pointer is safe to use...
}
// ALLOCATE MEMORY                        // DEALLOCATE MEMORY
// FOR A SINGLE VARIABLE                  // FOR A SINGLE VARIABLE
int * num = new int;                      delete num;
Node* newNode = new Node;                 delete newNode;


// ALLOCATE SPACE FOR AN ARRAY            // DEALLOCATE SPACE FOR AN ARRAY
int * numList = new int[100];             delete [] numList;
Node* nodeList = new Node[10];            delete [] nodeList;

Author: RachelWil

Created: 2026-07-06 Mon 12:41

Validate