Вы находитесь на странице: 1из 21

OBJECT ORIENTED PROGRAMMING (OOP) WITH C++

CLASS AND OBJECTS


AIUB. FALL 2017

Dr. Mahbubul Syeed


Associate Professor, Department. of CS, AIUB
mahbubul.syeed@aiub.edu
www.msyeed.weebly.com
CONTENTS

 Declaring and defining functions.


 Setter, getter functions in a Class.
 Passing arguments to a function
 call-by-value
 call-by-reference
 Default arguments /parameter for functions
 Function overloading (polymorphism)
FUNCTION

A function is a self contained body of code to perform a specific task. For instance, a function to sort a list
of integers.

Features:
- Function helps organizing code based on specific purpose. Increase modularity of the code to a
certain extent.
- Help implementing code that has recurring utilization in the program.
- We can pass arguments to a functions. Different type and number of arguments can be passed to a
function.
- A function can also return a value upon completion of this execution.
FUNCTION
return_type is the type of value returned by the function. (Optional)
Function_name is the identifier by which the function can be called.
Any valid name acceptable to C++ is allowed.

return_type function_name ( parameter1, parameter2, ...)


{
statements ….
return return_type_data;
}
parameters (as many as needed):
Each parameter consists of a type followed by an identifier.
Each parameter being separated from the next by a comma.

statements is the function's body. It is a block of statements


surrounded by braces { } that specify what the function actually does.
FUNCTION – BUBBLE SORT FUNCTION

void bubbleSort(int arr[], int n)


{
void swap(int *xp, int *yp)
int i, j;
{
for (i = 0; i < n-1; i++) int temp = *xp;
for (j = 0; j < n-i-1; j++) *xp = *yp;
*yp = temp;
if (arr[j] > arr[j+1]) }
swap(&arr[j], &arr[j+1]);
}
FUNCTION

#include <iostream>
using namespace std;

int addition (int a, int b) {


int r;
r=a+b;
return r;
}

int main () {
int z;
z = addition (5,3);
cout << "The result is " << z;
return 0;
}
PASSING ARGUMENTS TO A FUNCTION

Two ways:

(1) Call by value.


Only a copy of the actual variable are passed to the calling function. Therefore any change to the arguments
in the calling function has no effect on the actual variables.

(1) Call by reference.


A reference of the actual variable are passed to the calling function. Therefore any change to the arguments
in the calling function also changes the value in the actual variables.
#include <iostream> Passing arguments – Call by value
using namespace std;

void change(int x, int y){


x = 150;
y = 250;
cout << "In Change function after changing the values." << endl << "x: " << x << " y: " << y << endl;
}

int main(){
int x = 20, y = 30;
cout << "In main function before calling change function." << endl << "x: " << x << " y: " << y << endl;
//calling change function and passing x and y as arguments
change(x,y);
cout << "In main function after returning from change function." << endl << "x: " << x << " y: " << y << endl;
}
In main function before calling change function.
x: 20 y: 30
In Change function after changing the values.
x: 150 y: 250
In main function after returning from change function.
x: 20 y: 30
#include <iostream> Passing arguments – Call by value
using namespace std; What is happening?????

void change(int x, int y){


x = 150;
Memory mapping
y = 250;
cout << "In Change function after changing the values." << endl << "x: " << x << x = 20 x = 20
" y: " << y << endl;
} y = 30
y = 30

int main(){ x = 150


int x = 20, y = 30; y = 250
cout << "In main function before calling change function." << endl << "x: " << x
<< " y: " << y << endl;
//calling change function and passing x and y as arguments
change(x,y);
cout << "In main function after returning from change function." << endl << "x:
" << x << " y: " << y << endl;
}
#include <iostream> Passing arguments – Call by reference
using namespace std;

void change(int &x, int &y){


x = 150;
y = 250;
cout << "In Change function after changing the values." << endl << "x: " << x << " y: " << y << endl;
}

int main(){
int x = 20, y = 30;
cout << "In main function before calling change function." << endl << "x: " << x << " y: " << y << endl;
//calling change function and passing x and y as arguments
change(x,y);
cout << "In main function after returning from change function." << endl << "x: " << x << " y: " << y << endl;
}
In main function before calling change function.
x: 20 y: 30
In Change function after changing the values.
x: 150 y: 250
In main function after returning from change function.
x: 150 y: 250
#include <iostream> Passing arguments – Call by reference
using namespace std; What is happening?????

