BCE-5
BCE-5
LEARNING OBJECTIVES
At the end of this chapter you will be able to understand :
Introduction and Feature of C++
Character and Token
Precedence and Associativity
Program Structure
Data Types and Operators
Variables and Their Scope
Expressions
5.3. TOKEN
In a passage of text, individual words and punctuation marks are called token or lexical
elements.
L Keywords are the reserved word that have special to the language
compiler they cannot use as identifiers in your program.
L Constants are data items that never change the value during the execution
of program.
Output:
Hello, world.
1. Comment section. The programmer can use comment section for short explanations
of the source code itself or other comment. In above program the comment section
display the following comment:
// This Program is Written By Pawan Thakur // comment section
2. Preprocessor directive. Lines beginning with a hash sign (#) are directives for the
preprocessor. They are not regular code lines with expressions but indications for the
compiler's preprocessor. For Ex.
#include<iostream.h>
In this case the first directive #include <iostream.h> tells the preprocessor to include the
iostream.h standard file.
3. Main function definition. The main function is the point by where all C++ programs
start their execution, independently of its location within the source code. It is essential
that all C++ programs have a main function.
void main()
{
----------
body of main
----------
}
This line corresponds to the beginning of the definition of the main function. The word
main is followed in the code by a pair of parentheses (). That is because it is a function.
4. Body of main. It contains the set of statements. A statement is a simple or compound
expression that can actually produce some effect. C++ uses curly-braces {} to group
things together.
cout represents the standard output stream in C++, and the meaning of the entire statement
is to insert a sequence of characters into the standard output stream.
INTRODUCTION OF C++, DATA TYPES AND OPERATORS 5-7
cout<<"Hello, world."<<endl;
In this case, Hello World is the sequence of characters which display on screen. The
statement ends with a semicolon character (;). This character is used to mark the end of
the statement and in fact it must be included at the end of all expression statements in all
C++ programs.
L Data type specifies the size and type of values that can stored in computer
memory.
Output:
Output-:
(c) Long Integers. An integer variable whose value should be positive can also be
declared with the long keyword. The long keyword is a positive 32-bit integer whose
value ranges from 0 to 4,294,967,295. The program 5.3 displays the example integer
data type.
Program 5.3. Example of Long Integer Data Type
#include <iostream.h>
#include<conio.h>
void main()
{
clrscr();
long UArea;
unsigned long Population;
cout << "What is the area of the U? ";
cin >> UArea;
cout << "What is the population of the U? ";
cin >> Population;
cout << "\nCharacteristics of the U";
5-10 INTRODUCTION OF C++, DATA TYPES AND OPERATORS
cout << "\n\tArea = " << UArea<< "\n\tPopulation = "
<< Population;
getch();
}
Output-:
2. Floating - Point. The integers we have used so far have the main limitation of not
allowing decimal values. C++ provides floating identifier values that would solve this
problem. Floating point is another type of integer, used to hold numbers containing decimal
parts such as 34.456 and -12.780, 15.89 etc. Float and Double are two types of floating
point variables in C++.
(a) Float. The most fundamental floating variable is declared with the float keyword.
The value typically fits in 32 bits (4 bytes). To declare a variable that would hold decimal
values we can use the float data type. The program 5.4 displays the example of float
data type.
Program 5.4. Example of Float Data Type
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
float side, perimeter, area;
cout<<"Enter the side of the square: ";
cin>>side;
perimeter = side * 4;
area = side * side;
cout<<"Characteristics of the square:";
cout<<"\nSide: "<<side;
cout<<"\nPerimeter: "<<perimeter;
cout<<"\nArea: "<<area;
getch();
}
INTRODUCTION OF C++, DATA TYPES AND OPERATORS 5-11
Output-:
(b) Double. When a variable is larger than the float we can use double identifier. The
double-precision identifier is an 8 Byte. The program 5.5 displays the example of double
data type.
Program 5.5. Example of Double Data Type
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
double side, perimeter, area;
cout<<"Enter the side of the square: ";
cin>>side;
perimeter = side * 4;
area = side * side;
cout<<"Characteristics of the square:";
cout<<"\nSide: "<<side;
cout<<"\nPerimeter: "<<perimeter;
cout<<"\nArea: "<<area;
getch();
}
Output :
3. Void. The void was introduce in ANSI C. It is use to specify the return type of function
when it is not returning any value and indicate and empty argument list to a function.
For example : void getsum (void) ;
Output-:
(b) Boolean Type. In C++, Boolean data type is used for logical values. It can have
only one value of two possible values, true or false. The program 5.7 displays the example
of boolean data type.
Program 5.7. Boolean Data Type
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
INTRODUCTION OF C++, DATA TYPES AND OPERATORS 5-13
bool MachineIsWorking = true;
cout<<"Since this machine is working, its value is"
<<MachineIsWorking<<endl;
MachineIsWorking = false;
cout<<"The machine has stopped operating."
<<"Now its value is "<<MachineIsWorking<<endl;
getch();
}
5.6.3. User define data type. User defined data type are those data type which is
defined by user. C++ support following user defined data type :
(a) Class. A class is user define data type, a C++ program can have definition of
different classes. A class contains variables and functions. Variables are called instances
and functions are called method. Class is a template, which declare and define methods
that are called by the objects of the classes.
(b) Structure. A structure is a collection of different types of variables works under one
name, providing a convenient means of keeping related information. For example a
student record is a collection of rollno, name, class, marks, grade etc.
(c) Union. A union is a memory location that is shared by two or more different variables
at different times.
(d) Enumeration. An alternative method for naming integer constants is often convenient
then constant. This can be achieved by creating enumeration using keyword enum. For
example, enum{Start, Pause, Play}.
5.6.4. Derived data type. From the built in data types other data type can be derived by
using the declaration operations. C++ support following non-primitive derived data type:
(a) Arrays. An array is a derived data type. It is a container object that holds a fixed
number of values of a single type.
(b) Function. Function is a derived data type. The brief introduction of function is in 6
chapter.
(c) Pointer. Pointer is special type of variable which hold the address of another variable.
5.7. VARIABLES
A variable is a place to store information. A variable is a location in your computer’s
memory in which you can store a value and from which you can later retrieve that value.
These are two valid declarations of variables. The first one declares a variable of type
int with the identifier a. The second one declares a variable of type float with the identifier
mynumber. Once declared, the variables a and mynumber can be used within their
scope in the program. The program 5.8 displays the example of variables.
Program 5.8. Example of variables
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a, b;
int result;
a = 5;
b = 2;
a = a + 1;
result = a - b;
cout<<result;
getch();
}
Operator Descriptions
The operations of addition, subtraction, multiplication and division are done by the
mathematical operators.
5-16 INTRODUCTION OF C++, DATA TYPES AND OPERATORS
2. Relational operators and equality. The equality and relational operators are used
if one operand is greater than, less than, equal to or not equal to another operand. The
Table 5.5 describes the equality and relational operators and their descriptions.
Table5.3 Equality and Relational operators
Operator Descriptions
== Equal to
!= Not equal to
> Greater than
>= Greater than or equal to
< Less than
<= Less than or equal to
Note: Keep in mind that you must use "==", not "=", when testing if two primitive values
are equal.
3. Assignment operator (=). The assignment operator assigns a value to a variable.
For Example: a = 5;
This statement assigns the integer value 5 to the variable a. The program 5.10 displays
the example of assignment operator.
Program 5.10. Example of assignment operator
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a, b;
INTRODUCTION OF C++, DATA TYPES AND OPERATORS 5-17
a = 10;
b = 4;
a = b;
b = 7;
cout<<"a:";
cout<<a;
cout<<" b:";
cout<<b;
getch();
}
4. Increment and decrement operator (++, --). In C++, the increase operator (++)
and the decrease operator (--) used to increase or reduce by one the value stored in a
variable. They are equivalent to +=1 and to -=1, respectively. Thus:
c++;
c+=1;
c=c+1;
The above three statements are all equivalent in its functionality increase by one the
value of c.
A characteristic of this operator is that it can be used both as a prefix and as a suffix.
That means that it can be written either before the variable identifier (++a) or after it
(a++). Although in simple expressions like a++ or ++a both have exactly the same
meaning. But other expressions in which the result of the increase or decrease operation
is evaluated as a value in an outer expression they may have an important difference in
their meaning: Notice the difference in table 5.6:
Table 5.6 Increase and decrease operator
Example 1 Example 2
B=3; B=3;
A=++B; A=B++;
// A contain 4, B contain 4 // A contain 3, B contain 4
If condition is true, the expression will return result1, if it is not true it will return
result2. The program 5.11 displays the example of conditional operators.
Program 5.11. Example of conditional operators
#include<iostream.h>
#include<conio.h>
void main ()
{
clrscr();
int a,b,c;
a=2;
b=7;
c = (a>b) ? a : b;
cout << c;
getch();
}
In this example a is 2 and b is 7, the expression being evaluated (a>b) was not true, thus
the first value specified after the question mark was discarded in favor of the second
value (the one after the colon) which was b, with a value of 7.
7. Compound assignment (+=, -=, *=, /=, %=, >>=, <<=, &=,^=, |=).
When we want to modify the value of a variable by performing an operation on the value
currently stored in that variable we can use compound assignment. The program 5.12
displays the example of assignment operator.
Program 5.12. Example of compound assignment operator
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a, b=3;
a = b;
a+=2; // equivalent to a=a+2
cout<<a;
getch();
}
9. The size of() operator. This operator accepts one parameter, which can be either a
type or a variable itself and returns the size in bytes of that type or object:
a = sizeof(char);
This will assign the value 1 to a because char is a one-byte long type. The value returned
by sizeof is a constant, so it is always determined before program execution.
This is an expression not only adds a and b but also assigns the result to x and returns the
value of that assignment (the value of x) as well.
y = x = a + b;
This line is evaluated in the following order: Add a to b. Assign the result of the expression
a + b to x. Assign the result of the assignment expression x = a + b to y. If a, b, x, and y
are all integers, and if a has the value 2 and b has the value 5, both x and y will be
assigned the value 7. The program 5.12 displays the example of expressions.
5-20 INTRODUCTION OF C++, DATA TYPES AND OPERATORS
Program 5.13. Example of Expressions
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a=0, b=0, x=0, y=35;
cout<<"a: "<<a<<" b: "<<b;
cout<<" x: "<<x<<" y: "<<y<< endl;
a = 9;
b = 7;
y = x = a+b;
cout<<"a: "<<a<<" b: "<<b;
cout<<" x: "<<x<<" y: "<<y<< endl;
getch();
}
POINTS TO REMEMBER
(i) C++ is not a pure object oriented language.
(ii) Pure object oriented language purely deals with classes objects which means you
can not write even very simple program without using a class. If you want to
display “Hellow World” you have to write this with in class.
(iii) Java is pure object oriented language.
(iv) If you want to convert one data type to another data type you have to use type
cast.
(v) The small individual unit of C++ program is known as a token.
(vi) A constant value in C++ is created by using a literal.
(vii) Data type specifies the size and type of values that can stored in computer
memory.
(viii) The range of short integers is from – 32768 to 32767.
(ix) Class is user define data type.
(x) Operators are special symbols that perform mathematical or logical operation.
KEY TERMS
❍ Character set ❍ Token
❍ Identifiers ❍ Constant
❍ Basic data type ❍ User defined data type
❍ Integer type ❍ Floating type
❍ Boolean ❍ Character type
INTRODUCTION OF C++, DATA TYPES AND OPERATORS 5-21
❍ Derived data type ❍ Associativity and precedence
❍ Operator ❍ Arithmetical and logical
❍ Increment and decrement ❍ Assignment operator
ANSWERS
1. (a) 2. (b) 3. (b) 4. (c) 5. (b)
6. (b) 7. (a) 8. (a) 9. (b) 10. (c)
UNSOLVED QUESTIONS
1. What is C++ ? Explain the features of C++.
2. Explain different token in C++.
3. Write down the structure of C++ program.
4. What do you mean by data type ? Explain different data type in C++.
5. What is the different between user defined and derived data type ?
6. Explain basic data type in C++.
7. What is variable ? Explain the scope of variables in C++.
8. What is operator ? Explain different type of operators in C++.
9. Explain the concept expression in C++.
10. What is the difference between C and C++ ?
❍❍❍