0% found this document useful (0 votes)
1 views48 pages

OOP (CO1201) Unit-3 - C++ Functions

This document covers key concepts in C++ functions, including call by value and call by reference, access specifiers (public, private, protected), static variables, macros, inline functions, and friend functions. It provides code examples to illustrate each concept and explains the scope resolution operator's usage. Overall, it serves as a comprehensive guide to understanding fundamental C++ programming principles.

Uploaded by

harsh.parmar
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)
1 views48 pages

OOP (CO1201) Unit-3 - C++ Functions

This document covers key concepts in C++ functions, including call by value and call by reference, access specifiers (public, private, protected), static variables, macros, inline functions, and friend functions. It provides code examples to illustrate each concept and explains the scope resolution operator's usage. Overall, it serves as a comprehensive guide to understanding fundamental C++ programming principles.

Uploaded by

harsh.parmar
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/ 48

Unit-3 : C++ Functions

By,
Ms.Anuja Gunale
Call by Value
 When arguments are passed by value, a copy of the actual parameter is passed to the function.
 Changes made to the parameter inside the function do not affect the original value.

#include <iostream>
using namespace std;
void modifyValue(int a)
{
a = 10; // This changes only the local copy of 'a'
cout << "Inside function (call by value): " << a << endl;
}
int main()
{
int x = 5;
cout << "Before function call: " << x << endl;
OUTPUT:
modifyValue(x); Before function call: 5
cout << "After function call: " << x << endl; // 'x' remains unchanged Inside function (call by value): 10
return 0; After function call: 5
}
Call by Reference
 When arguments are passed by reference, the function receives a reference to the actual parameter.
 Changes made to the parameter inside the function do affect the original value.

#include <iostream>
using namespace std;

void modifyValue(int &a)


{
a = 10; // This changes the original 'a'
cout << "Inside function (call by reference): " << a << endl;
}

int main()
{
int x = 5;
cout << "Before function call: " << x << endl;
modifyValue(x); OUTPU:
cout << "After function call: " << x << endl; // 'x' is changed Before function call: 5
return 0; Inside function (call by reference): 10
} After function call: 10
Access Specifiers in C++
 In C++, access specifiers define the access level of members (variables and functions) of a class.
 The three main access specifiers are public, private, and protected.
 Here are simple examples to illustrate their usage:

•Public: Members are accessible from anywhere.


•Private: Members are accessible only within the class.
•Protected: Members are accessible within the class and by derived classes.
 Let us now look at each one of these access modifiers in detail:
1. Public: All the class members declared under the public specifier
will be available to everyone. The data members and member
functions declared as public can be accessed by other classes and
functions too. The public members of a class can be accessed from
anywhere in the program using the direct member access operator (.)
with the object of that class.
 2. Private: The class members declared as private can be accessed
only by the member functions inside the class. They are not allowed
to be accessed directly by any object or function outside the class.
Only the member functions or the friend functions are allowed to
access the private data members of the class.
 3. Protected: The protected access modifier is similar to the private
access modifier in the sense that it can’t be accessed outside of its
class unless with the help of a friend class. The difference is that the
class members declared as Protected can be accessed by any subclass
(derived class) of that class as well.
Public
 Members declared as public are accessible from outside the class.

#include <iostream>
using namespace std;
class PublicExample
{
public: int x;
void display()
{
cout << "Value of x: " << x << endl;
}
};
int main()
{
PublicExample obj;
obj.x = 10; // Accessible because x is public
obj.display();
return 0;
}
Private
 Members declared as private are accessible only within the class. They cannot be accessed or modified from outside the class.

#include <iostream>
using namespace std;
class PrivateExample
{
private:
int x;
public:
void setX(int val)
{
x = val;
}
void display()
{
cout << "Value of x: " << x << endl;
}
};
int main()
{
PrivateExample obj; //
obj.x = 10; // Error: 'x' is private
obj.setX(10); // Correct way to set 'x' value
obj.display();
return 0;
}
Function
A function is a block of code which only
runs when it is called.

 Youcan 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.
#include <iostream.h>
void myFunction()
{
cout << "I just got executed!";
}

int main()
{
myFunction();
return 0;
}
#include <iostream.h>
int sum(int a, int b = 20)
{
int result;
result = a + b;
return (result);
}

int main ()
{
int a = 100, b = 200, result;
result = sum(a, b);
cout << "Total value is :" << result << endl;
return 0;
}
Static keyword in c++
 A static variable is a variable that is declared using the