Memory mapping
void change(int &x, int &y){
x = 150;
y = 250; Memory Address
cout << "In Change function after changing the values." << endl << "x: " << x << x = 150
20 0ABC
" y: " << y << endl;
} y = 250
30 0ABD

int main(){
int x = 20, y = 30;
cout << "In main function before calling change function." << endl << "x: " << x
<< " y: " << y << endl;
//calling change function and passing x and y as arguments
change(x,y);
cout << "In main function after returning from change function." << endl << "x:
" << x << " y: " << y << endl;
}
PASSING ARGUMENTS Constant arguments

// passing parameters by reference


#include <iostream>
using namespace std;

string concatenate (const string& a, const string& b)


{
return a+" "+b;
}

int main ()
{
string firstName = "Shakib";
string lastName = "Khan";
cout << "After merging the names: " <<concatenate(firstName, lastName) << endl;

return 0;
}
PASSING ARGUMENTS Constant arguments (why to use!)

#include <iostream>
using namespace std;

string concatenate (string& a, string& b)


{
a = a + "a";
b = b + "am";
return a+" "+b;
}

int main ()
{
string firstName = "Shakib";
string lastName = "Khan";
cout << "After merging the names: " <<concatenate(firstName, lastName) << endl;

return 0;
}
DEFAULT ARGUMENTS

 C++ allows functions to have default values for certain parameters.

 If no values are passed for those parameters the default values are used.

 Only the trailing arguments can have default values.

 No intermediate gap between the default values are allowed.


DEFAULT ARGUMENTS
#include <iostream>
using namespace std;

int divide (int a, int b=2){


What would be the output?
int r;
r=a/b; 6
return (r); 3
}

int main (){


cout << divide (12) << '\n';
cout << divide (12,4) << '\n';
return 0;
}
DEFAULT ARGUMENTS
#include <iostream>
using namespace std;
Is it allowed?
int divide (int x, int a=3, int b=2)
{ int divide (int x =3, int a, int b=2)
int r;
r=a/b;
return (r);
}

int main ()
{
cout << divide (12) << '\n';
cout << divide (12,4) << '\n';
return 0;
}
FUNCTION OVERLOADING- POLYMORPHISM

More than one Function to have the same name with at least one of the following
distinctions:

1. Different number of arguments.

2. Different type of arguments.

But not by return type of the function.


FUNCTION OVERLOADING- POLYMORPHISM
#include <iostream>
using namespace std;

int multiply(int a, int b) {


int main () {
return (a*b);
} int x=5,y=2,z=3;
double n=5.0,m=2.0;
double multiply(double a, double b) { cout << multiply (x,y) << '\n';
return (a*b);
} cout << multiply (n,m) << '\n';
cout << multiply (x,y,z) << '\n';
int multiply(int a, int b, int c) { return 0;
return (a*b*c);
}
}
NOT A FUNCTION OVERLOADING

#include <iostream>
using namespace std; int main () {

int multiply(int a, int b) { int x=5,y=2,z=3;


return (a*b); double n=5.0,m=2.0;
} cout << multiply (x,y) << '\n';
return 0;
double multiply(int a, int b) {
return (a*b); }
}
EXERCISE

Write a function named swap, that will accept two integers as its argument and swap their values.
Write a main function and call swap function with with integer variables to swap their values and then
print the swapped values in main function.

Sample input output (all print outs must be done in main function):

Enter first integer x = 10


Enter second integer y =40

After Swapping..
x= 40
y=10

Hint : use call-by-reference method to pass variables to swap function.


ASSIGNMENT 3

Write four overloaded swap function to swap (a) two integers, (b) two floats, (c) two doubles and (d)
two character variables.
Call each of these functions from main function to swap each type of variables.

Submission detail:
- Deadline:17.10.2017
- Email attachment (.txt file)
- Email Subject: assignment3_PL2
- Write your name and id in the email body

For Section A+B1: Send your assignment to the following email address: asiful.islam@aiub.edu
For Section C+B2: Send your assignment to the following email address: sabilah.nishat@gmail.com

Вам также может понравиться