Important Understanding
Creating projects and understanding the basic structure of code is important before we understand how to write and use code.
|
Project Setup
Create a Project
On the right of the window you can see multiple options but to create a project click the last option that says "Create a new project".
Project TypeAfter creating new project you will see many options for templates of projects, scroll down until you see "Empty Project". Click this to make anew empty project.
Note: Other project types re useful but for more advanced projects and programming experience Naming
To finalize the project enter a name for the project and the solution. The solution is the overarching program while projects are a sort of sub module for the overall program. For our uses name them the same thing.
Creating a FileTo create a file right click on the project directory located on the right hand file explorer. On the drop down window hover over "Add" and then Click on "New Item" on the new drop down window. On the window that shows up click on the "C++ File". Then name the file with your wanted name followed by .cpp.
Eg: filename.cpp |
Finally, click on the file that you just created in the file explorer. This should be under "Source Files".
Code Structure
Hello World Program
This is one of the simplest possible program. It simply outputs "Hello World!", to the console.
Lets go through line by line to understand it better. CommentsComments in code are extremely helpful in annotating your code. To start a line of comments in code just use a double slash //. This makes the whole line a comment and wont be compiled.
To make a section of lines comments start a section using /* and end it using */ . Preprocessor Headers
Lines at the start of a program that are indicated with the hash sign (#) are known as directive code. They tell the preprocessor instructions on compilation of the code. In this case #include means that we are trying to include definitions and code from a header file.
Here we are including the "iostream" header file which includes definitions such as "std::cout". This header file lets us output to the console and get input from it as well. Header files can be created, or we can use system header files like "iostream" to have easily accessed functionality. The Main Function
A function is a way of grouping code so that it can be called upon at any time. The main function can be thought of as the root of all code that is run. This is where you write all code that will be run. the main() function is common to all C++ programs. This syntax is important to remember.
int refers to the output of the function currently this is not important to understand until we go in depth into functions. But it is important to know the main function will most commonly be an integer function. The body of the function lays between the curly brackets. The line this function contains is written to output the given text to the console. "std::cout" is a command using the std namespace from the iostream header we included. We are calling cout short for console output. This is followed by << which represent pushing the given text to the call. I like to visualize this as pushing the text to the console. Text is notates with " and the text goes in between. To finish off the line of code put a semicolon to indicate the end. |