3 Programming Fundamentals
3 Programming Fundamentals
Writing Comments
Data Types
I/O Statements
Defining Constants
First Program
// Simple C++ program to display "Hello World"
The C++ program is written using a specific template structure. The structure of the
program written in C++ language is as follows:
Documentation Section:
This section comes first and is used to document the logic of the program that the programmer
going to code.
It can be also used to write for purpose of the program.
Whatever written in the documentation section is the comment and is not compiled by the compiler.
Documentation section is optional since the program can execute without them. Below is the
snippet of the same:
Example:/* This is a C++ program to find the
factorial of a number
The basic requirement for writing this
program is to have knowledge of loops
To find the factorial of number
iterate over range from number to one
*/
Linking Section
Standard libraries section
#include <iostream>
using namespace std;
#include is a specific preprocessor command that effectively copies and pastes the
entire text of the file, specified between the angle brackets, into the source code.
The file <iostream>, which is a standard file that should come with the C++ compiler,
is short for input-output streams. This command contains code for displaying and
getting an input from the user.
This enables the programmer to use standard input, output, and error facilities that are
provided only through the standard streams defined in <iostream>. These standard
streams process data as a stream of characters, that is, data is read and displayed in a
continuous flow.
Definition Section:
int main() {}
The starting point of all C++ programs is the main function.
This function is called by the operating system when your program is executed by the computer.
{ signifies the start of a block of code, and } signifies the end.
Example:
#include <iostream>
using namespace std;
int main() {
cout << "Hello World!" << endl;
return 0;
}
C++ Variables
The name of a variable can be composed of letters, digits, and the underscore character. It must begin with
either a letter or an underscore. Upper and lowercase letters are distinct because C++ is case-sensitive .
A variable is a name given to a memory location. It is the basic unit of storage in a program.
Local variables
Instance variables
Static variables
Local variables: A variable defined within a block or method or constructor is called
local variable.
These variable are created when the block in entered or the function is called and
destroyed after exiting from the block or when the call returns from the function.
The scope of these variables exists only within the block in which the variable is
declared. I.E. We can access these variable only within that block.
class calculation{
int a=10; // instance variable;
static int b=20; //static variable
void add() // this is function (or) method
{
int sum;// creating variables
sum=a+b; // performing addition operation
cout<<“the sum of value is :”<<sum //printing sum of the value
}
};
// driver code
int main() // this is the main function
{
Calculation add(); // we are calling the function
}
Instance variables: instance variables are non-static variables and are declared in a class outside any
method, constructor or block.
As instance variables are declared in a class, these variables are created when an object of the class is created
and destroyed when the object is destroyed..
Instance variable can be accessed only by creating objects.
Wide Character: wide character data type is also a type but this data type has size greater than the normal 8-
bit datatype. Represented by wchar_t. It is gcharacter data enerally 2 or 4 bytes long.
Constants
Constants refer to fixed values that the program may not alter and they are called literals.
Constants can be of any of the basic data types and can be divided into Integer Numerals,
Floating-Point Numerals, Characters, Strings and Boolean Values.
Integer Literals
An integer literal can be a decimal, octal, or hexadecimal constant. A prefix specifies the
base or radix: 0x or 0X for hexadecimal, 0 for octal, and nothing for decimal.
Floating-point Literals
A floating-point literal has an integer part, a decimal point, a fractional part, and an
exponent part. You can represent floating point literals either in decimal form or exponential
form.
Boolean Literals
There are two Boolean literals and they are part of standard C++ keywords −
A value of true representing true.
A value of false representing false.
Character Literals
Character constant is a single symbol enclosed in the single quotes.
‘e’ ‘1’ ‘$’
String Literals
Collection of character is called as string. String literals are enclosed in double quotes.
“Computer”
“Programming”
Defining Constants
There are two simple ways in C++ to define constants −
iostream: iostream stands for standard input-output stream. This header file contains
definitions to objects like cin, cout, cerr etc.
iomanip: iomanip stands for input output manipulators. The methods declared in this
files are used for manipulating streams. This file contains definitions of setw,
setprecision, etc.
fstream: This header file mainly describes the file stream. This header file is used to
handle the data being read from a file as input or data being written into the file as
output.
The Standard Output Stream (cout)
The predefined object cout is an instance of ostream class. The cout object is said to be
"connected to" the standard output device, which usually is the display screen.