0% found this document useful (0 votes)
17 views7 pages

C++ Operators and Functions Explained

The document provides an overview of various C++ concepts including insertion and extraction operators, scope resolution operator, function overloading, inline functions, pointers, and friend functions. It explains how these features work with examples, highlighting their usage and characteristics. Each concept is described succinctly, making it a useful reference for understanding fundamental C++ programming elements.
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)
17 views7 pages

C++ Operators and Functions Explained

The document provides an overview of various C++ concepts including insertion and extraction operators, scope resolution operator, function overloading, inline functions, pointers, and friend functions. It explains how these features work with examples, highlighting their usage and characteristics. Each concept is described succinctly, making it a useful reference for understanding fundamental C++ programming elements.
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

C++

Q1.) Write short note on insertion operator and extraction operator.


Ans:
<< is insertion or put to operator.
It inserts (or sends) the contents of the variable on its right to also the
object on its left.
This operator is used along with cout stream.
For example,
cout << string;
This statement will display the contents of string. Here cout is predefined
object that represents standard output stream in C++.
>> is extraction operator or get from operator.
It extracts (or takes) the value from the standard input device (keyboard)
and assign it to the variable on its right. This operator is used with cin
object.
For example,
cin >> number1;
This statement causes the programmer to wait for the user to type in a
number. The number keyed in is placed in the variable number1. The cin
is predefined object in C++ that corresponds to standard input stream.
Here, it represents keyboard.

Q2.) Explain Scope resolution operator with an example.


Ans:
A C++ program may contain a block within a block.

block 1 block 2
In C++, a variable is declared in an inner block (block 2) then it hides
declaration of same variable in an outer block (block 1). Therefore, each
declaration will cause refer to a different data object.

:: scope resolution operator can be used to uncover the hidden variable. It


has the form

:: variable-name

The scope resolution operator allows access to a global version of variable.

#include<iostream.h>

int a=10; //global a

void main( )

int a=15; // a redeclared, local to main

cout<<“\nlocal a=”<<a<<“global a=”<<::a;

::a=20; // assigning value to global a

cout<<“\nlocal a=”<<a<<“global a=”<<::a;

:: scope resolution operator allows to access global variable a.

Q3.) What is function overloading? Give suitable example for function


overloading.

Ans:
Function overloading means the use of same function name to create
functions that perform a variety of different tasks. This is known as
function polymorphism in object oriented programming.
The function would perform different operations depending on the
argument list in the function call.
The correct function to be invoked is determined by checking the number
and type of the arguments but not on the function type.
The type and/ or number of parameters of each overloaded function must
differ.
You should overload only similar operations.
#include <iostream.h>
//abs is overloaded three ways
int abs (int i);
long abs (long l) ;
double abs (double d) ;
void main()
{
cout << abs (-10) << "\n";
cout << abs (-11.0) << "\n"
cout << abs (-9L) << "\n“;
}
int abs (int i)
{
cout << "Using integer abs () \n“;
return (i <0 ? –i : i) ;
}
long abs (long l)
{
cout << "Using long abs() \n";
return (l < 0 ? -1: 1);
}
double abs (double d)
{
cout << "Using double abs () \n" ;
return (d <0 ? -d: d) ;
}
Q4.) What are Inline functions? What are its advantages?
Ans:
An inline function is a function that is expanded in line when it is invoked
i.e. compiler replaces the function call with corresponding function code.
Thus, when a function is declared as inline, it tells the compiler to
replace each call to the function with explicit code for the function.
The inline function has the form
inline function – header
{
function body
}
For example,
inline double cube (double);
#include <iostream.h>
void main()
{
cout<<“Square of the given number is: ”<<square(25);
}
inline double cube (double a)
{
return (a *a * a) ;
}

Q5.) What is Pointer?


A pointer is a variable that holds a memory address of another object
(typically variable) in memory.

The declaration of pointer variable has the form


Data type *pointer_name;
For example,
int *p;
There are two special pointer operators:
* and &
The & is unary operator that returns the memory address of its operand.
For example,
m = &count;
The second pointer operator is *. This is also a unary operator called as
indirection operator.

It returns the value located at the address that follows.


q = *m;

int *p, m;
p = &m;

Memory Address Variable in memory

m
Q6.) Explain friend function in C++ with example. (2020)
Q7.) State the characteristics of friend function. (2016)

i) The private data members of a class cannot be accessed by a non-


member function.
ii) C++ allows the common function to be made friendly with more
than one classes, thereby allowing the function to have access to
the private data of these classes. Such a function need not be a
member of any of these classes.
iii) To make an outside function friendly to a class, simply declare the
function as a friend of the class as shown below:
class ABC
{
private:
--------------------
--------------------
public:
-------------------
friend void XYZ (void); //declaration
};
Here function XYZ is friend of class ABC.
iv) The function declaration should be preceded by the keyword
friend. The function is defined else where in the program like a
normal C++ function. The function definition will not use the
keyword friend or scope resolution operator :: .
v) The function can be declared as a friend in any number of
classes.

Example:

class sample
{
private:
int a,b;

public:
void setvalue()
{
a=25;
b=40;
}
friend float mean (sample s); // friend function declaration
};
float mean sample (sample s) // function definition
{
return float (s.a + s.b) / 2.0;
}
void main()
{
sample x;
[Link]();
cout<<”Mean value ”<<mean(x);
}
A function that is not a member of that class and has access to the private
data of the class is called friend function.
A friend function has following characteristics:
i) It is not in the scope of the class to which it has been declared as
friend.
ii) Since it is not in scope of the class, it cannot be called by using object
of that class. It is called like a normal C++ function.
iii) It can be declared either in public or private part of a class.
iv) Usually, it has objects as arguments.

You might also like