C++: Using Visual Studio for C++

This entry is part 1 of 61 in the series C++

In an earlier post in Learn C, I have worked out a simple guide on compiling a C program using Visual Studio.

For compiling C++ using Visual Studio, we use very similar steps to compile and run a program.

If you have not downloaded C++, please download it from this link.

Launch the visual studio and click on New Project. The following screenshot will appear.

cp1

You can use the default name and click on OK.

In the Welcome To Win32 Application Wizard, click on Next.

cp2

Click On Empty Project as shown in the screenshot above.

An empty project with the following screen shot will be shown.

cp3

Notice the Solution Explorer on the right of the screen.

Now we want to create a file and put our source code there.

Right click on Source File in Solution Explorer.  Then Add -> New Item.

Name our first C++ program as Hello.cpp as shown in the screenshot below.

cp4

Click on Add.

Paste the code below to Hello.cpp.

#include <iostream>

int main()
{
	std::cout << "Hello World";
	getchar();
}

Click on Local Windows Debugger to run the program.

cp5

In our code, the cout is a standard string output in C++.  Notice the line getchar().  This is a C code and I have used it so that the console will prompt us to type in a character allowing us to see the Hello World text in the console.

 

C++: The language

This entry is part 2 of 61 in the series C++

C++ is a general purpose programming language.

It is an extension of C and so all the C code that you have learnt could be used when you are writing C++.

C++ is standardized by the ISO with the latest (and current) been published in September 2011.  This latest iteration is also called C++ 11.

A newer iteration was drafted in the year 2014 and it is called C++ 14.  C++ 14 is, however, a working draft version.

It is object-oriented in nature and the original idea is that an object-oriented program is well suited for large scale program development.

C++: The main function

This entry is part 3 of 61 in the series C++

We look at a simple program that basically does nothing.

In all C++ program, you need a starting main function similar to C.

The operating system such as your Windows OS will first call the main function when the program is executed.

The program below contains only one return statement wrapped inside 2 curly braces.

int main()
{
	return 0;
}

 

The main function is required to return type of int, which is a type that represents integers.  In this simple program, we simply return a value of 0 to it.

C++: Namespace

This entry is part 4 of 61 in the series C++

A namespace is a declarative region that attaches an identifier to any names declared inside it.

The identifier makes it less likely that a name will conflict with names declared elsewhere in the program.

In the example below, std is a namespace declaration.

#include <iostream>

int main()
{
	std::cout << "Hello World";
	getchar();
}

By using std::cout, we ensure that we are using cout in the namespace std and not elsewhere.

To make our code look cleaner, we can use using namespace.

#include <iostream>
using namespace std;

int main()
{
	cout << "Hello World";
	getchar();
}

 

C++: Addition of 2 numbers

This entry is part 5 of 61 in the series C++

Addition of 2 numbers, using system(“pause”) to let the console stay.

#include "stdafx.h"
#include <iostream>

using namespace std;

int main()
{
	int a, b, c;

	cout << "Enter two numbers to add\n";
	cin >> a >> b;

	c = a + b;
	cout << "Sum of entered numbers = " << c << endl;

	system("pause");
	return 0;
}

add