0% found this document useful (0 votes)
3K views54 pages

BPLCK 205d Module 2 Introduction To C

The document discusses various topics related to functions in C++ including tokens, keywords, identifiers, constants, operators, dynamic memory allocation using new and delete operators, scope resolution operator and different types of expressions in C++.

Uploaded by

Mohammed owais
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3K views54 pages

BPLCK 205d Module 2 Introduction To C

The document discusses various topics related to functions in C++ including tokens, keywords, identifiers, constants, operators, dynamic memory allocation using new and delete operators, scope resolution operator and different types of expressions in C++.

Uploaded by

Mohammed owais
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 54

lOMoARcPSD|28362339

Bplck 205D module-2-Introduction to C++

Introduction to C++ (Visvesvaraya Technological University)

Studocu is not sponsored or endorsed by any college or university


Downloaded by Surya prakash Gowda ([email protected])
lOMoARcPSD|28362339

Module-2 Functions in C++ BPLCK205D

Module-2 FUNCTIONS IN 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

Downloaded by Surya prakash Gowda ([email protected])


lOMoARcPSD|28362339

Module-2 Functions in C++ BPLCK205D

Example :

Identifiers :

Dept.of. AIML 2
GMIT, Davangere

Downloaded by Surya prakash Gowda ([email protected])


lOMoARcPSD|28362339

Module-2 Functions in C++ BPLCK205D

Example

Dept.of. AIML 3
GMIT, Davangere

Downloaded by Surya prakash Gowda ([email protected])


lOMoARcPSD|28362339

Module-2 Functions in C++ BPLCK205D

Constants :

Example

Dept.of. AIML 4
GMIT, Davangere

Downloaded by Surya prakash Gowda ([email protected])


lOMoARcPSD|28362339

Module-2 Functions in C++ BPLCK205D

Dept.of. AIML 5
GMIT, Davangere

Downloaded by Surya prakash Gowda ([email protected])


lOMoARcPSD|28362339

Module-2 Functions in C++ BPLCK205D

Q.2. With an example, describe the operators in C++

Dept.of. AIML 6
GMIT, Davangere

Downloaded by Surya prakash Gowda ([email protected])


lOMoARcPSD|28362339

Module-2 Functions in C++ BPLCK205D

Dept.of. AIML 7
GMIT, Davangere

Downloaded by Surya prakash Gowda ([email protected])


lOMoARcPSD|28362339

Module-2 Functions in C++ BPLCK205D

Dept.of. AIML 8
GMIT, Davangere

Downloaded by Surya prakash Gowda ([email protected])


lOMoARcPSD|28362339

Module-2 Functions in C++ BPLCK205D

Dept.of. AIML 9
GMIT, Davangere

Downloaded by Surya prakash Gowda ([email protected])


lOMoARcPSD|28362339

Module-2 Functions in C++ BPLCK205D

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

Downloaded by Surya prakash Gowda ([email protected])


lOMoARcPSD|28362339

Module-2 Functions in C++ BPLCK205D

Dept.of. AIML 11
GMIT, Davangere

Downloaded by Surya prakash Gowda ([email protected])


lOMoARcPSD|28362339

Module-2 Functions in C++ BPLCK205D

Dept.of. AIML 12
GMIT, Davangere

Downloaded by Surya prakash Gowda ([email protected])


lOMoARcPSD|28362339

Module-2 Functions in C++ BPLCK205D

Dept.of. AIML 13
GMIT, Davangere

Downloaded by Surya prakash Gowda ([email protected])


lOMoARcPSD|28362339

Module-2 Functions in C++ BPLCK205D

Dept.of. AIML 14
GMIT, Davangere

Downloaded by Surya prakash Gowda ([email protected])


lOMoARcPSD|28362339

Module-2 Functions in C++ BPLCK205D

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

Downloaded by Surya prakash Gowda ([email protected])


lOMoARcPSD|28362339

Module-2 Functions in C++ BPLCK205D

● 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

Downloaded by Surya prakash Gowda ([email protected])


lOMoARcPSD|28362339

Module-2 Functions in C++ BPLCK205D

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

Downloaded by Surya prakash Gowda ([email protected])


lOMoARcPSD|28362339

Module-2 Functions in C++ BPLCK205D

Constants are used in the following situations:


● It is used in the subscript declarator to describe the array bound.
● It is used after the case keyword in the switch statement.
● It is used as a numeric value in an enum
● It specifies a bit-field width.
● It is used in the pre-processor #if
In the above scenarios, the constant expression can have integer, character, and enumeration
constants. We can use the static and extern keyword with the constants to define the
function-scope.
The following table shows the expression containing constant value:

Dept.of. AIML 18
GMIT, Davangere

Downloaded by Surya prakash Gowda ([email protected])


lOMoARcPSD|28362339

Module-2 Functions in C++ BPLCK205D

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

Downloaded by Surya prakash Gowda ([email protected])


