QandA3 ProgrammingConcepts
QandA3 ProgrammingConcepts
Programming Concepts
(Using C++)
Slide 1
Reference:
Slide 2
The Main Function
• The main function in a program is used like a
void function.
• Do you have to end the program with a
return-statement?
Slide 3
The Main Function
• The main function in a program is used like a
void function.
• Do you have to end the program
with a return-statement?
– Because the main function is defined to return a
value of type int, the return is needed
– C++ standard says the return 0 can be omitted, but
many compilers still require it
Slide 4
4.2
Call-by-Reference Parameters
• Why is Call-by-reference required?
• Why is Call-by-value not adequate?
Slide 5
4.2
Call-by-Reference Parameters
• Call-by-value is not adequate when we need
a sub-task to obtain input values
– Call-by-value means that the formal parameters
receive the values of the arguments
– To obtain input values, we need to change the
variables that are arguments to the function
• Recall that we have changed the values of
formal parameters in a function body, but we have not
changed the arguments found in the function call
• Call-by-reference parameters allow us to change
the variable used in the function call
– Arguments for call-by-reference parameters must be
variables, not numbers
Slide 6
4.2
Call-by-Reference Example
• Give an example of Call-By-reference usage.
Slide 7
Call-by-Reference Example
• void get_input(double& f_variable)
{
using namespace std;
cout << “ Convert a Fahrenheit temperature”
<< “ to Celsius.\n”
<< “ Enter a temperature in Fahrenheit: “;
cin >> f_variable;
}
• ‘&’ symbol (ampersand) identifies f_variable as a
Display 4.4 (1)
call-by-reference parameter
– Used in both declaration and definition! Display 4.4 (2)
Slide 8
Call-By-Reference Details
• Call-by-reference works almost as if the
argument variable is substituted for the formal
parameter, not the argument’s value
• In reality, the memory location of the
argument variable is given to the formal
parameter
– Whatever is done to a formal parameter in the
function body, is actually done to the value at the
memory location of the argument variable
Slide 9
Call Comparisons
Call By Reference vs Value
• Call-by-reference • Call-by-value
– The function call: – The function call:
f(age); Memory f(age);
Name Location Contents
age 1001 34
initial 1002 A
hours 1003 23.5
1004
Slide 11
Mixed Parameter Lists
• Call-by-value and call-by-reference parameters
can be mixed in the same function
• Example:
void good_stuff(int& par1, int par2, double& par3);
– par1 and par3 are call-by-reference formal
parameters
• Changes in par1 and par3 change the argument variable
– par2 is a call-by-value formal parameter
• Changes in par2 do not change the argument variable
Slide 12
Choosing Parameter Types
Slide 13
Choosing Parameter Types
Slide 14
6.2
Classes
• What is a class in C++ ?
class DayOfYear
{
public:
void output( );
int month;
int day;
};
Member Function Declaration
tomorrow.set(11, 19);
due_date = tomorrow;
class BankAccount
{
public:
BankAccount(int dollars, int cents, double rate);
//initializes the balance to $dollars.cents
//initializes the interest rate to rate percent
– Note that the class name and function name are the same
Exception Handling
• How are exceptions handled in C++?
try
{
Code_To_Try
Possibly_Throw_An_Exception
More_Code
}