OOP (CO1201) Unit-3 - C++ Functions
OOP (CO1201) 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;
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:
#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.
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.
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.
Whenever a macro name is encountered by the compiler, it replaces the name with
int main()
{
class Box {
private:
double width;
public:
// Constructor
Box(double w) : width(w) {}
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;