lOMoARcPSD|28362339

Module-2 Functions in C++ BPLCK205D

Dept.of. AIML 20
GMIT, Davangere

Downloaded by Surya prakash Gowda ([email protected])


lOMoARcPSD|28362339

Module-2 Functions in C++ BPLCK205D

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

Downloaded by Surya prakash Gowda ([email protected])


lOMoARcPSD|28362339

Module-2 Functions in C++ BPLCK205D

Example

Dept.of. AIML 22
GMIT, Davangere

Downloaded by Surya prakash Gowda ([email protected])


lOMoARcPSD|28362339

Module-2 Functions in C++ BPLCK205D

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

Downloaded by Surya prakash Gowda ([email protected])


lOMoARcPSD|28362339

Module-2 Functions in C++ BPLCK205D

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

Downloaded by Surya prakash Gowda ([email protected])


lOMoARcPSD|28362339

Module-2 Functions in C++ BPLCK205D

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

Downloaded by Surya prakash Gowda ([email protected])


lOMoARcPSD|28362339

Module-2 Functions in C++ BPLCK205D

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

Downloaded by Surya prakash Gowda ([email protected])


lOMoARcPSD|28362339

Module-2 Functions in C++ BPLCK205D

Dept.of. AIML 27
GMIT, Davangere

Downloaded by Surya prakash Gowda ([email protected])


lOMoARcPSD|28362339

Module-2 Functions in C++ BPLCK205D

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

Downloaded by Surya prakash Gowda ([email protected])


lOMoARcPSD|28362339

Module-2 Functions in C++ BPLCK205D

● Embedded Assignment Expression


An embedded assignment expression is an assignment expression in which assignment
expression is enclosed within another assignment expression.

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

Downloaded by Surya prakash Gowda ([email protected])


lOMoARcPSD|28362339

Module-2 Functions in C++ BPLCK205D

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

Downloaded by Surya prakash Gowda ([email protected])


lOMoARcPSD|28362339

Module-2 Functions in C++ BPLCK205D

Dept.of. AIML 31
GMIT, Davangere

Downloaded by Surya prakash Gowda ([email protected])


lOMoARcPSD|28362339

Module-2 Functions in C++ BPLCK205D

Dept.of. AIML 32
GMIT, Davangere

Downloaded by Surya prakash Gowda ([email protected])


lOMoARcPSD|28362339

Module-2 Functions in C++ BPLCK205D

Dept.of. AIML 33
GMIT, Davangere

Downloaded by Surya prakash Gowda ([email protected])


lOMoARcPSD|28362339

Module-2 Functions in C++ BPLCK205D

Dept.of. AIML 34
GMIT, Davangere

Downloaded by Surya prakash Gowda ([email protected])


lOMoARcPSD|28362339

Module-2 Functions in C++ BPLCK205D

Q.8. Briefly explain call by reference and return by reference in C++ ?


Call by reference

For now, let us call the function swap() by passing values by reference as in the

following example −

Dept.of. AIML 35
GMIT, Davangere

Downloaded by Surya prakash Gowda ([email protected])


lOMoARcPSD|28362339

Module-2 Functions in C++ BPLCK205D

Dept.of. AIML 36
GMIT, Davangere

Downloaded by Surya prakash Gowda ([email protected])


lOMoARcPSD|28362339

Module-2 Functions in C++ BPLCK205D

Dept.of. AIML 37
GMIT, Davangere

Downloaded by Surya prakash Gowda ([email protected])


lOMoARcPSD|28362339

Module-2 Functions in C++ BPLCK205D

Dept.of. AIML 38
GMIT, Davangere

Downloaded by Surya prakash Gowda ([email protected])


lOMoARcPSD|28362339

Module-2 Functions in C++ BPLCK205D

Q.9. Describe the working of inline functions in C++


● C++ inline function is powerful concept that is commonly used with classes. If a function
is inline, the compiler places a copy of the code of that function at each point where the
function is called at compile time.
● Any change to an inline function could require all clients of the function to be recompiled
because compiler would need to replace all the code once again otherwise it will continue
with old functionality.
● To inline a function, place the keyword inline before the function name and define the
function before any calls are made to the function. The compiler can ignore the inline
qualifier in case defined function is more than a line.
● A function definition in a class definition is an inline function definition, even without
the use of the inline specifier.

Dept.of. AIML 39
GMIT, Davangere

Downloaded by Surya prakash Gowda ([email protected])


lOMoARcPSD|28362339

Module-2 Functions in C++ BPLCK205D

Dept.of. AIML 40
GMIT, Davangere

Downloaded by Surya prakash Gowda ([email protected])


lOMoARcPSD|28362339

Module-2 Functions in C++ BPLCK205D

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

Downloaded by Surya prakash Gowda ([email protected])


lOMoARcPSD|28362339

Module-2 Functions in C++ BPLCK205D

