PROGRAMMING WITH C++
LECTURE 2 – WORKING WITH DATA
LECTURER: MRS. ETUNA KAMATI
CONTACTS: E.KAMATI@IUM.EDU.NA / 0818484759
LEARNING OBJECTIVES
• Learn how to work with data of different types in your C++ programs
• Learn how to declare and assign data to variables
• Learn how to use mathematical operators and order of precedence
DECLARING VARIABLES
• Name variables
• Use naming rules for legal class identifiers
• Variable declaration
• A statement that reserves a named memory location
• Includes:
• Data type
• Identifier
• Optional assignment operator and assigned value
• Ending semicolon
DECLARING AND USING CONSTANTS AND
VARIABLES
• Constants
• A constant, like a variable, is a memory location where a value can be stored.
• The value of a constant cannot be changed while program is running
• Constants must be initialised at creation.
• C++ has two types of constants: literal and symbolic.
• Literal constants
• Value taken literally at each use
DECLARING AND USING CONSTANTS AND
VARIABLES
• Numeric constants
• A constant declared to store a numeric value
• Unnamed constants
• No identifier is associated with it
• Named constants
• Has a data type, name, and value
• Can be assigned a value only once
The keyword const is used when declaring a new symbolic constant.
C++ IDENTIFIERS
• A C++ identifier is a name used to identify a variable, function, class, module, or any other user-
defined item.
• An identifier starts with a letter A to Z or a to z or an underscore (_) followed by zero or more
letters, underscores, and digits (0 to 9).
• Punctuation characters such as @, $, and % are not allowed within identifiers.
• C++ is a case-sensitive programming language.
C++ RESERVED KEYWORDS
FUNDAMENTAL / PRIMITIVE DATA TYPES
• Boolean – bool
• Characters – char, wchar_t
• Integers – short, int, long
• Floating point values – float, double, long double
• String of characters - string
BOOLEAN DATA TYPES
• The result of a comparison or a logical association using AND or OR is a boolean value,
• The result can be either true or false.
• C++ uses the bool type to represent boolean values.
• true will be represented as the numerical value 1 and false by a zero.
• E.g. bool isfridaytoday;
RELATION OPERATORS FOR BOOLEAN
EXPRESSIONS
CHARACTER DATA TYPES
• The char type is used to store character codes in 1 byte (8 bits).
• The wchar_t (wide character type) type comprises at least 2 bytes (16 bits)
EXAMPLES OF CHARACTER CONSTANTS
Constant Character Constant Value
(ASCII code decimal)
'A' Capital A 65
'a' Lowercase a 97
'' Blank 32
'.' Dot 46
'0' Digit 0 48
'\0' Terminating null character 0
EXAMPLES OF CHARACTER CONSTANTS
SAMPLE PROGRAM USING CHARACTERS
• #include <iostream>
• using namespace std;
• int main()
• {
• cout << "\nThis is\t a string\n\t\t"
• " with \"many\" escape sequences!\n";
• return 0;
• }
RANGE OF CHARACTER AND INTEGER DATA
TYPES
• Order of values:
• char <= short <= int <= long
• The short type comprises at least 2 bytes and the long type at least 4 bytes.
• The current value ranges are available in the climits header file.
• This file defines constants such as CHAR_MIN, CHAR_MAX, INT_MIN, and INT_MAX, which
represent the smallest and greatest possible values.
RANGE OF INTEGER DATA TYPE
• #include <iostream>
• #include <climits> // Definition of INT_MIN, ...
• using namespace std;
• int main()
• {
• cout << "Range of types int and unsigned int"
• << endl << endl;
• cout << "Type Minimum Maximum"
• << endl
• << "--------------------------------------------"
• << endl;
• cout << "int " << INT_MIN << " "
• << INT_MAX << endl;
• cout << "unsigned int " << " 0 "
• << UINT_MAX << endl;
• return 0;
FLOATING-POINT NUMBER DATA TYPES
• Floating-point numbers represent decimal numbers.
• Floating-point numbers must be stored to a preset accuracy.
• Types of floating-point numbers:
• float for simple accuracy
• double for double accuracy
• long double for high accuracy
• The value range and accuracy of a type are derived from the amount of memory allocated and the
internal representation of the type.
• Accuracy is expressed in decimal places.
RANGE OF VALUES FOR FLOATING-POINT
NUMBER DATA TYPES
ORDER OF PRECEDENCE OF MATHEMATICAL AND
RELATIONAL OPERATORS
EXERCISE 1
• Exercise 1
• Write a C++ program that prompts the user to enter the following texts and then outputs the
following text on the screen:
Oh what
a happy day!
Oh yes,
what a happy day!
• Use the manipulator endl (end of line) or the new line character (\n) where appropriate.
• Exercise 2
• The following program contains several errors:
• */ Now you should not forget your glasses //
• #include <stream>
• int main
• {
• cout << "If this text",
• cout >> " appears on your display, ";
• cout << " endl;"
• cout << 'you can pat yourself on '
• << " the back!" << endl.
• return 0;
• )
• Rewrite the program in the correct way on paper. (10 marks)
END OF LECTURE