Session 4 Structures and Classes: CS 200 - Introduction To Programming
Session 4 Structures and Classes: CS 200 - Introduction To Programming
SESSION 4
STRUCTURES AND CLASSES
char
ifstream
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 10- 2
Class Definitions
can hold
A description of the member functions
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 10- 3
Structures
A structure can be viewed as an object
Contains no member functions
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 10- 4
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
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 10- 5
Using the Structure
Structure definition is generally placed outside
any function definition
This makes the structure type available to all code
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 10- 6
The Structure Value
balance
interest_rate
term
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 10- 7
Specifying Member Variables
Member variables are specific to the
structure variable in which they are declared
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 10- 8
Using Member Variables
Member variables can be used just as any other
variable of the same type
Display 10.1 (1)
my_account.balance = 1000;
Display 10.2
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 10- 9
Display 10.1 (1/2)
Back Next
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 10- 10
Display 10.1
(2/2) Back Next
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 10- 11
Display 10.2 Back Next
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 10- 12
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;
}; };
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 10- 13
Structures as Arguments
Example:
void get_data(CDAccount& the_account);
Uses the structure type CDAccount we saw
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 10- 14
Structures as Return Types
Structures can be the type of a value returned by
a function
Example:
CDAccount shrink_wrap(double the_balance,
double the_rate,
int the_term)
{
CDAccount temp;
temp.balance = the_balance;
temp.interest_rate = the_rate;
temp.term = the_term;
return temp;
}
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 10- 15
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);
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 10- 16
Assignment and Structures
The assignment operator can be used to assign
values to structure types
Using the CDAccount structure again:
CDAccount my_account, your_account;
my_account.balance = 1000.00;
my_account.interest_rate = 5.1;
my_account.term = 12;
your_account = my_account;
Assigns all member variables in your_account the
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 10- 17
Hierarchical Structures
Structures can contain member variables that are
also structures
struct PersonInfo
struct Date {
{ double height;
int month; int weight;
int day; Date birthday;
int year; };
};
struct PersonInfo contains a Date structure
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 10- 18
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
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 10- 19
Initializing Structures
A structure can be initialized when declared
Example:
struct Date
{
int month;
int day;
int year;
};
Can be initialized in this way
Date due_date = {12, 31, 2004};
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 10- 20
Section 10.1 Conclusion
Can you
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 10- 21
10.2
Classes
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 10- 23
A Class Example
To create a new type named DayOfYear as
a class definition
Decide on the values to represent
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 10- 24
Class DayOfYear Definition
class DayOfYear
{
public:
void output( );
int month;
int day;
};
Member Function Declaration
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 10- 25
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 © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 10- 26
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 © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 10- 27
The ‘::’ Operator
‘::’ is the scope resolution operator
Tells the class a member function is a member
of
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 10- 28
‘::’ and ‘.’
‘::’ used with classes to identify a member
void DayOfYear::output( )
{
// function body
}
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 10- 29
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 for
use by the output function
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 10- 30
Display 10.3 (1/2)
Back Next
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 10- 31
Display 10.3
(2/2) Back Next
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 10- 32
Encapsulation
Encapsulation is
Combining a number of items, such as
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 10- 33
Problems With DayOfYear
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
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 10- 34
Ideal Class Definitions
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 10- 35
Fixing DayOfYear
To fix DayOfYear
We need to add member functions to use when
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 10- 36
Public Or Private?
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 10- 37
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
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 10- 38
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
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 10- 39
A New DayOfYear
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 10- 40
Display 10.4 (1/2)
Back Next
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 10- 41
Display 10.4 (2/2)
Back Next
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 10- 42
Using Private Variables
It is normal to make all member variables private
Private variables require member functions to
perform all changing and retrieving of values
Accessor functions allow you to obtain the
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 10- 43
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 © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 10- 44
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
};
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 10- 45
The Assignment Operator
tomorrow.set(11, 19);
due_date = tomorrow;
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 10- 46
Program Example:
BankAccount Class
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 10- 47
Calling Public Members
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 10- 48
Calling Private Members
When a member function calls a private
member function, an object name is not used
fraction (double percent);
void BankAccount::update( )
{
balance = balance + fraction(interest_rate)*
balance;
}
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 10- 49
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
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 10- 50
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
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 10- 51
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 © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 10- 52
Calling A Constructor (1)
BankAccount account1;
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 10- 53
Calling A Constructor (2)
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 10- 54
Overloading Constructors
Constructors can be overloaded by defining
constructors with different parameter lists
Other possible constructors for the
BankAccount
class might be
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 10- 55
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
};
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 10- 56
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
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 10- 57
Calling the Default Constructor
The default constructor is called during
declaration of an object
An argument list is not used
BankAccount account1;
// uses the default BankAccount
constructor
Display 10.6 (1)
BankAccount account1( ); Display 10.6 (2)
// Is not legal
Display 10.6 (3)
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 10- 58
Display 10.6
(1/3) Back Next
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 10- 59
Display 10.6 (2/3)
Back Next
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 10- 60
Display 10.6
(3/3) Back Next
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 10- 61
Initialization Sections
An initialization section in a function definition
provides an alternative way to initialize
member variables
BankAccount::BankAccount( ): balance(0),
interest_rate(0.0);
{
// No code needed in this example
}
The values in parenthesis are the initial values for the
member variables listed
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 10- 62
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
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 10- 63
Section 10.2 Conclusion
Can you
Describe the difference between a class and
a structure?
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 10- 64
10.3
Abstract Data Types
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 10- 66
Classes To Produce ADTs
To define a class so it is an ADT
Separate the specification of how the type is used
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 10- 67
ADT Interface
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 10- 68
ADT Implementation
The ADT implementation tells how the
interface is realized in C++
The implementation consists of
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 10- 69
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
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 10- 70
Program Example
The BankAccount ADT
In this version of the BankAccount ADT
Data is stored as three member variables
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 10- 72
Display 10.7 (2/3)
Back Next
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 10- 73
Display 10.7 (3/3)
Back Next
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 10- 74
Interface Preservation
changed
Public member definitions can be changed
deleted, or changed
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 10- 75
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
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 10- 76
Section 10.3 Conclusion
Can you
Describe an ADT?
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 10- 77
Chapter 10 -- End
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 10- 78
Display 10.1 (1/2)
Back Next
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 10- 79
Display 10.1
(2/2) Back Next
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 10- 80
Display 10.2 Back Next
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 10- 81
Display 10.3 (1/2)
Back Next
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 10- 82
Display 10.3
(2/2) Back Next
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 10- 83
Display 10.4 (1/2)
Back Next
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 10- 84
Display 10.4 (2/2)
Back Next
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 10- 85
Display 10.5 (1/4)
Back Next
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 10- 86
Display 10.5 (2/4)
Back Next
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 10- 87
Display 10.5
(3/4) Back Next
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 10- 88
Display 10.5
(4/4) Back Next
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 10- 89
Display 10.6
(1/3) Back Next
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 10- 90
Display 10.6 (2/3)
Back Next
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 10- 91
Display 10.6
(3/3) Back Next
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 10- 92
Display 10.7 (1/3)
Back Next
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 10- 93
Display 10.7 (2/3)
Back Next
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 10- 94
Display 10.7 (3/3)
Back Next
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 10- 95