Download as key, pdf, or txt
Download as key, pdf, or txt
You are on page 1of 30

Basic C++ Programming

Contents
Keywords, Variables and Identifiers
Variable Declaration & Definition
Basic C++ Variable Types
Assignment Statement
Variable Initialization and Memory Concepts
Input with cin
Cascading of Input/Output Operator
setw Manipulator
Objectives
To understand how to declare the variables and assign values to
them
To know different data types of the variables and their size
To understand how to accept the user’s input using cin object
To know use of setw manipulator
Learning Outcomes
After completing this lecture, students will be able to:
Understand the concept of variables and constants.
Use the concept of data types in C++.
Know the syntax and semantics of data declaration statements and
assignment statements.
Use cin to get information from user.
Use of setw manipulator to set field width for text as well as numbers.
Keywords

Keywords are words reserved as part of the language and special meaning to the
compiler.

Example,

int, return, main, void


They cannot be used by the programmer to name things.
They consist of lowercase letters only.
Variables
Variables

are containers that locate on computer’ memory to store data then use and change its
value in a program.
E.g.; int firstnumber=1;

Memory Variable name Memory firstnumber

Variable value 1
Identifiers
The name given to variables are called identifiers.
The rules for writing identifiers:
use upper or lower case letter
use digit letters 0..9
use the underscore(_)
the first character must be a letter or underscore
The compiler distinguishes the upper and lower letters means
case sensitive.(eg: num and NUM are two different variables.)
As Naming convention, all lowercase letters is used for
variable names.
Identifiers with Examples

Samples of acceptable variable Samples of unacceptable variable


names: names:

Grade My Name
myName 3rd_Number
movie_name first-name
a_123 Grade(Test)
abc int
_temp 123
Variable Declaration and Definition
Variable declaration
declares firstly before it is used.
tells the compiler to allocate enough memory to hold a value of this
data type and to associate the identifier with this location.
Memory

number

1
Syntax: <data type> <name>; 0

Example: int number;//variable declaration


Variable definition
assigns a memory location and a value.
Example: number = 10; //variable definition
Assignment statements
The assignment operator (=) causes the value on the right to be
assigned to the variable on the left.
Example: number = 10; //assignment statement
Basic C++ Variable Types
‘unsigned’ Data Types
Some data which start from zero and include only positive
number. For that kind of data, unsigned data type can be used.
This allows them to represent numbers twice as big as the
signed type.
Example- age, count
Basic C++ Variable Types (Cont’d)
bool

Variables of type bool can have only two possible values: true and false.
Compilers often store them as a byte (for true/false).
Float

stores decimal numbers (7 decimal digits).


and requires 4 bytes of storage.
Double

stores decimal numbers (15 decimal digits) and requires 8 bytes of storage.
Long double

is often the same as double and compiler-dependent.


Floating-point constant
Character Variable

It stores single characters, such as ‘a’ or ‘A’ and is surrounded by single


quotes.
It occupies only one byte (eight bits) of memory.
They are commonly used to store ASCII characters such as ‘a’, ‘B’, ‘$’, ‘3’,
and so on, as numbers.

Example: char var_name; //character variable declaration


var_name = ‘A’; //character variable definition
ASCII
ASCII means American Standard Code for Information
Interchange and it defines a specific way of representing
English characters as number.
uses single quotation marks around a character like ‘A’ and ‘b’.
is translated into the corresponding ASCII code by the
compiler.
Example: var_name=‘A’; (or) var_name=65;
‘a’  97, ‘b’  98
‘A’  65 , ‘B’  99
‘$’  36, ‘0’  48
Basic C++ Variable Types (Cont’d)
Int Variable
Integer variables represent integer numbers and has no fractional part.
For example, int total; total = 1639;

Figure 2.3 shows an integer variable in memory.


Assignment Statement
The statements
total = 1639;
The equal sign = cause the value on the right to
be assign to the variable on the left.
The number 1639 (integer value) is assign to
variable ‘total’.
Example 1 – about int variable
#include<iostream.h>
int main()
var 20
{ 1
int var1; // declaration
var 30
int var2; // declaration 2
var1 = 20; // assignment statement
var2 = var1 + 10; //evaluate expression and then assignment
cout <<“var1 = “ << var1<< endl;
cout<<“var2 =“<< var2;
return 0;
}
Variable Initialization
Variable can be initialized at the same time they are
defined. E.g., int value = 0;
#include<iostream.h> var A/ B
1
int main()
{ char var1= ‘A’; //initialize var \t
char var2=‘\t’; //initialize 2
cout << var1;
cout << var2;
Output Screen
var1= ‘B’;
cout <<var1 ; A_ _ B_
retrun 0;
}
Memory Concepts
Variable names
Correspond to actual locations in computer's memory
Every variable has name, type, size and value
When new value placed into variable, overwrites previous value

