YOUR FIRST C++ PROGRAM
We now introduce C++ programming, which facilitates a disciplined approach to program design. Most of the C++ programs you’ll study in this book process information and display results. In this Lesson, we present five examples that demonstrate how your programs can display messages and obtain information from the user for processing.The first three examples simply display messages on the screen. The next obtains two numbers from a user, calculates their sum and displays the result. The accompanying discussion shows you how to perform arithmetic calculations and save their results for later use.
The fifth example demonstrates decision-making by showing you how to compare two numbers, then display messages based on the comparison results. We analyze each program one line at a time to help you ease your way into C++ programming.
1
2
3
4
5
6
7
| #include
// function main begins program execution
int main()
{
std::cout << "Hello Word" << std::endl;
}
|
Output
Welcome to C++!
#include Preprocessor Directive
Line 1 #include <iostream>
Comments
Line 3 begin with //, indicating that the remainder of each line is a comment. Comments do not cause the computer to perform any action when the program is run—they’re ignored by the C++ compiler and do not cause any machine-language object code to be generated.
its a document to your programs and to help other people to read and understand your code. Its always good practice That every program/code should begin with a comment that describes the purpose of the program/code.
is a part of every C++ program. The parentheses "()" after main indicate that main is a program building block called a function. C++ programs typically consist of one or more functions and classes (as you’ll learn in the Subsequence Lessons ). Exactly one function in every program must be named main. In this Demo Example contains only one function. C++ programs begin executing at function main, even if main is not the first function in the program
No comments:
Post a Comment