C++ Programming Basics – theteche.com

In any language C++ programming basics there are some fundamentals you need to know before you can write even the most elementary programs. This chapter introduces three such fundamentals: basic program construction, variables, and input/output (I/O). It also touches on a variety of other language features, including comments, arithmetic operators, the increment operator, data conversion, and library functions.

These topics are not conceptually difficult, but you may find that the style in C++ is a little austere compared with, say, BASIC or Pascal. Before you learn what it’s all about, a C++ program may remind you more of a mathematics formula than a computer program. Don’t worry about this. You’ll find that as you gain familiarity with C++, it starts to look less forbidding, while other languages begin to seem unnecessarily fancy and verbose.

Getting Started – C++ Programming Basics

As we noted in the Introduction, you can use either a Microsoft or a Borland compiler with this book. Appendixes C and D provide details about their operation. (Other compilers may work as well.) Compilers take source code and transform it into executable files, which your computer can run as it does other programs. Source files are text files (extension .CPP) that correspond with the listings printed in this book. Executable files have the .EXE extension, and can be executed either from within your compiler, or, if you’re familiar with MS-DOS, directly from a DOS window.

The programs run without modification on the Microsoft compiler or in an MS-DOS window. If you’re using the Borland compiler, you’ll need to modify the programs slightly before running them; otherwise the output won’t remain on the screen long enough to see. Make sure to read Appendix D, “Borland C++Builder,” to see how this is done.

Basic Program Construction

Let’s look at a very simple C++ programming basics. This program is called FIRST, so its source file is FIRST.CPP. It simply prints a sentence on the screen. Here it is:

#include <iostream>
using namespace std;

int main()
{
cout<<"Every age has a language of its own\n";
return 0;
}

Despite its small size, this program demonstrates a great deal about the construction of C++ programs. Let’s examine it in detail.

Functions

Functions are one of the fundamental building blocks of C++. The FIRST program consists almost entirely of a single function called main(). The only parts of this program that are not part of the function are the first two lines the ones that start with #include and using. (We’ll see what these lines do in a moment.)

We noted in before post, “The Big Picture,” that a function can be part of a class, in which case it is called a member function. However, functions can also exist independently of classes. We are not yet ready to talk about classes, so we will show functions that are separate standalone entities, as main() is here.

Function Name

The parentheses following the word main are the distinguishing feature of a function. Without the parentheses the compiler would think that main refers to a variable or to some other program element. When we discuss functions in the text, we’ll follow the same convention that C++ uses: We’ll put parentheses following the function name. Later on we’ll see that the parentheses aren’t always empty. They’re used to hold function arguments: values passed from the calling program to the function.

The word int preceding the function name indicates that this particular function has a return value of type int. Don’t worry about this now; we’ll learn about data types later in this chapter and return values in next post, “Functions.”

Braces and the Function Body

The body of a function is surrounded by braces (sometimes called curly brackets). These braces play the same role as the BEGIN and END keywords in some other languages: They surround or delimit a block of program statements. Every function must use this pair of braces around the function body. In this example there are only two statements in the function body: the line starting with cout, and the line starting with return. However, a function body can consist of many statements.

Always Start with main()

When you run a C++ program, the first statement executed will be at the beginning of a function called main(). (At least that’s true of the console mode programs in this book.) The program may consist of many functions, classes, and other program elements, but on startup, control always goes to main(). If there is no function called main() in your program, an error will be reported when you run the program.

In most C++ programs, as we’ll see later, main() calls member functions in various objects to carry out the program’s real work. The main() function may also contain calls to other stand alone functions. This is shown in Figure.

Program Statements

The program statement is the fundamental unit of C++ programming. There are two statements in the FIRST program: the line
cout << “Every age has a language of its own\n”;
and the return statement
return 0;

The first statement tells the computer to display the quoted phrase. Most statements tell the computer to do something. In this respect, statements in C++ are similar to statements in other languages. In fact, as we’ve noted, the majority of statements in C++ are identical to statements in C.

A semicolon signals the end of the statement. This is a crucial part of the syntax but easy to forget. In some languages (like BASIC), the end of a statement is signaled by the end of the line, but that’s not true in C++. If you leave out the semicolon, the compiler will often (although not always) signal an error.

Read More Topics
Relationship between C and C++
Characteristics of object oriented programming
What is an array in C language?
Dynamic memory allocation array in C
Program testing and debugging in C

About the author

Santhakumar Raja

Hi, This blog is dedicated to students to stay update in the education industry. Motivates students to become better readers and writers.

View all posts

Leave a Reply