Computer Programming in C++: (PRACTICAL#03)
Computer Programming in C++: (PRACTICAL#03)
in
C++
(PRACTICAL#03)
Objective: To become familiar with the basic C++
program structure, source code and object code,
language translators, primitive data types,
identifiers and escape sequence.
Program statements /Program instructions and Program
Syntax
Source code and object code , source file and object file
Executable file
Language Translators (Interpreter, Compiler, Assembler)
Data Types
Escape Sequence
Introduction to C++
Do this
Introduction to C++
Anatomy of C++ basic program
#include<iostream>
#include<conio.h>
int main()
{
cout<<“17 Software ";
getch();
return 0;
}
Anatomy of C++ basic program
Anatomy of C++ basic program
main() function
The main is the name of the function.
Every C++ program must contain at least one function i.e. main()
function.
Main function is the gateway of any C++ program because every
program starts from the main function.
Every function name is followed by () parenthesis.
The int is the return type of the function, which specifies that, at the
end, the main function will return one value to the operating system
whose data type will be integer.
Outputting with cout
In C++, cout is Standard Output Stream, cout statement is used to
print/display the text or numbers on the monitor screen.
Language translator is a program that converts the source code into the
object code.
Language Translators (Compiler)
• int • float
• short • double
• long • boolean
• char
Integer data Types
Variable: A variable is a space in the computer’s memory set a side for a certain kind of data
and gives it a name for easy reference.
Integers : store whole number, integers have no fractional part. The value can be –ve or +ve.
There are several integer types.
Data Type Description
int Variables of this type occupy 4 bytes (32 bits) in memory
and can have values from -2147483648 to 2147483647
int var1;
int var2;
Variable Declaration Definition and
Initialization
variable Initialization: To combine a variable declaration with an assignment
operator so that a variable is given a value at the same time.
Example Assignment statement int var1 = 5 ;
Identifiers : the names given to variables are called identifiers.
Short
The most commonly used integer type is int. It is a signed 32-bit type that has a range from –2,147,483,648
to 2,147,483,647.
In addition to other uses, variables of type int are commonly employed to control loops and to index arrays.
Although you might think that using a byte or short would be more efficient than using an int in situations in
which the larger range of an int is not needed.
long : is a signed 64-bit type and is useful for those occasions where an int type is not large enough to hold
the desired value. The range of a long is quite large.
This makes it useful when big, whole numbers are needed.
long milliseconds;
long population;
Integer Data Types Example
Program
int main(){
int numberOfClasses, studsPerClass, totalStudents;
numberOfClasses = 10;
studsPerClass = 6;
totalStudents = numberOfClasses * studsPerClass;
cout<< studsPerClass <<" students Per Class and";
cout<<number Of Classes<< " Clsses, then");
cout<<"the total number of students:" + totalStudents;
getch();
Return 0;
Floating Point Data Types
}
Escape Sequences
Are special characters , they don’t appear on the screen rather their effect can be seen.
Escape Sequence Description
\n Newline. Position the screen cursor to the beginning of the next line.
\t Horizontal tab. Move the screen cursor to the next tab stop.
\r Carriage return. Position the screen cursor to the beginning of the current line;
do not advance to the next line. Any characters output after the carriage
return overwrite the characters previously output on that line.
\\ Backslash. Used to print a backslash character.
\’ Single quote.
\" Double quote. Used to print a double-quote character. For example,
Cout<<"\"in quotes\"" ; // displays
"in quotes"
Input with cin>>
Task #3
Write a program in C++ that asks the user to enter mass
in Kilograms and converts into grams and displays on
the monitor screen.