0% found this document useful (0 votes)
36 views5 pages

Basics of C++ (C++ Program Structure)

The document provides an overview of the basic structure of a C++ program, including the use of preprocessor directives, the main function, and input/output operations using cout and cin. It explains the significance of comments in C++ code and the proper syntax for single and multi-line comments. Additionally, it emphasizes the importance of readability and clarity in programming through comments and variable naming conventions.

Uploaded by

ephremmulu486
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views5 pages

Basics of C++ (C++ Program Structure)

The document provides an overview of the basic structure of a C++ program, including the use of preprocessor directives, the main function, and input/output operations using cout and cin. It explains the significance of comments in C++ code and the proper syntax for single and multi-line comments. Additionally, it emphasizes the importance of readability and clarity in programming through comments and variable naming conventions.

Uploaded by

ephremmulu486
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

12/19/2022

CH-2
Basics of C++ (C++ Program Structure)

Basics of C++ (C++ Program Structure)


1. The parts of a C++ Program.
To understand the basic parts of a simple program in C++;let’s have a look at the following
code: #include<iostream>
using namespace std;
int main()
{
cout<<”\n Hello World!”;
return 0;
}

1
12/19/2022

Cont…
▪ Any C++ program file should be saved with file name extension ".CPP"
▪ Type the program directly into the editor, and save the file as hello.cpp, compile it and then run it.
It will print the words Hello World! On the computer screen.
▪ The first character is the #. This character is a signal to the preprocessor. Each time you start your
compiler, the preprocessor runs through the program and looks for the pound (#) symbols and act
on those lines before the compiler runs.
▪ Theinclude instruction is a preprocessor instruction that directs the compiler to include a copy of
the file specified in the angle brackets in the source code.
▪ If the path of the file is not specified, the preprocessor looks for the file under c:\tc\include\
folder or in include folder of the location where the editor is stored.

Cont….
▪ The effects of line 1, i.e. include<iostream> is to include the file iostreaminto the program as if the
programmer had actually typed it.
▪ When the program starts, main() is called automatically.
▪ Every C++ program has a main() function.
▪ The return value type for main() here is void, which means main function will not return a value
to the caller (which is the operating system).
▪ The main function can be made to return a value to the operating system.
▪ The Left French brace “{“signals the beginning of the main function body and the corresponding
Right French Brace “}” signals the end of the main function body. Every Left French Brace needs to
have a corresponding Right French Brace.
• The lines we find between the braces are statements or said to be the body of the function.

2
12/19/2022

Cont….

▪ A statement is a computation step which may produce a value


or interact with input and output streams.
▪ The end of a single statement ends with semicolon (;).
▪ The statement in the above example causes the sting “Hello
World!” to be sent to the “cout” stream which will display it on
the computer screen.

A brief look at cout and cin

▪ Cout is an object used for printing data to the screen.


▪ To print a value to the screen, write the word cout, followed by the
insertion operator also called output redirection operator (<<) and the
object to be printed on the screen.
▪ Syntax: Cout<<Object;
▪ The object at the right hand side can be:
o A literal string: “Hello World”
o A variable: a place holder in memory

3
12/19/2022

▪ Cin is an object used for taking input from the keyboard.


▪ To take input from the keyboard, write the word cin, followed by the input
redirection operator (>>) and the object name to hold the input value.
▪ Syntax: Cin>>Object
▪ Cin will take value from the keyboard and store it in the memory. Thus the
cin statement needs a variable which is a reserved memory place holder.
▪ Both << and >> return their right operand as their result, enabling multiple
input or multiple output operations to be combined into one statement. The
following example will illustrate how multiple input and output can be
performed:

Cont….
• E.g.:
▪ Cin>>var1>>var2>>var3;
• Here three different values will be entered for the three variables. The
input should be separated by a space, tan or newline for each variable.
▪ Cout<<var1<<”, “<<var2<<” and “<<var3;
• Here the values of the three variables will be printed where there is a “,”
(comma) between the first and the second variables and the “and” word
between the second and the third.

4
12/19/2022

Putting Comments on C++ programs

▪ A comment is a piece of descriptive text which explains some aspect of a program.


▪ Program comments are text totally ignored by the compiler and are only intended to
inform the reader how the source code is working at any particular point in the
program.
▪ C++ provides two types of comment delimiters:
o Single Line Comment: Anything after // {double forward slash} (until the end of the
line on which it appears) is considered a comment.
• o Eg: cout<<var1; //this line prints the value of var1
o Multiple Line Comment: Anything enclosed by the pair /* and */ is considered a
comment.

Cont….
o Eg:
• /*this is a kind of comment where
• Multiple lines can be enclosed in
• one C++ program */
▪ Comments should be used to enhance the readability of a program. The following points, in particular,
should be noted:
o A comment should be easier to read and understand than the code which it tries to explain. A
confusing or unnecessarily-complex comment is worse than no comment at all.
o Over-use of comments can lead to even less readability. A program which contains so much comment
that you can hardly see the code can by no means be considered readable.
• Use of descriptive names for variables and other entities in a program, and proper indentation of the
code can reduce the need for using comments.

You might also like