Day4.0 Classes
Day4.0 Classes
(Using C++)
Slide 1
Reference:
Slide 2
Object Oriented
Programming
• Abbreviated OOP
Slide 3
OOP Characteristics
• Encapsulation
– Information hiding
– Objects contain their own data and algorithms
• Inheritance
– Writing reusable code
– Objects can inherit characteristics from other objects
• Polymorphism
– A single name can have multiple meanings depending
on its context
Slide 4
Defining Classes
Slide 5
Overview
▪ Introduction
▪ Structures
▪ Classes
▪ Abstract Data Types
Slide 6
What Is a Class?
• A class is a data type whose variables are
objects
• Some pre-defined classes you have used are
– int
– char
– ifstream
• You can define your own classes as well
Slide 7
Class Definitions
Slide 8
6.1
Structures
• A structure can be viewed as an object
– Contains no member functions
(The structures used here have no member functions)
Slide 9
The CD Definition
• The Certificate of Deposit structure can be
defined as
struct CDAccount
{
double balance;
double interest_rate;
int term; //months to maturity
}; Remember this semicolon!
• Keyword struct begins a structure definition
• CDAccount is the structure tag or the structure’s
type
• Member names are identifiers declared in the braces
Slide 10
Using the Structure
• Structure definition is generally placed outside
any function definition
– This makes the structure type available to all code
that follows the structure definition
• To declare two variables of type CDAccount:
balance
interest_rate
term
Slide 12
Specifying Member
Variables
• Member variables are specific to the
structure variable in which they are declared
– Syntax to specify a member variable:
Structure_Variable_Name . Member_Variable_Name
Slide 13
Using Member Variables
Slide 14
Duplicate Names
• Member variable names duplicated between
structure types are not a problem.
struct FertilizerStock struct CropYield
{ {
double quantity; int quantity;
double nitrogen_content; double size;
}; };
Slide 15
Structures as Arguments
Slide 16
Structures as Return Types
Slide 17
Using Function shrink_wrap
• shrink_wrap builds a complete structure value
in temp, which is returned by the function
• We can use shrink_wrap to give a variable of
type CDAccount a value in this way:
CDAccount new_account;
new_account = shrink_wrap(1000.00, 5.1,
11);
Slide 18
Assignment and Structures
Slide 20
Using PersonInfo
• A variable of type PersonInfo is declared by
PersonInfo person1;
• To display the birth year of person1, first access the
birthday member of person1
Slide 22
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
Slide 23
A Class Example
• To create a new type named DayOfYear as
a class definition
– Decide on the values to represent
– This example’s values are dates such as July 4
using an integer for the number of the month
Slide 24
Class DayOfYear Definition
class DayOfYear
{
public:
void output( );
int month;
int day;
};
Member Function Declaration
Slide 25
Defining a Member Function
Slide 26
Member Function Definition
Slide 27
The ‘::’ Operator
• ‘::’ is the scope resolution operator
– Tells the class a member function is a member of
Slide 28
‘::’ and ‘.’
• ‘::’ used with classes to identify a member
void DayOfYear::output( )
{
// function body
}
Slide 29
Calling Member Functions
• Encapsulation is
– Combining a number of items, such as variables
and functions, into a single package such as an
object of a class
Slide 31
Problems With DayOfYear
Slide 32
Ideal Class Definitions
Slide 33
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
Slide 34
Public Or Private?
Slide 35
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;
}
Slide 36
Public or Private Members
Slide 37
A New DayOfYear
Slide 38
Using Private Variables
Slide 39
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
…
};
Slide 40
Declaring an Object
Slide 41
The Assignment Operator
tomorrow.set(11, 19);
due_date = tomorrow;
Slide 42
Program Example:
BankAccount Class
• This bank account class allows
– Withdrawal of money at any time
– All operations normally expected of a bank
account
(implemented with member functions)
– Storing an account balance
– Storing the account’s interest rate
Slide 43
Calling Public Members
Slide 44
Calling Private Members
Slide 45
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
Slide 46
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
};
Slide 47
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
Slide 48
Calling A Constructor
BankAccount account1;
Slide 49
Calling A Constructor
Slide 50
Overloading Constructors
Slide 51
The Default Constructor
• A default constructor uses no parameters
• A default constructor for the BankAccount class
could be declared in this way
class BankAccount
{
public:
BankAccount( );
// initializes balance to $0.00
// initializes rate to 0.0%
… // The rest of the class definition
};
Slide 52
Default Constructor
Definition
• The default constructor for the BankAccount
class could be defined as
BankAccount::BankAccount( )
{
balance = 0;
rate = 0.0;
}
• It is a good idea to always include a default
constructor even if you do not want to initialize
variables
Slide 53
Calling Default Constructor
BankAccount account1;
// uses the default BankAccount
constructor
BankAccount account1( );
// Is not legal
Slide 54
Initialization Sections
{
// No code needed in this example
}
– The values in parenthesis are the initial values for
the member variables listed
Slide 55
Parameters and Initialization
• Member functions with parameters can use
initialization sections
BankAccount::BankAccount(int dollars, int cents, double
rate)
: balance (dollars + 0.01 *
cents),
interest_rate(rate)
{
if (( dollars < 0) || (cents < 0) || (rate < 0))
{
cout << “Illegal values for money or rate\n”;
exit(1);
}
}
– Notice that the parameters can be arguments in the initialization
Slide 56
6.3
Abstract Data Types
Slide 57
Classes To Produce ADTs
Slide 58
ADT Interface
• The ADT interface tells how to use the ADT in
a program
– The interface consists of
• The public member functions
• The comments that explain how to use the functions
– The interface should be all that is needed to know
how to use the ADT in a program
Slide 59
ADT Implementation
• The ADT implementation tells how the
interface is realized in C++
– The implementation consists of
• The private members of the class
• The definitions of public and private member functions
– The implementation is needed to run a program
– The implementation is not needed to write the
main part of a program or any non-member
functions
Slide 60
ADT Benefits
• Changing an ADT implementation does
require
changing a program that uses the ADT
• ADT’s make it easier to divide work among
different programmers
– One or more can write the ADT
– One or more can write code that uses the ADT
• Writing and using ADTs breaks the larger
programming task into smaller tasks
Slide 61
Program Example
The BankAccount ADT
• In this version of the BankAccount ADT
– Data is stored as three member variables
• The dollars part of the account balance
• The cents part of the account balance
• The interest rate
– This version stores the interest rate as a fraction
Slide 62
Interface Preservation
• To preserve the interface of an ADT so that
programs using it do not need to be changed
– Public member declarations cannot be changed
– Public member definitions can be changed
– Private member functions can be added, deleted,
or changed
Slide 63
Information Hiding
• Information hiding was refered to earlier as
writing functions so they can be used like
black boxes
• ADT’s implement information hiding because
– The interface is all that is needed to use the ADT
– Implementation details of the ADT are not needed
to know how to use the ADT
– Implementation details of the data values are not
needed to know how to use the ADT
Slide 64
Display 6.1
(1/2) Back Next
Slide 65
Display 6.1
(2/2) Back Next
Slide 66
Display 6.2
Back Next
Slide 67
Display 6.3
(1/2) Back Next
Slide 68
Display 6.3
(2/2) Back Next
Slide 69
Display 6.4
(1/3) Back Next
Slide 70
Display 6.4
(2/3) Back Next
Slide 71
Display 6.4
(3/3) Back Next
Slide 72
Display 6.5
(1/4) Back Next
Slide 73
Display 6.5
(2/4) Back Next
Slide 74
Display 6.5
(3/4) Back Next
Slide 75
Display 6.5
(4/4) Back Next
Slide 76
Display 6.6
(1/3) Back Next
Slide 77
Display 6.6
(2/3) Back Next
Slide 78
Display 6.6
(3/3) Back Next
Slide 79
Display 6.7
(1/4) Back Next
Slide 80
Display 6.7
(2/4)Back Next
Slide 81
Display 6.7
(3/4) Back Next
Slide 82
Display 6.7
(4/4)Back Next
Slide 83