ADT class interface, constructor, default constructor

CS183-Su'02-Lecture 4
2 June 2002
Additional Member Functions
„
So far…
„
„
„
„
„
Constructors [more today]
Accessors
Mutators
Operators
New today
„
„
„
Copy constructor
Assignment/operator=
Destructor
1
Recall: Complex class
Definition
class Complex {
public:
Complex(double r=0.0, double i=0.0);
Complex Add(const Complex& right) const;
Complex Subtract(const Complex& right) const;
Complex Multiply(const Complex& right) const;
…
bool Equal(const Complex& right) const;
bool LessThan(const Complex& right) const;
…
void Insert(ostream& os) const;
void Extract(istream& is);
…
private:
double real;
double imaginary;
};
2
Constructors
„
Special member function
„
„
„
„
Job is to initialize class objects
Same name as the class
No return type
Variations
„
„
No argument – generic values
Easily customizable using overloading
3
© 2002 by Eric A. Durant, Ph.D.
1
CS183-Su'02-Lecture 4
2 June 2002
Complex Constructor
class Complex {
public:
Complex(double r=0.0, double i=0.0);
Complex::Complex(double r, double i)
{
real = r;
imaginary = i;
}
–OR–
Complex::Complex(double r, double i) :
real(r), imaginary(i)
{}
Initializer lists are common!
4
Copy Constructor
„
„
Makes a new copy of an object
Used by the compiler where needed
„
„
„
„
By value function parameters
Function return values
Some object declarations
Compiler default (if one not written)
„
Make a copy of each data member
5
Copy Constructor Example
„
In .h file
class Complex { …
Complex(const Complex& right);
…};
„
In .cpp file
Complex::Complex(const Complex& right)
{
real = right.real;
imaginary = right.imaginary;
}
6
© 2002 by Eric A. Durant, Ph.D.
2
CS183-Su'02-Lecture 4
2 June 2002
Assignment/operator=
„
Similar to the copy constructor
„
Except…
„
„
„
„
„
„
Make a copy
Assignments can be chained
Careful about self-assignment
This is an overwrite operation
MUST be a member function
Compiler default
„
Make a copy of each data member
7
operator= Example
???? Complex::operator=(const
Complex& right)
Don’t assign over yourself!
{
if (???? != &right) {
real = right.real;
imaginary = right.imaginary;
}
return ????;
}
8
Referring to Ourselves
„
Sometimes an object must refer to itself
„
„
„
Not as individual members
But as a whole
Special “variable” exists
„
„
„
Similar to an iterator
this – pointer to the current object
*this – the current object
Accessible in member functions
9
© 2002 by Eric A. Durant, Ph.D.
3
CS183-Su'02-Lecture 4
2 June 2002
Complete operator=
For efficiency and “first class” lvalue behavior
Complex& Complex::operator=(const
Complex& right)
{
if ( this != &right) {
real = right.real;
imaginary = right.imaginary;
}
return *this ;
}
10
Destructor/Deconstructor
„
Cleans up an object at the end of lifetime
„
„
Cleans up the data members prior to their own
destruction
Closes things, etc.
„
Special name
„
Compiler default
„
„
~classname
Destroy each data member
11
Destructor Example
„
„
In .h file
class Complex {…
public:
~Complex(); No arguments or return
…};
In .cpp file
Complex::~Complex()
{} // Do nothing!
12
© 2002 by Eric A. Durant, Ph.D.
4
CS183-Su'02-Lecture 4
2 June 2002
ADT Design Example
„
Red/Yellow/Green game
„
James P. Cohoon and Jack W. Davidson, C++
Program Design: An Introduction to Programming
and Object-Oriented Design, McGraw-Hill, 1999,
pp. 437-55.
„
Two implementations
„
Text:
http://people.msoe.edu/~welch/courses/cs183/rygtxt.zip
„
„
GUI:
http://people.msoe.edu/~welch/courses/cs183/ryggui.zip
Consider the design decisions
13
Game Concept
„
„
Mastermind® –
http://www.javaonthebrain.com/java/
mastermind/
Computer has a secret number
„
„
„
Three decimal digits
Player makes a guess
(Unordered) feedback for each guessed digit
„
„
„
Green – correct digit and position
Yellow – correct digit, wrong position
Red – digit not in number
14
Classes Chosen
Number
Response
GameRYG
15
© 2002 by Eric A. Durant, Ph.D.
5
CS183-Su'02-Lecture 4
2 June 2002
User Interface
„
„
Not part of GameRYG class
Interfaces built around the game
classes
„
„
„
Text version
GUI version
Design principle
„
“Model-View Separation”
16
Model-View Separation
„
„
How is it accomplished? Is there a trick?
No, just code abstractly…
„
„
Don’t hardcode I/O
Make the classes a “black box”
„
„
Functions for starting game, getting secret number,
evaluating guess as RYG, etc.
Interface chooses how to use functions
„
Console I/O?, Tie to GUI (ClassWizard?, X11?, …),
Connect to cell phone display or Web application?
17
Sample Screen Shots
Text
Enter guess for number: 123
Guess: 123
Response: 1 red, 0 yellow 2 green
Enter guess for number: 124
Guess: 124
Response: 1 red, 1 yellow 1 green
Enter guess for number:
GUI
18
© 2002 by Eric A. Durant, Ph.D.
6