C++ FUNTIONS
CHAPTER 3 (C++ FUNTIONS)
IMPORTANT QUETIONS FOR EXAM (GTU,CVM,SP)
CHAPTER THREE COMPLETE
HERE IS YOUR CVMU SYLLABUS -Link
BLOG BY JAY SHAH(GMAIL)
Explain Call by Value and Call by Reference with appropriate examples.
OR
Write a program to interchange (swap) the value of two variables.
Call By Value
- In call by value, the value of the variable is passed during the function call.
- And copy this value in another variable at function definition.
- In call by value the original value in the calling function will never change after execution of the function.
Example:
#include<iostream>
using namespace std;
void swp(int a, int b)
{
int temp=a;
a=b;
b=temp;
}
int main()
{
int x,y;
cout<<"enter value of a and b:";
cin>>x>>y;
swp(x,y);
cout<<”\nafter swapping a = ”<<a<<”and b = ”<<b;
return 0;
}
Output:
Enter the value of a and b:
4
5
After swapping a = 5 and b = 4
Call By Reference
In incall by reference, reference is passed during the function call.
The formal arguments in the called function become aliases to the actual function call.
Incall by reference, the original value in the calling function will change after the execution of function.
Example:
#include<iostream>
using namespace std;
void swap(int &a, int &b) // It is the only difference from above program
{
int temp=a;
a=b;
b=temp;
}
int main()
{
int a,b;
cout<<"Enter two numbers:";
cin>>a>>b;
swap(a,b);
cout<<"a="<<a<<"b="<<b;
return 0;
}
Output:
Enter the value of a and b:
4
5
After swapping a = 5 and b = 4
What is an inline function? Explain with an example.
The functions can be made inline by adding prefix inline to the function definition.
An inline function is a function that is expanded in line when it is invoked.
The compiler replaces the function call with the corresponding function code.
The inline function saves time on the calling function, saving registers, pushing arguments onto the stack, and
returning from the function.
Preprocessor macros are popular in C, which has similar kinds of advantages mentioned above.
The major drawback of macros is that they are not really function.
Therefore, the usual error checking does not occur during the execution of macros.
We should be careful while using the inline function. If a function has very few lines of code and simple
expressions then only it should be used.
Critical situations for inline function:
1) If a loop, a switch, or a goto exists in the function body.
2) If the function is not returning any value.
3) If the function contains static variables.
4) If the function is recursive.
Example:
inline int cube(int n)
{
return n*n*n;
}
int main()
{
int c;
c = cube(10);
cout<<c;
}
Function call is replaced with expression so c = cube(10); becomes c=10*10*10; at compile time.
Explain function overloading with an example.
Function overloading is compiled time polymorphism.
Function overloading is the practice of declaring the same function with different signatures.
The same function name will be used with a different number of parameters and parameters of a different
type.
Overloading of functions with different return types is not allowed.
The compiler identifies which function should be called out of many using the type and number of arguments.
A function is overloaded when the same name is given to different functions.
However, the two functions with the same name must differ in at least one of the following,
a) The number of parameters
b) The data type of parameters
c) The order of appearance
Example:
#include <iostream>
using namespace std;
class Math
{
public:
void Add(int num1, int num2) //Function 1
{
cout<<num1 + num2 <<endl;
}
void Add(float num1, float num2) //Function 2
{
cout<<num1 + num2 <<endl;
}
What is the friend function? Explain with an example.
A friend function is a function that is declared using the friend keyword.
It is not a member of the class but it has access to the private and protected members of the class.
It is not in the scope of the class to which it has been declared a friend.
It cannot access the member names directly.
It can be declared either in the public or private part of the class.
It is not a member of the class so it cannot be called using the object.
Usually, it has the objects as arguments.
It is a normal external function that is given special access privileges.
Syntax:
class ABC
{
public:
...................................................
friend void xyz(void); // declaration
...................................................
};
Example:
#include<iostream>
using namespace std;
class numbers
{
int num1, num2;
public:
void setdata(int a, int b);
friend int add(numbers N);
};
void numbers :: setdata(int a, int b)
{
num1=a;
num2=b;
}
int add(numbers N)
{
return (N.num1+N.num2);
}
int main()
{
numbers N1;
N1.setdata(10,20);
cout<<”Sum = ”<<add(N1);
}
Output: Sum = 30
add is a friend function of the class numbers so it can access all members of the class(private, public and
protected).
Member functions of one class can be made friend function of another class, like...
class X
{
.............................................
int f();
};
class Y
{
.............................................
friend int X :: f();
};
The function f is a member of class X and a friend of class Y.
We can declare all the member functions of one class as the friend functions of another class. In such
cases, the class is called a friend class, like class X is the friend class of class Z
class Z
{
.............................................
friend class X;
..............................................
};
Explain operator overloading with an example.
Operator overloading is compiled time polymorphism.
The operator overloading provides a mechanism to perform operations on user-defined data types.
We can give special meaning to any operators in which program it is implemented.
Rules for operator overloading
1. Only the existing operator can be overloaded.
2. The overloaded operator must have at least one operand that is a user-defined type.
3. We cannot change the basic meaning and syntax of an operator.
4. We cannot use the friend function to overload certain operators. However, the member functions can be used
to overload them.
5. Unary operators, overloaded using a member function, take no explicit arguments and return
no explicit value, but, those overloaded using a friend function, take one reference argument.
6. Binary operators overloaded through a member function take one explicit argument and those which
are overloaded through a friend function take two explicit arguments.
7. When using binary operators overloaded through a member function, the left-hand operand must be
an object of the relevant class.
8. We cannot overload the following operators.
Operator Name
. and .* Class member access operator
:: Scope Resolution Operator
sizeof() Size Operator
?: Conditional Operator
Example for Unary Operator Overloading
#include <iostream>
using namespace std;
class sample
{
int a,b;
public:
void getdata()
{
a=10;
b=20’
}
void operator -() //Unary Member Function
{
a = a - 5;
b = b - 5;
}
void disp()
{
cout<<"\nThe value of a="<<a;
cout<<"\nThe value of b="<<b;
}
};
int main()
{
sample S;
S.getdata();
-S; //Call Unary Member Function
S.disp();
getch();
return 0;
Example for Unary Operator Overloading using Friend function.
#include <iostream>
using namespace std;
class sample
{
int a,b;
public:
void getdata()
{
a=10;
b=20;
}
friend sample operator +(sample A) //Unary Friend Function
{
A.a = A.a + 5;
A.b = A.b + 5;
return A;
}
void disp()
{
cout<<"\nThe value of a="<<a;
cout<<"\nThe value of b="<<b;
}
};
int main()
{
sample S;
S.getdata();
S=+S; //Call Unary Friend Function
S.disp();
getch();
return 0;
}
Example for Binary Operator Overloading
#include <iostream>
using namespace std;
class complex
{
float real, imag;
public:
complex(float _real, float _imag) // constructor
{
real = _real;
imag = _imag;
}
void disp()
{
cout<<"The value of real="<<real;
cout<<"The value of imag="<<imag;
}
void operator +(complex c) //Binary Member function
{
real = real + c.real;
imag = imag + c.imag;
}
};
int main()
{
complex x(4,4);
complex y(6,6);
x + y; // Call Binary Member Function
x.disp();
getch();
return 0;
}
Example of Binary operator overloading using friend function
#include <iostream>
using namespace std;
class complex
{
float real, imag;
public:
complex(float _real, float _imag) // constructor
{
real = _real;
imag = _imag;
}
void disp()
{
cout<<"The value of real="<<real;
cout<<"The value of imag="<<imag;
}
friend complex operator +(complex c, complex d) //Binary Friend function
{
d.real = d.real + c.real;
d.imag = d.imag + c.imag;
return d;
}
};
int main()
{
complex x(4,4);
complex y(6,6);
complex z = x + y; // Call Binary Friend Function
z.disp();
}
Comments
Post a Comment