Learntofish's Blog

A blog about math, physics and computer science

Tutorial: OOP in C++ (Part 1 – Multiple files)

Posted by Ed on October 3, 2012

This is part 1 of the tutorial on object oriented programming in C++. We will not begin with classes and objects right away. Instead we will focus on using multiple files first.

Other parts can be found here:
Part 2 – Class and object

1. First C++ program
To follow this tutorial you have to install C++ first. There are several ways. Here, I will assume that you have Codelite installed. So, run Codelite and do the following:

1. Create a new workspace
– Click on “Workspace” -> “New Workspace”. Type in “Tutorial” as Workspace name and confirm by clicking on the “Create” button.
– On the left hand side of Codelite there is a “Workspace” tab. Click on it. You can see the orange colored folder “Tutorial”.

2. Create a new project
– Rightclick on that orange “Tutorial” folder and click on “Create New Project”.
– Type in “helloWorld” as project name and click “OK”.
– On the left hand side, i.e. in the Workspace tab, you should see a folder “helloWorld”.
– Set the project as active: Either double click on the “helloWorld” folder or rightclick on it and choose “Set As Active”.
The active project will appear as green folder.

3. Code in main.cpp
– Left of the green “helloWorld” folder is a triangle. Click on it such that the yellow “src” folder appears.
– Click on the triangle left of the yellow “src” folder such that that you can see the “main.cpp” file.
– Double click on the main.cpp file. That file will already contain some code. Delete it and replace it with the following code:

#include <iostream>
using namespace std;

int main(){
	cout << "Hello World!" << endl;

	return 0;
}

4. Compile and run
– Compile the program by clicking on the “Build Active Project” icon (left from the red x icon)
– Run the program by clicking on the icon “Run Active Project” icon (the icon looks like a gear)
A console window should open with the following output:

Hello World!

2. Functions
Let’s write a function called “printSomething()” by extending our code as follows:

#include <iostream>
using namespace std;

// function 
void printSomething(){
	cout << "I like pizza." << endl;
}

int main(){
	cout << "Hello World!" << endl;
	printSomething();   // calling the function

	return 0;
}

– In line 05-07 we defined our function “printSomething”. Line 04 is just a comment that is ignored by C++.
– In line 11 we call the function.
Compile and run the program, and you should get this as output:

Hello World!
I like pizza.

3. Function prototype
It is possible to split the function into two parts:
(i) prototype (declaration)
(ii) implementation
Change the code as follows:

#include <iostream>
using namespace std;

// function prototype
void printSomething();

int main(){
	cout << "Hello World!" << endl;
	printSomething();   // calling the function
	return 0;
}

// implementation
void printSomething(){
	cout << "I like pizza." << endl;
}

– Line 05 contains the function prototype. This tells C++ that there exists a function called “printSomething()”.
– Line 14-16 shows the implementation. The implementation tells C++ what the function “printSomething()” does.
Compile and run the program. It still prints the same output.

4. Putting the implementation in a different file
Now, C++ does not care where the implementation is located. You could have put the implementation before or after the main() function. We can even put the implementation in another file:
– First, add a new file by rightclicking on the yellow “src” folder and choosing “Add New File”.
– Choose “C++ source File (.cpp)” as file type and type in “impl” as name. Click on OK. This will create a file called “impl.cpp”.
– Cut the “implementation” part from the main.cpp file and paste it into the “impl.cpp” file.
Both files should look like this:

File “main.cpp”

//main.cpp

#include <iostream>
using namespace std;

// function prototype
void printSomething();

int main(){
	cout << "Hello World!" << endl;
	printSomething();   // calling the function
	return 0;
}

File “impl.cpp”

//impl.cpp

#include <iostream>
using namespace std;

// implementation
void printSomething(){
	cout << "I like pizza." << endl;
}

Notice that impl.cpp requires lines 03 and 04 because we use the keyword “cout”.

In Codelite you can drag the “implementation.cpp” tab to the right hand side of the screen such that you can view both the “main.cpp” and “implementation.cpp” code.

5. Putting the prototype in a header file
It is important that the prototype is always above the main() function. However, we can put the prototype in another file too.
– First, add a new file by rightclicking on the yellow “src” folder and choosing “Add New File”.
– Choose “Header File (.h)” as file type and call it proto. Clicking on OK will create a file called “proto.h”.
– Cut the function prototype in “main.cpp” and paste it into the empty file “proto.h”.
The three files should look like this:

File “main.cpp”

//main.cpp

#include <iostream>
#include "proto.h"
using namespace std;

int main(){
	cout << "Hello World!" << endl;
	printSomething();   // calling the function

	return 0;
}

File “impl.cpp”

//impl.cpp

#include <iostream>
using namespace std;

// implementation
void printSomething(){
	cout << "I like pizza." << endl;
}

File “proto.h”

// proto.h

// function prototype
void printSomething();

I have mentioned that the prototype is written above the main() function. However, if you look at our main.cpp function there is no prototype of the “printSomething()” function. Nevertheless, the prototype is there with a little trick: If you closely, we have added the following in line 04 of the main.cpp file:

#include "proto.h"

With #include we insert the content of the file “proto.h” right into line 04, so the prototype is indeed above the main() function.

6. Exercise
Write a function “int addNumbers(int x, int y)”. The function takes two integers and returns their sum. Instructions:
a) Put the prototype in a header file “proto_add.h”.
b) Write the implementation in the file “impl_add.cpp”.
c) Don’t forget to include the header file.
d) Test the function by adding the following to our “main.cpp” file:

cout << "99 + 101 equals " << add(99,101) << endl;

Solution
Part a)
We create a file “proto_add.h”:

// proto_add.h

int add(int x, int y);

Part b)
We create a file “impl_add.h”:

// impl_add.cpp

int add(int x, int y){
	return x+y;
}

Part c)
The file “main.cpp” is modified as follows:

//main.cpp

#include <iostream>
#include "proto.h"   // important line!
#include "proto_add.h" // important line!

using namespace std;

int main(){
	cout << "Hello World!" << endl;
	printSomething();   // calling the function
	
	// testing the "add()" function
	cout << "99 + 101 equals " << add(99,101) << endl;

	return 0;
}

– Line 05 includes the new header file.
– With line 14 we test the “add()” function.

Part d)
If you compile and run the program, you will get the following output:

Hello World!
I like pizza.
99 + 101 equals 200

7. Summary
– The header file contains the prototype which is the declaration of a function.
– The implementation can be put in an additional file (e.g. impl.cpp).
– The header file must be included in the main file.

Question
Why do we have to include the header file in “main.cpp”?

Answer:
We have mentioned that in C++ the prototype (function declaration) must be located above the main() function. We achieve this by putting a line

#include "myHeaderFile.h"

above the main() function.

8. Other OOP tutorials
If you liked this blogpost, you should also have a look at my other tutorials:
OOP in Java
OOP in Python

Advertisements

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

 
%d bloggers like this: