Learn Programming with C++
C++in the mid 1980s. C++is an extension of the programming language C, a product of AT&T Bell Labs from the early 1970s. C++’s close relationship to C allows C++ programs to utilize a large collection of code developed in C.
Source code to target code sequence Structure
Examples of software written in C++ include Microsoft Windows 8, Microsoft Office, macOS, and Adobe Creative Suite.
the needs of commercial software development and accomplish all that it does, C++ itself is complex.
Writing a C++ Program
Properly written C++ programs have a particular structure. The syntax must be correct, or the compiler will generate error messages and not produce executable machine language. This chapter introduces C++ by providing some simple example programs and associated fundamental concepts. Most of the concepts presented in this chapter are valid in many other programming languages as well.
General Structure of a Simple C++ Program
#include <iostream>
int main()
{
std::cout << "This is a simple C++ program!\n";
}
Output
This is a simple C++ program!
#include This line is a preprocessing directive. All preprocessing directives with in C++ sourcecodebeginwith a # symbol.
int main() { This specifies the real beginning of our program. Here we are declaring afunction named main. All C++ programs must contain this function to be executable.
std::cout << "This is a simple C++ program!\n";
The body of our main function contains only one statement. This statement directs the executing program to print the message This is a simple C++ program! on the screen. A statement is the fundamental unit of execution in a C++ program. Functions contain statements that the compiler translates into executable machine language instructions.
Variations of our simple program
#include <iostream>
using std::cout;
int main()
{
cout << "This is a simple C++ program!\n";
}
#include <iostream>
using namespace std;
int main()
{
cout << "This is a simple C++ program!\n";
}
The statement in the main function in any of the three versions of our program uses the services of an object called std::cout. The std::cout object prints text on the computer’s screen. The text of the message asitappearsin the C++ source code is called a string, for string of characters.
Arrow C program
#include <iostream>
int main()
{
std::cout << " * \n";
std::cout << " *** \n";
std::cout << " ***** \n";
std::cout << " * \n";
std::cout << " * \n";
std::cout << " * \n";
}
constitute the body of the main function. The body consists of all the statements between the open curly brace({)and close curly brace(}). We say that the curly braces delimit the body of the function. The word delimit means to determine the boundaries or limits of something
Template for simple C++ programs
Our programs generally will print something, so we need the #include directive that brings the std::cout definition from into our program. Depending on what we need our program to do, we may need additional #include directives. The main function definition is required for an executable program, and we will fill its body with statements that make our program do as we wish.
Exercises
- 1.What preprocessor directive is necessary to use statements with the std::cout printing stream object?
- 2. What statement allows the short namecoutto be used instead ofstd::cout?
- 3. What does the namestdstand for?
- 4. All C++ programs must have a function named what?
- 5. The body ofmainis enclosed within what symbols?
- 6. What operator directs information to thestd::coutoutput stream?
- 7. Write a C++ program that prints your name in the console window.