- Install Microsoft Visual Studio
- Create a Project with Microsoft
- Writing a Program in Microsoft Visual Studio
- Running a Program in Visual Studio
- Compatibility Issue #1: stdafx.h
- Compatibility Issue #2: Pausing the Screen
- If You're Not Using Microsoft
- Advancing to the Next Print Line
- Storing Data: C++ Variables
- Introduction to Data Types
- A Word about Variable Names and Keywords
- Chapter 1 Summary
Chapter 1 Summary
Here are the main points of Chapter 1:
- Creating a program begins with writing C++ source code. This consists of C++ statements, which bear some resemblance to English. (Machine code, by contrast, is completely incomprehensible unless you look up the meaning of each combination of 1s and 0s.) Before the program can be run, it must be translated into machine code, which is all the computer really understands.
- The process of translating C++ statements into machine code is called compiling.
- After compiling, the program also has to be linked to standard functions stored in the C++ library. This process is called linking. After this step is successfully completed, you have an executable program.
- If you have a development environment, the process of compiling and linking a program (building) is automated so you need only press a function key. With Microsoft Visual Studio, press Ctrl+F5 to build programs.
If you’re working with Microsoft Visual Studio, make sure you leave #include “stdafx” at the beginning of every program. If you start a project by going through the New Project command, the environment will always put this in for you. Just make sure you don’t delete #include “stdafx” when pasting code into the environment.
#include "stdafx.h"
Simple C++ programs have the following general form:
#include <iostream> using namespace std; int main() { Enter_your_statements_here! return 0; }
To print output, use the cout object. For example:
cout << "Never fear, C++ is here!";
To print output and advance to the next line, use the cout object and send a newline character (endl). For example:
cout << "Never fear, C++ is here!" << endl;
- Most C++ statements are terminated by a semicolon (;). Directives—lines beginning with a pound sign (#)—are a major exception.
- Double slashes (//) indicate a comment; all text to the end of the line is ignored by the compiler itself. But comments can be read by humans who have to maintain the program.
Before using a variable, you must declare it. For example:
double x; // Declare x as a floating-pt variable.
- Variables that may store a fractional portion should have type double. This stands for “double-precision floating point.” The single-precision type (float) should be used only when storing large amounts of floating-point data on disk.
To get keyboard input into a variable, you can use the cin object. For example:
cin >> x;
You can also put data into a variable by using assignment (=). This operation evaluates the expression on the right side of the equal sign (=) and places the value in the variable on the left side. For example:
x = y * 2; // Multiply y times 2, place result in x.