This chapter will introduce you to the basics of C++. If you are used to programming in Visual C#, much of this chapter will be review. Let's get started by creating a project.
If you do not have "Visual C++" templates available in the New Project window, you need to Modify your Visual Studio Installation.
Next, we need to create a file to write our code.
The Main Function
Every program needs an entry point. The windows applications that you wrote in C# had a file called Program.cs, that contained a "Main" function. Similarly, we need to create a starting point for our appliction.
To keep the console open when debugging, we will call a function named "getch." In order to use it however, we need to include a header file. We will go into the details of these files in the future. Once we have the window held open, we can print text to the console and have time to actually read it!
In upcoming sections of the chapter, we'll be writing to the console a lot. At this point, it might be good practice to write the previous section of code from scratch (or perhaps... To start press any key.). Once you can do that (without peeking), you're ready to move on!
Review Questions
The following are examples of questions similar to what you can expect to see on the next exam.
- What is the "main" function for? It designates the entry point for the application. (where the instructions start)
- How many "main" functions can a program have? Only one. Having more or less will result in a compiler error.
- How do you make a single line comment in C++? // This is a single line comment.
-
How do you make a multi-line comment in C++?
/* This comment
can span
multiple lines. */ -
What is
#include <conio.h>
used for? It allows us to use _getch().