You are on page 1of 14

Medi-Caps University, Indore

Computer Science

Academic Year 2020-21

Bachelor of Computer Application (BCA)

Theory - 2
on

Object Oriented Programming

CA3CO05

Submitted to: Submitted by:


Dr. Arpit Neema Parth Gupta

Assistant Professor Roll No.: SC20CS301041

Department of Computer Application Class & Sem: BCA

2nd Sem Medi-Caps University, Indore


Q.1 WHAT ARE TOKENS IN C++?EXPLAIN

EACH ONE.

ANS.

A token is the smallest element of a C++ program that is

meaningful to the compiler. The C++ parser recognizes these

kinds of tokens: identifiers, keywords, literals, operators,

punctuators, and other separators. A stream of

these tokens makes up a translation unit.

Tokens are the smallest elements of a program, which are meaningful to the
compiler.
The following are the types of tokens: Keywords, Identifiers, Constant, Strings,
Operators, etc.
Let us begin with Keywords.

Keywords
Keywords are predefined, reserved words in C and each of which is associated with specific features. These words help us to
use the functionality of C language. They have special meaning to the compilers.

There are total 32 keywords in C.

auto double int struct

break else long switch


case enum register typedef

char extern return union

continue for signed void

do if static while

default goto sizeof volatile

const float short unsigned

Identifiers
Each program element in C programming is known as an identifier. They are used for naming of variables, functions, array
etc. These are user-defined names which consist of alphabets, number, underscore ‘_’. Identifier’s name should not be same or
same as keywords. Keywords are not used as identifiers.

Rules for naming C identifiers −

• It must begin with alphabets or underscore.

• Only alphabets, numbers, underscore can be used, no other special characters, punctuations are allowed.

• It must not contain white-space.

• It should not be a keyword.

• It should be up to 31 characters long.

Strings
A string is an array of characters ended with a null character(\0). This null character indicates that string has ended. Strings
are always enclosed with double quotes(“ “).

Let us see how to declare String in C language −

• char string[20] = {‘s’,’t’,’u’,’d’,’y’, ‘\0’};


• char string[20] = “demo”;
• char string [] = “demo”;
Here is an example of tokens in C language,
Q.2 HOW MANY TYPES OF DATA TYPES
AVILABLE UN C++? EXPLAIN ALL BY
EXAMPLE.
ANS= There are five data types for C: void, int, float, double, and char.

Type Description

void associated with no data type


int integer

float floating-point number

double double precision floating-point number

char character

You may like to store information of various data types like character, wide character, integer, floating point, double floating

point, boolean etc. ... Based on the data type of a variable, the operating system allocates memory and decides what can be

stored in the reserved memory.

Data Type Meaning Size (in Bytes)

int Integer 2 or 4

float Floating-point 4

double Double Floating-point 8

char Character 1

wchar_t Wide Character 2

bool Boolean 1

void Empty 0
1. C++ int
The int keyword is used to indicate integers.

Its size is usually 4 bytes. Meaning, it can store values from -2147483648 to
2147483647.

For example,

int salary = 85000;

2. C++ float and double


float and double are used to store floating-point numbers (decimals and
exponentials).

The size of float is 4 bytes and the size of double is 8 bytes. Hence, double has two
times the precision of float. To learn more, visit C++ float and double.

For example,

float area = 64.74;

double volume = 134.64534;

As mentioned above, these two data types are also used for exponentials. For
example,

double distance = 45E12 // 45E12 is equal to 45*10^12

3. C++ char
Keyword char is used for characters.

Its size is 1 byte.

Characters in C++ are enclosed inside single quotes ' '.

For example,

char test = 'h';

Note: In C++, an integer value is stored in a char variable rather than the character
itself. To learn more, visit C++ characters.
4. C++ wchar_t
Wide character wchar_t is similar to the char data type, except its size is 2 bytes
instead of 1.

It is used to represent characters that require more memory to represent them than
a single char.

For example,

wchar_t test = L'‫ 'ם‬// storing Hebrew character;

Notice the letter L before the quotation marks.

Note: There are also two other fixed-size character types char16_t and char32_t
introduced in C++11.

5. C++ bool
The bool data type has one of two possible values: true or false.

Booleans are used in conditional statements and loops (which we will learn in later
chapters).

For example,

bool cond = false;

6. C++ void
The void keyword indicates an absence of data. It means "nothing" or "no value".

We will use void when we learn about functions and pointers.

Note: We cannot declare variables of the void type.


Q.3 WHAT DO YOU MEAN BY VOID POINTER?
ANS=
A void pointer is a pointer that has no associated data type
with it. A void pointer can hold address of any type and can be
typcasted to any type. Note that the above program compiles
in C, but doesn't compile in C++. In C++, we must explicitly
typecast return value of malloc to (int *)

Q.4 WHAT IS REFERENCE


