KCA University Nairobi, Kenya Bit 2108: Object Oriented Programming
KCA University Nairobi, Kenya Bit 2108: Object Oriented Programming
Nairobi, Kenya
// this is an example of
// c++ program
// thank you
The c comment symbols /* ….*/ are still valid and more suitable for multi line comments.
/* this is an example of c++ program */
is an input statement and causes the program to wait for the user to type
in a number.
The number keyed in is placed in the variable number1. The
identifier cin is a predefined object in C++ that corresponds to the
standard input stream. Here this stream represents the key board.
The operator >> is known as get from operator. It extracts value from
Fig.3.2. Output using insertion operator
the keyboard and assigns it to the variable on its right (Fig.3.2).
10/21/2021 BIT 2108 5
Week 4: Chapter 3: OBJECT ORIENTED PROGRAMMING IN A
SELECTED LANGUAGE
3.1. Basics of C++
// my first program in C++
3.1.5. Cascading of I/O Operator:
This is a comment line.
cout<<”sum=”<<sum<<”\n”;
cout<<”sum=”<<sum<<”\n”<<”average=”<<average<<”\n”;
cin>>number1>>number2; All lines beginning with two slash signs (//) are
3.1.6. Structure of a Program considered comments and do not have any effect on the
Consider the following program: behavior of the program.
// my first program in C++
#include <iostream>
Program 3.1: Average of two numbers using namespace std;
Include files
Class declaration
Class functions, definition
Main function program
3.3.2. Identifiers
Identifiers refers to the name of variable, functions, array, class etc. created by programmer.
Each language has its own rule for naming the identifiers.
The following rules are common for both C and C++.
In ANSI C the maximum length of a variable is 32 chars but in c++, there is no bar.
a) Variables
In programming, a variable is a container Note: The int data type suggests that the variable can
(storage area) to hold data. only hold integers. Similarly, we can use the double data
To indicate the storage area, each variable type if we have to store decimals and exponentials.
should be given a unique name (identifier).
For example,
The value of a variable can be changed, hence the
int age = 14;
name variable.
int age = 14; // age is 14
age = 17; // age is 17
10/21/2021 BIT 2108 18
Week 4: Chapter 3: OBJECT ORIENTED PROGRAMMING IN A
SELECTED LANGUAGE
3.4. BASIC DATA TYPES IN C++
Example on declaration of variables: A references variable provides an alternative name for a previously defined
variable.
main( ) For example, if we refer to the variable total, then sum and total can be used
{
float x,average; interchangeably to represent the variable.
float sum=0; A reference variable is created as follows:
for(int i=1;i<5;i++) Syntax: Datatype & reference –name=variable name;
{
cin>>x; Example:
sum=sum+x float total=1500;
} float &sum=total;
float average;
average=sum/x; Here sum is the alternative name for variables total; both the variables refer to the
cout<<average; same data object in the memory.
}
A reference variable must be Literals are data used for representing fixed values. They can be used directly in
initialized at the time of the code. For example: 1, 2.5, 'c' etc.
declaration. Here, 1, 2.5 and 'c' are literals. Why? You cannot assign different values to these
Note that C++ assigns additional
terms.
meaning to the symbol & here &
The following is a list of different literals in C++ programming.
is not an address operator.
The notation float & means 1. Integers
reference to float. An integer is a numeric literal (associated with numbers) without any fractional
or exponential part. There are three types of integer literals:
Example:
•decimal (base 10)
int n[10];
int &x=n[10]; •octal (base 8)
char &a=’\n’; •hexadecimal (base 16)
marks. For example: 'a', 'm', 'F', '2', '}' etc. \r Return
\t Horizontal tab
4. Escape Sequences
\v Vertical tab
Sometimes, it is necessary to use characters
\\ Backslash
that cannot be typed or has special meaning \' Single quotation mark
\0 Null Character
5. String Literals
A string literal is a sequence of characters enclosed in double-quote marks. For example (Table3.4):
Table 3.4. String Literals
5. String Literals
Defining symbolic constants Study the program below that uses a constant ,
compile and run it.
[i]. Using #define preprocessor.
Study the program below then compile and run.