Practical 03
Practical 03
Practical 03
3.1 Introduction
C++ is a high-level programming language developed by Bjarne Stroustrup at Bell Labs beginning in
1979 as an enhancement to the C programming language and named it "C with Classes". In 1983 it
was renamed to C++.
3.1.2 Code::Blocks
Code::Blocks is a free, open source cross-platform Integrated Development Environment (IDE) which
supports multiple compilers including GCC, Clang and Visual C++.
using namespace std enables a program to use all the names in any standard C++ header (such as
<iostream>) that a program might include.
The main() function is the first executable function in any C++ program. The braces “{ “and “}” also
known as curly braces, enclose the block of code. The cout<<”My name is Ali Asghar” tells the
computer to print the string constant “My name is Ali Asghar” on the console screen.
The getch() (get character) function waits to get the character from keyboard. If you run your
program without using getch() your program will show the result in just one blink and will vanish
out quickly. When the return statement is used at the end of main, the value 0 indicates that the
program has terminated successfully, and any other value indicates abnormal termination.
Practical 03: Getting familiar with fundamentals of programming using C++
3.1.4 Statements
Statements are the instructions to the computer to work accordingly. In C++ all the statements are
always terminated with the semicolon “;”. This semicolon is known as terminator.
The preprocessor directives are the instruction to the part of the compiler known as preprocessor
and the header files contain the definitions of functions.
The cout statement displays text, numbers, characters and graphical symbols on the console screen.
cout<<”My name is Ali Asghar”;
The text in the double quotation marks ”My name is Ali Asghar” is the string constant and is printed
on the console screen as it is. The operator << is known as insertion or put to operator.
You can use the variable name in cout statement to print its value on the console screen:
cout<<”The addition of two numbers is: “<<Add;
The cin statement is responsible for getting input from the user during run-time.
cin>>variable_name;
3.1.8 Comments
Comments are non-executable lines of code in source code. Comments help make code easier to
understand.
There are two types of comments in C++: Single-line comment and Multi-line comment. In first case
we start comment with the two back slashes “//” , this causes the compiler to consider the whole line
as a comment. The second way is to start the comment with a back slash and an asterisk “/*” and
terminate the comment with an asterisk and back slash “*/”, which cause the certain block of line as
a comment.
3.1.9 Variables
A variable is a name piece of memory location that stores the data temporarily. The name given to
the variable is known as identifier. In C++ there are certain rules for identifiers as given below:
1) It can contain letters form a-z, numbers form 0-9 and an underscore sign.
2) It is case sensitive.
3) The first character of the identifier must be letter or an underscore sign.
4) The identifier should not contain any space (white space) within it.
5) You can also give underscore sign in the middle of the identifier as an space for your ease.
6) The identifier must not be same as any of the keyword (reserved word in C++).
7) The identifier can be as long as you like, but only the first 247 will be recognized.
8) The identifier must be unique throughout the program.
Practical 03: Getting familiar with fundamentals of programming using C++
Some valid identifiers are: Var, var, VAR, Var1, VAR1, Var_one, _Var1, _Var_one_of_one etc.
Some of invalid identifiers are: 1Var, 1_var, void, cout, etc.
Whereas the process of initializing certain value to the variable at the time of declaration is referred
as defining a variable. The line below specifies the syntax of variable definition:
data_type variable_name = value;
Data types specify the type of the data to be stored in a memory location (variable). The data types
available in C++ are given below:
Numerical Range
Data Type Bytes of Memory
Low High
bool true false 1
char -128 127 1
short -32,768 32,767 2
int -2,147,483,648 2,147,483,647 4
long -2,147,483,648 2,147,483,647 4
float 3.4 x 10-38 3.4 x 1038 4
double 1.7 x 10 -308 1.7 x 10308 8
long double 1.2 x 10-4932 1.2 x 104932 10
The keyword const is known as the constant qualifier. It specifies that the value of the variable will
remain constant and will not be altered throughout the program.
const float PI = 3.1415F;
It specifies that the variable PI stores a floating point number 3.1415 and its value will not be altered.
3.2 Procedure
Consider the following problem example, we are writing C++ source code in Code::Blocks IDE.
Problem statement: Write a program in C++ that accepts the width and the height of a rectangle
from the user and prints the area of the rectangle.
int main()
{
float height, width, area;
getch();
return 0;
}
EXERCISE
1. Briefly answer the following questions:
2. Write a complete program that calculates and displays the product of three integers. Add
comments to the code where appropriate.
3. What, if anything, prints when each of the following C++ statements is performed? If
nothing prints, then answer “nothing.” Assume x = 2 and y = 3.
a. cout << x;
b. cout << x + x;
c. cout << "x=";
d. cout << "x = " << x;
e. cout << x + y << " = " << y + x;
f. z = x + y;
4. Write a program that asks the user to enter two numbers, obtains the two numbers from
the user and prints the sum, product, difference, and quotient of the two numbers.
5. Consider the following C++ program that calculates the area of triangle;
#include <iostream>
include <conio>
int MAIN()
}
char height, base, area;
getch()
return;
{
6. For each of the following, specify whether it is a valid or invalid identifier. In case of invalid
identifier give the reason i.e. why it is invalid?
7. For each of the data item given below, write down the example value and the data type.