0% found this document useful (0 votes)
3 views

QandA3 ProgrammingConcepts

Uploaded by

Bhumika Kukade
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

QandA3 ProgrammingConcepts

Uploaded by

Bhumika Kukade
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 52

QandA Module 3:

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

void f(int& ref_par); void f(int var_par);


Slide 10
Mixed Parameter Lists
• Can call-by-value and call-by-reference
parameters be mixed in the same function?

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

• How do you decide whether a call-by-


reference or call-by-value formal parameter is
needed?

Slide 13
Choosing Parameter Types

• How do you decide whether a call-by-


reference
or call-by-value formal parameter is needed?
– Does the function need to change the value of the

variable used as an argument?


– Yes? Use a call-by-reference formal parameter
– No? Use a call-by-value formal parameter

Slide 14
6.2
Classes
• What is a class in C++ ?

Copyright © 2003 Pearson Education, Slide 15


Inc.
6.2
Classes
• A class is a data type whose variables are
objects
– The definition of a class includes
• Description of the kinds of values of the member
variables
• Description of the member functions
– A class description is somewhat like a
structure definition plus the member variables

Copyright © 2003 Pearson Education, Slide 16


Inc.
A Class Example
• Create a new type named DayOfYear as
a class definition
– This example’s values are dates such as July 4
using an integer for the number of the month

• Member variable month is an int (Jan = 1, Feb = 2, etc.)


• Member variable day is an int
– Use just one member function named output

Copyright © 2003 Pearson Education, Slide 17


Inc.
Class DayOfYear Definition

class DayOfYear
{
public:
void output( );
int month;
int day;
};
Member Function Declaration

Copyright © 2003 Pearson Education, Slide 18


Inc.
Defining a Member Function
• Member functions are declared in the class
declaration
• Member function definitions identify the class
in which the function is a member
– void DayOfYear::output()
{
cout << “month = “ << month
<< “, day = “ << day
<< endl;
}
Copyright © 2003 Pearson Education, Slide 19
Inc.
Member Function Definition

• Member function definition syntax:


Returned_Type Class_Name::Function_Name(Parameter_List)
{
Function Body Statements
}
– Example: void DayOfYear::output( )
{
cout << “month = “ << month
<< “, day = “ << day << endl;
}

Copyright © 2003 Pearson Education, Slide 20


Inc.
The ‘::’ Operator
• ‘::’ is the scope resolution operator
– Tells the class a member function is a member of

– void DayOfYear::output( ) indicates that function


output is a member of the DayOfYear class

– The class name that precedes ‘::’ is a type


qualifier

Copyright © 2003 Pearson Education, Slide 21


Inc.
‘::’ and ‘.’
• ‘::’ used with classes to identify a member
void DayOfYear::output( )
{
// function body
}

• ‘.’used with variables to identify a member


DayOfYear birthday;
birthday.output( );

Copyright © 2003 Pearson Education, Slide 22


Inc.
Calling Member Functions

• Calling the DayOfYear member function


output
is done in this way:
DayOfYear today, birthday;
today.output( );
birthday.output( );
– Note that today and birthday have their own
versions of the month and day variables
Displayfor 6.3
use (1)
by the output function Display 6.3 (2)
Copyright © 2003 Pearson Education, Slide 23
Inc.
Problems
• Changing how the month is stored in the class
DayOfYear requires changes to the program
• If we decide to store the month as three
characters (JAN, FEB, etc.) instead of an int
– cin >> today.month will no longer work because
we now have three character variables to read
– if(today.month == birthday.month) will no longer
work to compare months
– The member function “output” no longer works
Copyright © 2003 Pearson Education, Slide 24
Inc.
Ideal Class Definitions
• Changing the implementation of DayOfYear
requires changes to the program that uses
DayOfYear
• An ideal class definition of DayOfYear could
be changed without requiring changes to
the program that uses DayOfYear

Copyright © 2003 Pearson Education, Slide 25


Inc.
Fixing DayOfYear
• How To fix DayOfYear

Copyright © 2003 Pearson Education, Slide 26


Inc.
Fixing DayOfYear
• To fix DayOfYear
– We need to add member functions to use when
changing or accessing the member variables
• If the program never directly references the member
variables, changing how the variables are stored will
not require changing the program
– We need to be sure that the program does not
ever directly reference the member variables

Copyright © 2003 Pearson Education, Slide 27


Inc.
Public Or Private?
• C++ helps us restrict the program from directly

referencing member variables


– private members of a class can only be referenced

within the definitions of member functions


• If the program tries to access a private member, the
compiler gives an error message
– Private members can be variables or functions

Copyright © 2003 Pearson Education, Slide 28


Inc.
Private Variables
• Private variables cannot be accessed directly
by the program
– Changing their values requires the use of public
member functions of the class
– To set the private month and day variables in a
new
DayOfYear class use a member function such as
void DayOfYear::set(int new_month, int new_day)
{
month = new_month;
day = new_day;
}
Copyright © 2003 Pearson Education, Slide 29
Inc.
Public or Private Members
• The keyword private identifies the members of
a class that can be accessed only by member
functions of the class
– Members that follow the keyword private are
private members of the class
• The keyword public identifies the members of
a class that can be accessed from outside the
class
– Members that follow the keyword public are public
members of the class

Copyright © 2003 Pearson Education, Slide 30


Inc.
General Class
Definitions
• What is the general syntax for a class definition?

Copyright © 2003 Pearson Education, Slide 31