VARIABLE?EXPLAIN BY EXAMPLE.
ANS=
A reference variable is a variable that points to an object of a given
class, letting you access the value of an object. An object is a compound
data structure that holds values that you can manipulate. A reference
variable does not store its own values. . OpenROAD includes both
system classes and user-defined classes.

Reference variable is an alternate name of already existing variable.


It cannot be changed to refer another variable and should be
initialized at the time of declaration and cannot be NULL. The operator
'&' is used to declare reference variable. ... datatype − The datatype
of variable like int, char, float etc

Q.5 EXPLAIN DIFFERENT OPERATOR IN C++?


ANS=
In programming, an operator is a symbol that operates on a value or a variable.
Operators are symbols that perform operations on variables and values. For example, + is an

operator used for addition, while - is an operator used for subtraction.

TYPES:

1. C++ Arithmetic Operators


Arithmetic operators are used to perform arithmetic operations on
variables and data. For example,

A+B;

Here, the + operator is used to add two variables a and b. Similarly there are various other

arithmetic operators in C++.

Operator Operation

+ Addition

- Subtraction

* Multiplication

/ Division

% Modulo Operation (Remainder after division)

2. C++ Assignment Operators


In C++, assignment operators are used to assign values to variables. For example,
// assign 5 to a

a = 5;

Here, we have assigned a value of 5 to the variable a.

Operator Example Equivalent to

= a = b; a = b;

+= a += b; a = a + b;

-= a -= b; a = a - b;

*= a *= b; a = a * b;

/= a /= b; a = a / b;

%= a %= b; a = a % b;

3. C++ Relational Operators


A relational operator is used to check the relationship between two operands. For example,

// checks if a is greater than b

a > b;

Here, > is a relational operator. It checks if a is greater than b or not.

If the relation is true, it returns 1 whereas if the relation is false, it returns 0.

Operator Meaning Example

== Is Equal To 3 == 5 gives us false

!= Not Equal To 3 != 5 gives us true

> Greater Than 3 > 5 gives us false

< Less Than 3 < 5 gives us true

>= Greater Than or Equal To 3 >= 5 give us false

<= Less Than or Equal To 3 <= 5 gives us true


4. C++ Logical Operators

Logical operators are used to check whether an expression is true or false. If the

expression is true, it returns 1 whereas if the expression is false, it returns 0.

Operator Example Meaning

&& expression1 && expression2 Logical AND.

True only if all the operands are true.

|| expression1 || expression2 Logical OR.

True if at least one of the operands is true.

! !expression Logical NOT.

True only if the operand is false.

5. C++ Bitwise Operators


In C++, bitwise operators are used to perform operations on
individual bits. They can only be used alongside char and int
data types.
Operator Description
& Binary AND

| Binary OR

^ Binary XOR

~ Binary One's Complement

<< Binary Shift Left

>> Binary Shift Right

Q.6 WHAT ARE MANIPULATORS IN


C++?EXPLAIN BY EXAMPLE
ANS=
Manipulators are helping functions that can modify the
input/output stream. It does not mean that we change the
value of a variable, it only modifies the I/O stream using
insertion (<<) and extraction (>>) operators. ... ends: It is
also defined in ostream and it inserts a null character into
the output stream.

Manipulators are used to changing formatting parameters on


streams and to insert or extract certain special characters.
Following are some of the most widely used C++ manipulators

THERE VARIOUS TYPE OF MAINUPLATOR:

endl Manipulator:
This manipulator has the same functionality as the ‘n’ newline character.

For example:

1. cout << "Exforsys" << endl;

2. cout << "Training";


setw Manipulator:
This manipulator sets the minimum field width on output.

Syntax:

setw(x)

Here setw causes the number or string that follows it to be printed within a field of x characters wide and x is the argument
set in setw manipulator.

The header file that must be included while using setw manipulator is .

setfill Manipulator:
This is used after setw manipulator.

If a value does not entirely fill a field, then the character specified in the setfill argument of the manipulator is used for filling
the fields.

Example:

#include <iostream>

#include <iomanip>

void main()

cout << setw(15) << setfill('*') << 99 << 97 << endl;

Output:

********9997

setprecision Manipulator:
The setprecision Manipulator is used with floating point numbers.

It is used to set the number of digits printed to the right of the decimal point.

This may be used in two forms:

fixed

scientific
These two forms are used when the keywords fixed or scientific are appropriately used before the setprecision manipulator.

The keyword fixed before the setprecision manipulator prints the floating point number in fixed notation.

The keyword scientific, before the setprecision manipulator, prints the floating point number in scientific notation.

Example:

#include <iostream>

#include <iomanip>

void main( )

float x = 0.1;

cout << fixed << setprecision(3) << x << endl;

cout << scientific << x << endl

Output:

0.100

1.000e-001

The first cout statement contains fixed notation and the setprecision contains argument 3.

This means that three digits after the decimal point and in fixed notation will output the first cout statement as 0.100. The
second cout produces the output in scientific notation.

The default value is used since no setprecision value is provided.

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
----------------

You might also like