Course 4 – Functions, classes, testing

Functions – Recap

In the second course I’ve said the following:

When we open the application, it must start from somewhere. In C++, the execution always starts from a function called main.

A function groups together some lines of code, and is defined by three things:

  • Function return type – the function return type (or void, if we don’t return anything further)
  • Function name – so we can call it
  • Function Parameters – variables that are used in the function

Other than that, the content of a function must be inside brackets { }

Let’s see some examples:

Example 1

A function that returns nothing, is called myFunction, and receives no parameters.

Example 2

A function that returns an int, is called doSum, and receives two numbers (returning their sum).

We notice the following:

  • There’s a new keyword added: return. This tells us to leave the function.
  • We receive two params of type int. Their names is first and second. We can give them whatever names we want, that name is used to be further used inside the function.

In the third course I’ve said the following:

Functions are a way of reusing the code. Instead of having 10 lines and copy-paste them many times, we can call the function. If we want/need to modify them, we do it in only one place.

So our application begins in main and ends when leaves main.

I’ve talked about functions, and I want you to get used to testing your code, so I expect an input-output validation function from you, for each function you’re creating.

What do I mean by that?

A function receives an input, returns an output, and compares them to check if they are equal.

We can then use it as follows:


Object Oriented Programming (OOP)

A class is simply a container for functions and variables which should be grouped together.

In the first course I’ve said about variables name and age:

Let’s write a class that defines a human. A human has a name, an age, it eats, sleeps, and work.

A class also needs a name. A class with nothing inside in it looks like this:

Let’s add to this class some variables and some functions.

First of all, we notice the keyword public..ignore it for now. I’ll talk about it in the next course. It allows us to access those members and variables.

Like i said, we use classes to gather together variables and functions that have something in common.

The class is a blueprint for creating objects. To use it, we need to create an instance of that type.

This is similar to the variable creation: type + name.

To access those functions and variables, we use the dot operator (.)


Implement a class that perform mathematical operations (+, -, *, /).

For division, validate to not divide by 0.

Functions must receive two parameters which are used to perform the operations.

The class must also contain two variables, and also have some functions that have no parameters, but further calls back the existing functions with the class variables.

Please test the code with input-output functions, like I said above.

You may also like...