BPLCK 205d Module 2 Introduction To C
BPLCK 205d Module 2 Introduction To C
Q.1. What are tokens. Briefly explain the Keywords, Identifiers and Constants.
● A C++ program is composed of tokens which are the smallest individual unit. Tokens can
be one of several things, including keywords, identifiers, constants, operators, or
punctuation marks.
Keywords
● In C++, keywords are reserved words that have a specific meaning in the language and
cannot be used as identifiers. Keywords are an essential part of the C++ language and are
used to perform specific tasks or operations.
Dept.of. AIML 1
GMIT, Davangere
Example :
Identifiers :
Dept.of. AIML 2
GMIT, Davangere
Example
Dept.of. AIML 3
GMIT, Davangere
Constants :
Example
Dept.of. AIML 4
GMIT, Davangere
Dept.of. AIML 5
GMIT, Davangere
Dept.of. AIML 6
GMIT, Davangere
Dept.of. AIML 7
GMIT, Davangere
Dept.of. AIML 8
GMIT, Davangere
Dept.of. AIML 9
GMIT, Davangere
Q.3. How dynamic memory allocation and freeing the memory performed with the help of
new and delete operators. Explain with suitable examples.
Dept.of. AIML 10
GMIT, Davangere
Dept.of. AIML 11
GMIT, Davangere
Dept.of. AIML 12
GMIT, Davangere
Dept.of. AIML 13
GMIT, Davangere
Dept.of. AIML 14
GMIT, Davangere
Q.4. Illustrate the uses and working of Scope resolution operator with an example.
● The scope resolution operator is used to reference the global variable or member function
that is out of scope.
● Therefore, we use the scope resolution operator to access the hidden variable or function
of a program. The operator is represented as the double colon (::) symbol.
● For example, when the global and local variable or function has the same name in a
program, and when we call the variable, by default it only accesses the inner or local
variable without calling the global variable.
Dept.of. AIML 15
GMIT, Davangere
● In this way, it hides the global variable or function. To overcome this situation, we use the
scope resolution operator to fetch a program's hidden variable or function.
Uses of the scope resolution Operator :
● It is used to access the hidden variables or member functions of a program.
● It defines the member function outside of the class using the scope resolution.
● It is used to access the static variable and static function of a class.
● The scope resolution operator is used to override function in the Inheritance.
Q.5. Explain different types of Expressions in C++. Give examples for each type
● C++ expression consists of operators, constants, and variables which are arranged
according to the rules of the language.
● It can also contain function calls which return values. An expression can consist of one or
more operands, zero or more operators to compute a value.
● Every expression produces some value which is assigned to the variable with the help of
an assignment operator.
Dept.of. AIML 16
GMIT, Davangere
Constant expressions
● A constant expression is an expression that consists of only constant values. It is an
expression whose value is determined at the compile-time but evaluated at the run-time.
It can be composed of integer, character, floating-point, and enumeration constants.
Dept.of. AIML 17
GMIT, Davangere
Dept.of. AIML 18
GMIT, Davangere
Integral Expressions
An integer expression is an expression that produces the integer value as output after performing
all the explicit and implicit conversions.
Following are the examples of integral expression:
Dept.of. AIML 19
GMIT, Davangere
Dept.of. AIML 20
GMIT, Davangere
Float Expressions
A float expression is an expression that produces floating-point value as output after performing
all the explicit and implicit conversions.
The following are the examples of float expressions:
Dept.of. AIML 21
GMIT, Davangere
Example
Dept.of. AIML 22
GMIT, Davangere
Pointer Expressions
A pointer expression is an expression that produces address value as an output.
The following are the examples of pointer expression:
Dept.of. AIML 23
GMIT, Davangere
Relational Expressions
A relational expression is an expression that produces a value of type bool, which can be either
true or false. It is also known as a boolean expression. When arithmetic expressions are used on
both sides of the relational operator, arithmetic expressions are evaluated first, and then their
results are compared.
The following are the examples of the relational expression:
In the above code, we have declared two variables, i.e., 'a' and 'b'. After declaration, we have
applied the relational operator between the variables to check whether 'a' is greater than 'b' or not.
Dept.of. AIML 24
GMIT, Davangere
Logical Expressions
A logical expression is an expression that combines two or more relational expressions and
produces a bool type value. The logical operators are '&&' and '||' that combines two or more
relational expressions.
The following are some examples of logical expressions:
Dept.of. AIML 25
GMIT, Davangere
Bitwise Expressions
A bitwise expression is an expression which is used to manipulate the data at a bit level. They
are basically used to shift the bits.
For example:
x=3
x>>3 // This statement means that we are shifting the three-bit position to the right.
In the above example, the value of 'x' is 3 and its binary value is 0011. We are shifting the value
of 'x' by three-bit position to the right. Let's understand through the diagrammatic representation.
Dept.of. AIML 26
GMIT, Davangere
Dept.of. AIML 27
GMIT, Davangere
Q.6. Briefly explain about the special assignment expressions with suitable examples.
Special assignment expressions are the expressions which can be further classified depending
upon the value assigned to the variable.
● Chained Assignment
Chained assignment expression is an expression in which the same value is assigned to more
than one variable by using single statement.
Dept.of. AIML 28
GMIT, Davangere
Compound Assignment
A compound assignment expression is an expression which is a combination of an assignment
operator and binary operator.
Dept.of. AIML 29
GMIT, Davangere
Q.7. Illustrate the Function prototyping and its parts with suitable examples.
A function is a block of code which only runs when it is called. We can pass data, known as
parameters, into a function.
● Functions are used to perform certain actions, and they are important for reusing code:
Define the code once, and use it many times.
Creating a Function
● C++ provides some pre-defined functions, such as main(), which is used to execute code.
But you can also create your own functions to perform certain actions.
● To create (often referred to as declare) a function, specify the name of the function,
followed by parentheses ():
Dept.of. AIML 30
GMIT, Davangere
Dept.of. AIML 31
GMIT, Davangere
Dept.of. AIML 32
GMIT, Davangere
Dept.of. AIML 33
GMIT, Davangere
Dept.of. AIML 34
GMIT, Davangere
For now, let us call the function swap() by passing values by reference as in the
following example −
Dept.of. AIML 35
GMIT, Davangere
Dept.of. AIML 36
GMIT, Davangere
Dept.of. AIML 37
GMIT, Davangere
Dept.of. AIML 38
GMIT, Davangere
Dept.of. AIML 39
GMIT, Davangere
Dept.of. AIML 40
GMIT, Davangere
Q.10. When do we use default arguments ? State the rules that need to be followed while
using default arguments ?
A default argument is a value in the function declaration automatically assigned by the compiler
if the calling function does not pass any value to that argument.
Dept.of. AIML 41
GMIT, Davangere
Dept.of. AIML 42
GMIT, Davangere
Output
prog.cpp: In function 'int main()':
prog.cpp:15:20: error: call of overloaded 'sum(int, int)' is ambiguous
cout << sum(10, 15) << endl; // x = 10, y = 15, z = 0, w = 0
^
prog.cpp:4:5: note: candidate: int sum(int, int, int, int)
int sum(int x, int y, int z=0, int w=0) // Here there are two values in the default arguments
^
prog.cpp:9:5: note: candidate: int sum(int, int, float, float)
int sum(int x, int y, float z=0, float w=0) // Here sum is overloaded with two float parameter
values
Dept.of. AIML 43
GMIT, Davangere
Dept.of. AIML 44
GMIT, Davangere
Dept.of. AIML 45
GMIT, Davangere
#include<iostream>
using namespace std;
int mul(int,int);
float mul(float,int);
Dept.of. AIML 46
GMIT, Davangere
Dept.of. AIML 47
GMIT, Davangere
Dept.of. AIML 48
GMIT, Davangere
The above example shows an error "call of overloaded 'fun(int)' is ambiguous". The fun(int a, int
b=9) can be called in two ways: first is by calling the function with one argument, i.e., fun(12)
and another way is calling the function with two arguments, i.e., fun(4,5). The fun(int i) function
is invoked with one argument. Therefore, the compiler could not be able to select among fun(int
i) and fun(int a,int b=9).
● Function with pass by reference
Dept.of. AIML 49
GMIT, Davangere
● The above example shows an error "call of overloaded 'fun(int&)' is ambiguous". The
first function takes one integer argument and the second function takes a reference
parameter as an argument.
● In this case, the compiler does not know which function is needed by the user as there is
no syntactical difference between the fun(int) and fun(int &).
Dept.of. AIML 50
GMIT, Davangere
● For example, C++ provides the ability to add the variables of the user-defined data type
that is applied to the built-in data types.
Dept.of. AIML 51
GMIT, Davangere
Dept.of. AIML 52
GMIT, Davangere
Dept.of. AIML 53
GMIT, Davangere