C++ Core Knowledge To Get The Ability To Read

The key points are beautifully addressed in Derek Banas’s one hour video. These concepts are gone through: ternary operator; file i/o; pointers; reference operator; class/objects; private; static variables; public/encapsulation; constructors; static functions; this; inheritance; call superclass constructor; execute static method; virtual methods; polymorphism; abstract data type.

Commonly seen signs << after cout is just a shift sign; another sign -> is unique in C++ as is related to pointer function, instead of . for member functions, -> is used for such kind of member functions involved in pointer and references.

There are two types of file – .h file is header file, and .cpp file that is a implementation file.

Namespace is a feature in C++ but not in C. It is a declarative region that provides a scope to the identifiers (names of the types, function, variables etc) inside it.

Void is to define function to return some values. Void pointers are often used to allow functions to operate on data of an unknown type.

Templates in C++ allows to write generic programs, used in large codebase for the purpose of code reusablity and flexibility of the programs.

Scope resolution operator :: is to make it clear to which namespace or class a symbol belongs. that is, tell the compiler where to look. How to use it? std:: cout and cin. another shorthand example is to first write a class:

class foo
void bar();

Then we call upon the object and function contained in this class:

// Implement the constructor

void foo::bar()
// Implement bar

Type of variable must be defined in C++, for example, to write a simple function as below:

int addNumbers(int firstNum, int secondNum = 0){
int combinedValue = firstNum + secondNum;
return combinedValue;

Or, a recursive function to do factorial calculation:

int getFactorial(int number){

int sum;
if(number == 1) sum = 1;
else sum = (getFactorial(number – 1) * number);
return sum;

Pointer use int*, refer to the refernced variable with *age, for example
void makeMeYoung(int* age){
cout << “i used to be ” << *age << endl;
*age = 21;
compare to
void actYoungAge(int& age){
age = 39;

Class in C++, a typical class of Animal looks like

// classes start with the name class

class Animal

// private variables are only available to methods in the class
int height;
int weight;
string name;

// A static variable shares the same value with every object in the class
static int numOfAnimals;

// Public variables can be accessed by anything with access to the object
int getHeight(){return height;}
int getWeight(){return weight;}
string getName(){return name;}
void setHeight(int cm){ height = cm; }
void setWeight(int kg){ weight = kg; }
void setName(string dogName){ name = dogName; }

// Declared as a prototype
void setAll(int, int, string);

// Declare the constructor
Animal(int, int, string);

// Declare the deconstructor

// An overloaded constructor called when no data is passed

// protected members are available to members of the same class and
// sub classes

// Static methods aren’t attached to an object and can only access
// static member variables
static int getNumOfAnimals() { return numOfAnimals; }

// This method will be overwritten in Dog
void toString();


int Animal::numOfAnimals = 0;

Prototype method is declared void setAll(int, int, string);
now define this prototype method setAll:

void Animal :: setAll(int height, int weight, string name){
// this is used to refer to an object created of this class type
this -> height = height;
this -> weight = weight;
this -> name = name;
Animal :: numofAnimals ++;

Constructor, a constructor is called when an object is created; a
destructor is called when an object is destroyed.

Animal::Animal(int height, int weight, string name) {

this -> height = height;
this -> weight = weight;
this -> name = name;


// The destructor is called when an object is destroyed
Animal::~Animal() {

cout << "Animal " < name << " destroyed" << endl;


A constructor can be called when no attributes are passed:

Animal::Animal() {

// This method prints object info to screen and will be overwritten
void Animal::toString(){

cout < name << " is " < height << " cms tall and "
< weight << " kgs in weight" << endl;


Inheritance is an universal concept across the board of all object-oriented languages, how is it applied in C++? We created another class Dog, and Dog can inherit from the Animal Class.

class Dog : public Animal{

string sound = “Woof”;
void getSound() { cout << sound << endl; }

// Declare the constructor
Dog(int, int, string, string);

// Declare the default constructor and call the default superclass
// constructor
Dog() : Animal(){};

// Overwrite toString
void toString();


// Dog constructor passes the right attributes to the superclass
// constructor and then handles the attribute bark that remains
Dog::Dog(int height, int weight, string name, string bark) :
Animal(height, weight, name){

this -> sound = bark;


// toString method overwritten
void Dog::toString(){

// Because the attributes were private in Animal they must be retrieved
// by called the get methods
cout < getName() << " is " < getHeight() <<
" cms tall and " < getWeight() << " kgs in weight and says " < sound << endl;


Other than “Void”, “Main” is everywhere in C++, what does ‘Main” do?
Attributes can be sent to main.

Switch is used in C++ for limited options.

C++ has this conditional comparison, // variable = (condition) ? if true : if false

int largestNum = (5 > 2) ? 5 : 2;

cout << "The biggest number is " << largestNum << endl;

Array in C++ seems to be more handy

// ———- ARRAYS ———-
// Arrays store multiple values of the same type

// You must provide a data type and the size of the array
int myFavNums[5];

// You can declare and add values in one step
int badNums[5] = {4, 13, 14, 24, 34};

// The first item in the array has the label (index) of 0
cout << "Bad Number 1: " << badNums[0] << endl;

// You can create multidimensional arrays
char myName[5][5] = {{'D','e','r','e','k'},{'B','a','n','a','s'}};

cout << "2nd Letter in 2nd Array: " << myName[1][1] << endl;

// You can change a value in an array using its index
myName[0][2] = 'e';

cout << "New Value " << myName[0][2] << endl;

C++ if or while loop syntax

/ You can also cycle through an array by nesting for loops
for(int j = 0; j < 5; j++){

for(int k = 0; k < 5; k++){
cout << myName[j][k];

cout << endl;


// ———- WHILE LOOP ———-
// Use a while loop when you don't know ahead of time when a loop will end

// Generate a random number between 1 and 100
int randNum = (rand() % 100) + 1;

while(randNum != 100){

cout << randNum << ", ";

// Used to get you out of the loop
randNum = (rand() % 100) + 1;


cout << endl;

// You can do the same as the for loop like this
// Create an index to iterate out side the while loop
int index = 1;

while(index <= 10){

cout << index << endl;

// Increment inside the loop


While certain condition is met, do some function, the syntax is

// Used when you want to execute what is in the loop at least once

// Used to store a series of characters
string numberGuessed;
int intNumberGuessed = 0;

do {
cout << "Guess between 1 and 10: ";

// Allows for user input
// Pass the source and destination of the input
getline (cin,numberGuessed);

// stoi converts the string into an integer
intNumberGuessed = stoi(numberGuessed);
cout << intNumberGuessed << endl;

// We'll continue looping until the number entered is 4
} while (intNumberGuessed != 4);

cout << "You Win" << endl;

String tricks are handy in C++, for example

// insert places a string in the index defined
yourName.insert(5, ” Justin”);
cout << yourName << endl;

// erase will delete 6 characters starting at index 7

File I/O in C++: // Open a stream to append to whats there with ios::app // ios::binary : Treat the file as binary // ios::in : Open a file to read input // ios::trunc : Default // ios::out : Open a file to write output

When I try to comprehend the meaning of “Catch” and “Throw”, I lean that A C++ exception is a response to an exceptional circumstance that arises while a program is running, such as an attempt to divide by zero. Exceptions provide a way to transfer control from one part of a program to another. C++ exception handling is built upon three keywords: try, catch, and throw.

// You can be prepared for potential problems with exception handling

int number = 0;


if(number != 0){
cout << 2/number << endl;
} else throw(number);

catch(int number){

cout << number << " is not valid input" << endl;


Continued blog will be about Pointer, Reference, virtual method and

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.