20cs204 Class 3 Pp-II
20cs204 Class 3 Pp-II
20cs204 Class 3 Pp-II
20CS204
Programming Paradigm-II
CLASS 3
Date:11.08.2021
Why we need functions in C
Functions are used because of following reasons –
To improve the readability of code.
Improves the reusability of the code, same function can be
used in any program rather than writing the same code
from scratch.
Debugging of the code would be easier if you use
functions, as errors are easy to be traced.
Reduces the size of the code, duplicate set of statements
are replaced by function calls.
Declaring, Defining and Calling a Function
Parameter Passing to functions
Calling a Function
Functions are called by their names. If the function is without
argument, it can be called directly using its name. But for
functions with arguments, we have two ways to call them,
Call by Value
In this calling technique we pass the values of arguments which
are stored or copied into the formal parameters of functions.
Hence, the original values are unchanged only the parameters
inside function changes.
Call by reference in C++
In call by reference, original value is modified because we
pass reference (address).
Here, address of the value is passed in the function, so actual
and formal arguments share the same address space. Hence,
value changed inside the function, is reflected inside as well
as outside the function.
Call by Value Diff Call by Reference
Inline Functions in C++
Drawbacks in function:
To save some memory space which becomes when a function
is likely called many times.
Every time a function is called ,it takes a lot of extra time in
executing a series of instructions for tasks such as jumping to
the function, saving registers, pushing arguments into stack
and returning to the calling function.
Diff Normal and Inline Function
Syntax
Output
10
Example 2
};
#include<iostream>
float calculate(AreaOfCircle ob)
class AreaOfCircle
{
{
return 3.14 * ob.radius * ob.radius;
int radius;
}
public:
int main()
void get()
{
{
AreaOfCircle object;
std::cout << "Enter the radius of Circle : ";
object.get();
std::cin >> radius;
std::cout<<"\nArea of Circle :
}
"<<calculate(object);
friend float calculate(AreaOfCircle ob);
}
% -= || %= ^= >>= ()
/= ~ ! ->* [] — ^
Syntax
return_type class_name : : operator op(argument_list)
{
// function body
}
return type- type of value returned by the specified operation
op-operator being overloaded
operator is the keyword
Three approaches of Operator
Overloading
Overloading unary operator.
Overloading binary operator.
Overloading binary operator using a friend function.
Rules to define the operator function
In case of a non-static function, the binary operator should have
only one argument and unary should not have an argument.
In the case of a friend function, the binary operator should have
only two argument and unary should have only one argument.
All the class member object should be public if operator
overloading is implemented.
Operators that cannot be overloaded are . .* :: ?:
Operator cannot be used to overload when declaring that
function as friend function = () [] ->
Overloading Unary Operator
In unary operator function, no arguments should be passed.
It works only with one class objects. It is a overloading of
an operator operating on a single operand.
#include<iostream> void operator--() //Overload Unary Decrement
using namespace std; {
class IncreDecre a--;
{ b--;
int a, b; }
public: void operator++() //Overload Unary Increment
void accept() {
{ a++;
cout<<"\n Enter Two Numbers : \n"; b++;
cout<<" "; }
cin>>a;
cout<<" ";
cin>>b;
}
Example
void display() cout<<"\n\n After Incrementing : ";
{ id.display();
cout<<"\n A : "<<a; return 0;
cout<<"\n B : "<<b; }
}
};
int main()
{
IncreDecre id;
id.accept();
--id;
cout<<"\n After Decrementing : ";
id.display();
++id;
++id;
Fill in the Blanks
class Student int main()
{ {
Student A;
int rollno; ____________________
A.____________(1);
public: cout<< A.getRollno();
int _________________() }
{
return rollno;
}
void setRollno(int i)
{
rollno=i;
}
};
Fill in the Blanks
#include <iostream.h> //Main Function
#include<conio.h>
int main() {
// Object Creation For Class
// Class Declaration __________ obj;
void print() {
public://Access - Specifier //Print the Output using cout
//Member Functions read(),sum() and cout << "Result :" << num1 << " + " << num1 << "
print() Declaration = " << result << endl;
}
};
Main function and Output
int main() {
Simple Class Addition: Add Two Integers In C++
// Object Creation For Class
AdditionClass obj1, obj2; AdditionClass : obj1 Usage
Enter Number 1 :100
cout << "Simple Class Addition : Add Two Integers In C++\n";
Enter Number 2 :200
cout << "\nAdditionClass : obj1 Usage" << endl; Result :100 + 100 = 300
obj1.read();
AdditionClass : obj2 Usage
obj1.sum();
Enter Number 1 :700
obj1.print(); Enter Number 2 :900
cout << "\nAdditionClass : obj2 Usage" << endl; Result :700 + 700 = 1600
obj2.read();
obj2.sum();
obj2.print();
getch();
return 0;
}
Scenario 1
There are 3 sections : A, B, and C
The functions that we need to perform in each class are taking
the attendance, distributing newspapers to students who
subscribed for it, and giving them their final grades
attendance()
newspapers()
grades()