int integer1;
integer1= 45; integer1 4
5
int integer2; integer1 4
integer2 = 72; 5
integer2 7
2
int sum = integer1 + integer2;
integer1 4
5
integer2 7
2
su 1
m 1
7
Input with cin
Input operator

called the extraction operator (>>) or get from operator


is used to take (extract) the value through cin and stores it in
the variable.
Syntax: cin >> varName;
cin - prnounced “C in” is an object, predefined in C++ to
correspond to the standard input stream.
Input with cin (Example)

//display the multiplication of the two //display the multiplication of the two numbers using cin
numbers without using cin #include <iostream>
#include <iostream> using namespace std;
using namespace std; int main()
int main() {
{ int x, y;
int x = 2, y = 3; int result;
int result; cin >> x;
result = x * y; cin >> y;
cout << “Result = " << result; result = x * y;
return 0; cout << “Result = " << result;
} Outp
Outp return 0; ut
ut 12
Result = 6 }
3

Result = 36
Cascading of Input/Output Operator
Cascading of the input operator
refers to the consecutive occurrence of input operators in a single
statement.
Example: cin >> a >> b >> c >> d ;
Cascading of the output operator
refers to the consecutive occurrence of output operators in a single
statement.
Example: cout << a << b << c << d ;
The setw Manipulator
The manipulators are operators used with the insertion operator (<<) to modify
—or manipulate—the way data is displayed.
The setw manipulator changes the field width of output.
The field width of integer 567 will occupy a field three numers wide, and the
string “pajamas” will occupy a field seven characters wide.
The setw manipulator causes the number (or string) that follows it in the stream
to be printed within a field n characters wide, where n is the argument to
setw(n).
The value is right justified within the field.
Example setw(10)
cout << setw(10) << “number” << endl;
The setw Manipulator Example 1
#include <iostream.h>
The output should be
#include <iomanip.h> // for setw

int main()
{
long pop1=2425785, pop2=47, pop3=9761;
cout << setw(8) << “LOCATION” << setw(12) << “POPULATION” <<
endl
<< setw(8) << “Portcity” << setw(12) << pop1 << endl
<< setw(8) << “Hightown” << setw(12) << pop2 << endl
<< setw(8) << “Lowville” << setw(12) << pop3 << endl;
return 0;
}
The setw Manipulator Example 2
#include <iostream.h>
#include <iomanip.h> // for setw What is the output of the program? Do you
guess the function of setfill()?
int main()
{
long pop1=2425785, pop2=47, pop3=9761;
cout << setw(8) << “LOCATION” << setw(12) << “POPULATION” <<
endl
<< setw(‘*’)<<setw(8) << “Portcity” << setw(12) << pop1 << endl
<< setw(‘*’)<< setw(8) << “Hightown” << setw(12) << pop2 << endl
<< setw(‘*’)<< setw(8) << “Lowville” << setw(12) << pop3 << endl;
return 0;
}
Summary
Keywords, Variables and Identifiers
Variable Declaration & Definition
Basic C++ Variable Types
Assignment Statement
Variable Initialization and Memory Concepts
Input with cin
Cascading of Input/Output Operator
setw Manipulator
Reading Assignments

1. Chapter(2) - C++ Programming Basics (page.30-71)


Reference book : “Object-Oriented Programming in C++” by Robert Lafore, 4th Edition
Download link : https://docs.google.com/file/d/0B21HoBq6u9TsUHhqS3JIUmFuamc/view
References

1. Object-Oriented Programming in C++ (Fourth Edition) by Robert Lafore


2. Programming Logic and Design Comprehensive (Sixth Edition) by Joyce Farrell
3. Data Structures using C++ by Varsah H. Patil
4. C++ Language Tutorial (e-book)
5. C Programming for Engineering & Computer Science by H.H. Tan and T.B. D’Orazio
Exercise
1. Write a program that generates the following table:
2.

Use a single cout statement for all output.


Exercise
2. By default, output is right-justified in its field. You can left-
justify text output using the manipulator setiosflags(ios::left).
(For now, don’t worry about what this new notation means.) Use
the setiosflags(ios::left) manipulator, along with setw(), to help
generate the following output:

You might also like