Dept.of. AIML 42
GMIT, Davangere

Downloaded by Surya prakash Gowda ([email protected])


lOMoARcPSD|28362339

Module-2 Functions in C++ BPLCK205D

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

Q.11. Discuss the different types of function overloading

Dept.of. AIML 43
GMIT, Davangere

Downloaded by Surya prakash Gowda ([email protected])


lOMoARcPSD|28362339

Module-2 Functions in C++ BPLCK205D

C++ Function Overloading


● Function Overloading is defined as the process of having two or more function with the
same name, but different in parameters is known as function overloading in C++.
● In function overloading, the function is redefined by using either different types of
arguments or a different number of arguments. It is only through these differences
compiler can differentiate between the functions.
● The advantage of Function overloading is that it increases the readability of the program
because you don't need to use different names for the same action.

Dept.of. AIML 44
GMIT, Davangere

Downloaded by Surya prakash Gowda ([email protected])


lOMoARcPSD|28362339

Module-2 Functions in C++ BPLCK205D

Dept.of. AIML 45
GMIT, Davangere

Downloaded by Surya prakash Gowda ([email protected])


lOMoARcPSD|28362339

Module-2 Functions in C++ BPLCK205D

// Program of function overloading with different types of arguments.

#include<iostream>
using namespace std;
int mul(int,int);
float mul(float,int);

int mul(int a,int b)


{
return a*b;
}
float mul(double x, int y)
{
return x*y;
}
int main()
{
int r1 = mul(6,7);
float r2 = mul(0.2,3);
std::cout << "r1 is : " <<r1<< std::endl;
std::cout <<"r2 is : " <<r2<< std::endl;
return 0;
}

Dept.of. AIML 46
GMIT, Davangere

Downloaded by Surya prakash Gowda ([email protected])


lOMoARcPSD|28362339

Module-2 Functions in C++ BPLCK205D

Dept.of. AIML 47
GMIT, Davangere

Downloaded by Surya prakash Gowda ([email protected])


lOMoARcPSD|28362339

Module-2 Functions in C++ BPLCK205D

Dept.of. AIML 48
GMIT, Davangere

Downloaded by Surya prakash Gowda ([email protected])


lOMoARcPSD|28362339

Module-2 Functions in C++ BPLCK205D

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

Downloaded by Surya prakash Gowda ([email protected])


lOMoARcPSD|28362339

Module-2 Functions in C++ BPLCK205D

● 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 &).

C++ Operators Overloading


● Operator overloading is a compile-time polymorphism in which the operator is
overloaded to provide the special meaning to the user-defined data type.
● Operator overloading is used to overload or redefines most of the operators available in
C++. It is used to perform the operation on the user-defined data type.

Dept.of. AIML 50
GMIT, Davangere

Downloaded by Surya prakash Gowda ([email protected])


lOMoARcPSD|28362339

Module-2 Functions in C++ BPLCK205D

● 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.

Rules for Operator Overloading


● Existing operators can only be overloaded, but the new operators cannot be overloaded.
● The overloaded operator contains atleast one operand of the user-defined data type.
● We cannot use friend function to overload certain operators. However, the member
function can be used to overload those operators.
● When unary operators are overloaded through a member function take no explicit
arguments, but, if they are overloaded by a friend function, takes one argument.
● When binary operators are overloaded through a member function takes one explicit
argument, and if they are overloaded through a friend function takes two explicit
arguments.

Dept.of. AIML 51
GMIT, Davangere

Downloaded by Surya prakash Gowda ([email protected])


lOMoARcPSD|28362339

Module-2 Functions in C++ BPLCK205D

C++ Operators Overloading Example


Let's see the simple example of operator overloading in C++. In this example, void operator ++
() operator function is defined (inside Test class).
// program to overload the unary operator ++.
#include <iostream>
using namespace std;
class Test
{
private:
int num;
public:
Test(): num(8){}
void operator ++() {
num = num+2;
}
void Print() {
cout<<"The Count is: "<<num;
}
};
int main()
{
Test tt;
++tt; // calling of a function "void operator ++()"
tt.Print();
return 0;
}
Output:
The Count is: 10

Dept.of. AIML 52
GMIT, Davangere

Downloaded by Surya prakash Gowda ([email protected])


lOMoARcPSD|28362339

Module-2 Functions in C++ BPLCK205D

// program to overload the binary operators.


#include <iostream>
using namespace std;
class A
{
int x;
public:
A(){}
A(int i)
{
x=i;
}
void operator+(A);
void display();
};
void A :: operator+(A a)
{
int m = x+a.x;
cout<<"The result of the addition of two objects is : "<<m;
}
int main()
{
A a1(5);
A a2(4);
a1+a2;
return 0;
}
Output:
The result of the addition of two objects is : 9

Dept.of. AIML 53
GMIT, Davangere

Downloaded by Surya prakash Gowda ([email protected])

You might also like