Installation

  1. Install Visual Studio Community
  2. Install Desktop Development with C++

https://learn.microsoft.com/en-us/cpp/build/vscpp-step-0-installation?view=msvc-170

Our first program

  1. Open Visual Studio
  2. Create a new project
  3. Console App
  4. Enter a name and location (if you want) and create

This template gives you the code that will print “Hello World!” on the console screen. You can run program by clicking run button (green triangle on the top side of the screen)

The run button

Now we can investigate the code:

#include <iostream>  // copy and paste "iostream" to the inside of this file 


// the first function that will be run when program starts 
int main(){ // Beginnig of the function 
	std::cout << "Hello World!" << std::endl; // print Hello World! to the screen

} // End of the function


// this is a comment line. Compiler does not see these lines 

When we press the run button we will take this output:

Hello World!

We need to include iostream in our code for printing the screen. If we comment this line we cannot use std::cout and std::endl.

Instead of typing text directly into the std::cout << “bla bla bla” << std::endl; We can store our data inside a variable.

#include <iostream> 
#include <string>

int main(){ 
	// greeting is a variable 
	// A variable can store data. 
	// The type of the greeting is a string so we can store text inside of it. 
	std::string greeting = "Hello C++";
	std::cout << greeting << std::endl;
} 

We can use two different string variables to store our data and after that, we can combine these strings by +


int main(){ 
	std::string name = "C++";
	std::string greeting = "Hello " + name;
	std::cout << greeting << std::endl;
} 

Until now you cannot see any difference between our first code and last code. You can ask “Why do we do that”

  1. We can use this variable in different places again and again. Of course, you can copy and paste “Hello C++” text again and again but the problem is when you want to write “Hello World” instead of “Hello C++” you have to change all text one by one. This is an error-prone approach.
  2. Now we can take this data from a user or a file and manipulate it.
#include <iostream> 
#include <string>

int main(){ 
	std::string name = "C++";
	std::cout << "Enter your name, please!" << std::endl;
	std::cin >> name; // We take a word from consele and write inside of the name variable.

	std::string greeting = "Hello " + name;
	std::cout << greeting << std::endl;
} 
Enter your name, please!
Esat
Hello Esat

Main function

The main function is a unique function that runs on program starts. In other words, it is the entry point of the program. It returns an INTEGER variable to the operating system (Windows, Mac, Linux…).

  • 0 -> SUCCESS
  • other values -> FAILURE
int main(){ 
	return 0;
}

Why we didn’t return integer on previous examples ?

A return statement ([stmt.return]) in main has the effect of leaving the main function (destroying any objects with automatic storage duration) and calling std​::​exit with the return value as the argument.

If control flows off the end of the compound-statement of main, the effect is equivalent to a return with operand 0 (see also [except.handle]).

https://eel.is/c++draft/basic.start.main#5

Namespace

#include <iostream>

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

Instead of writing std::cout, std::endl we can write

#include <iostream>

using namespace std; 

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

As you can see it is more easy but unfortunately we have got a problem:

#include <iostream>
using namespace std;

int cout = 5;

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

different libraries can use same name for different things. For that reason we should be carefull when using namespace. For now we haven’t got any problem because we will be using only std namespace.

std is an abbreviation of “standard”. std is the “standard namespace”. coutcin and a lot of other functions are defined within it.

https://stackoverflow.com/questions/67411397/what-does-using-namespacestd-mean#:~:text=std%20is%20an%20abbreviation%20of,functions%20are%20defined%20within%20it.

Comments


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *