Learntofish's Blog

A blog about math, physics and computer science

Posts Tagged ‘c++’

Lectures on C++ STL

Posted by Ed on April 25, 2016

Check out these Channel 9 lectures by Stephan T. Lavavej on the Standard Template Library (STL) in C++.

From the website’s description:

Learn all about the Standard Template Library (STL)  from the great Stephan T. Lavavej (STL), Microsoft’s keeper of the STL cloth (this means he manages the partnership with the owners of STL and Microsoft, including, of course, bug fixes and enhancements to the STL that ships as part of Visual C++).

Posted in programming | Tagged: , | 1 Comment »

C programming tutorial (by Adam)

Posted by Ed on August 29, 2013

Here is a great C programming tutorial by Adam. It’s currently made up of 87 videos where each video lasts for 5-10 minutes. I really like the drawings that illustrate what is going on in the memory.

I went through the tutorial by watching and simultaenously copying the source code. And before I would go to the next video I recreated the code from memory which I found best to learn the concepts.

Posted in computer science | Tagged: , , , | Leave a Comment »

Tutorial: OOP in C++ (Part 2 – Class and object)

Posted by Ed on October 6, 2012

This is part 2 of the C++ Object Oriented Programming tutorial. Last time we focused on using multiple files. Today we will have a look at classes and objects.

1. Definition of a class

Consider this example: Albert Einstein, Frank Sinatra, Marie Curie, what do they have in common? Obviously, they are persons. We say they are objects of the class “Person”. In general, a class describes its objects by two things:
1. Attributes (properties)
2. Methods (functions that can access attributes and change them)

The Person class above may have the attributes “name” and “age”. We will give it the method “talk()” which allows our objects to actually talk.

Let’s write the Person class in C++:
– Create a new project in Codelite and call it “personClass” by right-clicking on the orange “Tutorial” folder from last time. Then double click the “personClass” folder to set the project as active.
– Open the “main.cpp” file in the yellow “src” folder and replace the code by:

#include <iostream>
#include <string>
using namespace std;

// definition of class "Person"
class Person{

	// attributes	
		string name;
		int age;
	// methods
		Person(string myName, int myAge){
			name = myName;
			age = myAge;

		void talk(){
			cout << "Hi! My name is " << name << " and I am " << age << " years old." << endl;
};   // <--- Don't forget the semicolon here!

// main() function
int main(){

	Person p("Homer", 42);    // Create an object of the class "Person"
	p.talk();                 // Let the object talk
	return 0;

– In line 07-24 we defined Read the rest of this entry »

Posted in computer science | Tagged: , , , | Leave a Comment »

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 Read the rest of this entry »

Posted in computer science | Tagged: , , , | Leave a Comment »

Increment operator: difference between ++i and i++

Posted by Ed on November 22, 2011

The increment operator raises the value of an integer by 1:
Example in C++:

using namespace std;

int main(){
    int i=5;

    // increment
    cout << "before: i=" << i << endl;
    cout << "after: i=" << i << endl;

    return 0;

The console output is:

before: i=5
after: i=6

The result is the same if we Read the rest of this entry »

Posted in computer science | Tagged: , , , , , , | Leave a Comment »

Basic Algorithm 3: cut the number

Posted by Ed on November 12, 2011

Last time we talked about calculating the sum of the integers from 1 to N. Today we deal with the following problem:

Given an integer, e.g. 31415926, produce the following output:
So, you have to cut the number from right to left.

a) Write a function with a while loop that produces this output.
b) Write a function without a while loop producing the output (Hint: recursion).
Part b) is a little difficult if you don’t have experience with recursion.

Test your function with the following integers:
31415926, 1000, 5 and 0

Before I post my solution let me Read the rest of this entry »

Posted in computer science | Tagged: , , , , , | 1 Comment »

Awesome C++ tutorials by thenewboston

Posted by Ed on October 14, 2011

Sooooo, I’ve just finished watching these awesome C++ tutorial videos by Bucky Roberts, also known as thenewboston.
His C++ playlist contains 73 episodes and I highly recommend them. Bucky’s humor makes the lessons very entertaining
and he really explains concepts simply.

If you have never heard of Bucky before, check out his website thenewboston and
his youtube channel. You will find tutorials on a variety of topics such
as Java, PHP, Python, Game Development and much more.

Also read about his teaching philosophy here.

Posted in computer science | Tagged: , , , , , | Leave a Comment »

References in C++

Posted by Ed on August 1, 2009

Last time I wrote about pointers in C++. Today I’d like to talk about references in C++. Let’s view the following source code:
Read the rest of this entry »

Posted in computer science | Tagged: , | Leave a Comment »

Tutorial on Pointers in C++

Posted by Ed on July 11, 2009

In C++ there is something called pointers, and I’d like to explain what they are. But before I explain them I’d like to thank simplcool for his excellent tutorial on pointers in C++. Watch his videos: part1 and part2. Another introduction to pointers in C can be found here. (The picture on the left is from xkcd)

Read the rest of this entry »

Posted in computer science | Tagged: , , | Leave a Comment »