Inc.
General Class Definitions

• The syntax for a class definition is


– class Class_Name
{
public:
Member_Specification_1
Member_Specification_2

Member_Specification_3
private:
Member_Specification_n+1
Member_Specification_n+2

};
Copyright © 2003 Pearson Education, Slide 32
Inc.
Declaring an Object
• Once a class is defined, how is an object of the
class declared ?

Copyright © 2003 Pearson Education, Slide 33


Inc.
Declaring an Object
• Once a class is defined, an object of the class
is declared just as variables of any other type
– Example: To create two objects of type Bicycle:
class Bicycle
{
// class definition lines
};

Bicycle my_bike, your_bike;

Copyright © 2003 Pearson Education, Slide 34


Inc.
The Assignment Operator
• Objects and structures can be assigned values
with the assignment operator (=) ?

Copyright © 2003 Pearson Education, Slide 35


Inc.
The Assignment Operator
• Yes, objects and structures can be assigned
values with the assignment operator (=)
– Example:
DayOfYear due_date,
tomorrow;

tomorrow.set(11, 19);

due_date = tomorrow;

Copyright © 2003 Pearson Education, Slide 36


Inc.
Calling Public Members
• How is a member function called from the
main function of a program?

Copyright © 2003 Pearson Education, Slide 37


Inc.
Calling Public Members
• If calling a member function from the main
function of a program, you must include the
the object name. For e.g.
account1.update( );

Copyright © 2003 Pearson Education, Slide 38


Inc.
Calling Private Members
• When a member function calls a private
member function, an object name is not used
– fraction (double percent);
is a private member of the BankAccount class
– fraction is called by member function update
void BankAccount::update( )
{
balance = balance + fraction(interest_rate)*
balance;
}
Copyright © 2003 Pearson Education, Slide 39
Inc.
Constructors
• What is a constructor?

Copyright © 2003 Pearson Education, Slide 40


Inc.
Constructors
• A constructor can be used to initialize member
variables when an object is declared
– A constructor is a member function that is usually
public
– A constructor is automatically called when an object
of the class is declared
– A constructor’s name must be the name of the class
– A constructor cannot return a value
• No return type, not even void, is used in declaring or
defining a constructor

Copyright © 2003 Pearson Education, Slide 41


Inc.
Constructors
• How is a constructor declared and defined?

Copyright © 2003 Pearson Education, Slide 42


Inc.
Constructor Declaration
• A constructor for the BankAccount class could
be declared as:

class BankAccount
{
public:
BankAccount(int dollars, int cents, double rate);
//initializes the balance to $dollars.cents
//initializes the interest rate to rate percent

…//The rest of the BankAccount definition


};

Copyright © 2003 Pearson Education, Slide 43


Inc.
Constructor Definition
• The constructor for the BankAccount class
could be defined as
BankAccount::BankAccount(int dollars, int cents, double rate)
{
if ((dollars < 0) || (cents < 0) || ( rate < 0 ))
{
cout << “Illegal values for money or rate\n”;
exit(1);
}
balance = dollars + 0.01 * cents;
interest_rate = rate;
}

– Note that the class name and function name are the same

Copyright © 2003 Pearson Education, Slide 44


Inc.
17.1

Exception Handling
• How are exceptions handled in C++?

Copyright © 2003 Pearson Education, Slide 45


Inc.
17.1

Exception Handling Basics

• It is often easier to write a program by first


assuming that nothing incorrect will happen
• Once it works correctly for the expected
cases, add code to handle the exceptional
cases
• Exception handling is commonly used to
handle error situations
– Once an error is handled, it is no longer an error

Copyright © 2003 Pearson Education, Slide 46


Inc.
Functions and
Exception Handling
• A common use of exception handling:
– Functions with a special case that is handled in
different ways depending on how the function is
used
– If the function is used in different programs, each
program may require a different action when the
special case occurs

Copyright © 2003 Pearson Education, Slide 47


Inc.
Exception Handling
Mechanism
• In C++, exception handling proceeds by:
– Some library software or your code signals that
something unusual has happened
– This is called throwing an exception
– At some other place in your program you place
the
code that deals with the exceptional case
– This is called handling the exception

Copyright © 2003 Pearson Education, Slide 48


Inc.
The Try Block Outline
• The try block encloses code that you want to
"try" but that could cause a problem
• The basic outline of a try block is:

try
{
Code_To_Try
Possibly_Throw_An_Exception
More_Code
}

Copyright © 2003 Pearson Education, Slide 49


Inc.
The catch-block
• Something that is thrown goes from one place
to another
• In C++ throw causes the flow of control to go
to another place
– When an exception is thrown, the try block stops
executing and the catch-block begins execution
– This is catching or handling the exception

Copyright © 2003 Pearson Education, Slide 50


Inc.
The catch-block Parameter

• The catch-block parameter, (recall that the


catch-block is not a function) does two things:
– The type of the catch-block parameter identifies
the kind of value the catch-block can catch
– The catch-block parameter provides a name for
the value caught so you can write code using the
value that is caught

Copyright © 2003 Pearson Education, Slide 51


Inc.
try-blocks and if-else
• try-blocks are very similar to if-else statements
– If everything is normal, the entire try-block is
executed
– else, if an exception is thrown, the catch-block is
executed
• A big difference between try-blocks and if-else
statements is the try-block's ability to send a
message to one of its branches

Copyright © 2003 Pearson Education, Slide 52


Inc.

You might also like