Documente Academic
Documente Profesional
Documente Cultură
Using C++
Const in OOP
Objects as Function Arguments
Road Map
▶ Const in OOP continued
▶ Objects as function Arguments
2
Lesson 1: const in OOP
4
Passing by pointer Vs Passing by
Reference in C++
Difference in Reference variable and
pointer
▶ A pointer can be re-assigned while reference cannot, and must
be assigned at initialization only.
▶ Pointer can be assigned NULL directly, whereas reference
cannot.
▶ A pointer is a variable that holds a memory address. A
reference has the same memory address as the item it
references.
▶ A pointer to a class/struct uses ‘->'(arrow operator) to access
it’s members whereas a reference uses a ‘.'(dot operator)
▶ A pointer needs to be dereferenced with * to access the
memory location it points to, whereas a reference can be used
directly.
7
References in C++
▶ When a variable is
declared as reference, it
becomes an alternative
name for an existing
variable. A variable can
be declared as reference
by putting ‘&’ in the
declaration.
9
10
passing an argument by reference
▶ Modification: used to allow a function to modify a
variable in the calling program.
▶ Efficiency: only an address is really passed, not the
entire variable.
▶ Suppose you want to pass an argument by reference
for efficiency, but not modification.
▶ This modification can be avoided by making the
argument const (constant)
11
Output
107
12
const Function Argument
▶ This modification can be avoided by making the
argument const (constant)
▶ If you want to pass a const variable to a function as a
reference argument, you don’t have a choice: It must
be declared const in the function declaration.
▶
13
Error
Output
107
11
Lesson 2 :Objects as Function Arguments
Objects as Function Arguments
▶ In C++ we can pass class’s objects as
function arguments and also return them
from a function the same way we pass and
return other variables.
▶ No special keyword or header file is
required to do so.
▶ To pass an object as an argument we write
the object name as the argument while
calling the function the same way we do it
for other variables.
17
Function definition
Return type function_name(data_type variable_name)
{
…………..
}
18
Function Call
Object_name.function_name(variable_name)
Object_name.function_name(object_name)
19
Function definition and Call
20
Function Call
Object_name.function_name(variable_name)
Object_name.function_name(object_name)
21
Objects as Function Arguments
22
Objects as Function Arguments
24
Objects as Function Arguments
Class Distance
{
private:
int feet;
float inches
public:
Distance() : feet(0), inches(0.0)
{}
Distance(int ft, float in): feet(ft), inches(in)
{}
25
Objects as Function Arguments
void setdist ( ) {
cout <<“\n Enter feet : “;
cin >> feet;
cout <<“\nEnter inches : “; cin >>
inches;
}
void showdist ( ) {
cout <<feet << “ \ -” << inches <<“\n” ;
}
};
26
Objects as Function Arguments
void setdist ( ) {
cout <<“\n Enter feet : “;
cin >> feet;
cout <<“\nEnter inches : “; cin >>
inches;
}
void showdist ( ) {
cout <<feet << “ \ -” << inches <<“\n” ;
}
void add_dist(Distance, Distance);
};
27
Objects as Function Arguments
28
Objects as Function Arguments
29
Objects as Function Arguments
30
Weight Class Example
32
Class Definition
33
Defining Member Functions
34
Testing Weight Class
35
Weight Class
36