keyword static.

 The space for the static variable is allocated only one


time and this is used for the entirety of the program.

 Once this variable is declared, it exists till the program


executes. So, the lifetime of a static variable is the
lifetime of the program.

 A program that demonstrates a static variable is given as


follows.
 Static is a keyword in C++ used to give special
characteristics to an element.

 Static elements are allocated storage only once in a


program lifetime in static storage area.

 And they have a scope till the program lifetime. Static


Keyword can be used with following,
 Static variable in functions
 Static Class Objects
 Static member Variable in class
 Static Methods in class
#include <iostream>
using namespace std;
int add(int myNumber)
{
static int total = 0;
total += myNumber;
return total;
}
int main()
{
cout << add(5) << "\n";
cout << add(2) << "\n";
cout << add(4) << "\n"; OUTPUT:
5
cout << add(9) << "\n"; 7
return 0; 11
20
}
Macro
 Macros are nothing but a piece of code in C++ programming language represented by

some given names.

 Therefore, whenever you are running your source code and the same name is found

by the code compiler then the compiler will replace the name by the original piece

of code.

 A macro is a piece of code in a program that is replaced by the value of the macro.

Macro is defined by #define directive.

 Whenever a macro name is encountered by the compiler, it replaces the name with

the definition of the macro.

 Macro definitions need not be terminated by a semi-colon(;).

 Below are the programs to illustrate the use of macros in C/C++:


#include <iostream>
#define PI 3.1416
#define AREA(r) (PI*(r)*(r))

int main()
{

float r = 7; // radius of circle


cout<<"Area of Circle with radius " << r <<": "<< AREA(r);
return 0;
}
#include <iostream.h>
# define Square(a) a*a
int main()
{
int x= 15;
cout<<"square of x is "<<Square(x);
cout<<"square of x++ is "<<Square(x++);
return 0;
}
square of x is 225square of x++ is 240
#include <iostream>
using namespace std;
//creating inline function square
inline int square(int x)
{
int sq=x*x;
return sq;
}
int main()
{
int x =15;
cout<<"square of x is "<<square(x);
return 0;
}
Friend Function
 What is a friend function?
 A friend function is a function that is
specified outside a class but has the ability
to access the class members’ protected and
private data.
 A friend can be a member’s function,
function template, or function, or a class or
class template, in which case the entire
class and all of its members are friends.
 The function is not in the scope of the class to
which it has been declared as a friend.
 It cannot be called using the object as it is not
in the scope of that class.
 It can be invoked like a normal function
without using the object.
 It cannot access the member names directly
and has to use an object name and dot
membership operator with the member name.
 It can be declared either in the private or the
public part.
Syntax
class <class_name>
{
friend <return_type> <function_name>(argument/s);
};
#include <iostream>
using namespace std;

class Box {
private:
double width;

public:
// Constructor
Box(double w) : width(w) {}

// Friend function declaration


friend void printWidth(Box box);
};

// Friend function definition


void printWidth(Box box) {
// Since printWidth is a friend of Box, it can directly access private members of Box
cout << "Width of box: " << box.width << endl;
}

int main() {
Box box(10.5);
printWidth(box); // Call the friend function

return 0;
}
Explanation:

1.Class Definition:
•Box class has a private member width.
•A constructor Box(double w) initializes the width.

2.Friend Function:
•friend void printWidth(Box box); is declared inside the Box class.
•This allows printWidth to access private members of the Box class.

3.Function Definition:
•The function printWidth(Box box) is defined outside the class and it accesses the
private member width.

4.Main Function:
•A Box object box is created with a width of 10.5.
•The printWidth function is called with box as an argument, and it prints the width
of the box.
#include <iostream>
using namespace std;
class Cal {
public:
static int add(int a,int b){
return a + b;
}
static int add(int a, int b, int c)
{
return a + b + c;
}
};
int main(void) {
Cal C; // class object declaration.
cout<<C.add(10, 20)<<endl;
Output:
cout<<C.add(12, 20, 23);
30 55
return 0;
}
Scope Resolution Operator in C++
 This section will discuss the scope resolution operator and its various
uses in the C++ programming language.
 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.
 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.
 #include <iostream>
 using namespace std;
 // declare global variable
 int num = 50;
 int main ()
 {
 // declare local variable
 int num = 100;

 // print the value of the variables


 cout << " The value of the local variable num: " << num;

 // use scope resolution operator (::) to access the global variable


Output:
 cout << "\n The value of the global variable num: " << ::num;
100
 return 0; 50
 }

You might also like