CSC212
D t St
Data
Structure
t
- Section AB
Lecture 2
ADT and C++ Classes (I)
Instructor: Hao Tang
p
of Computer
p
Science
Department
City College of New York
1
Outline
A Review of C++ Classes (Lecture 2)
OOP,, ADTs and Classes
Class Definition, Implementation and Use
Constructors and Value Semantics
More on Classes (Lecture 3)
Namespace and Documentation
Classes and Parameters
Operator Overloading
2
Object Oriented Programming
Chapter 2 introduces Object Oriented Programming.
OOP is the typical approach to programming which
supports the creation of new data types and operations
to manipulate those types.
This lecture gives a review of C++ Classes and
introduces ADTs.
3
C++ Classes and ADTs
Class
Mechanism
to create objects and member
functions
Support information hiding
Abstract
Data Types (ADTs)
mathematical data type
Class as an ADT that programmers can use
without knowing how the member functions are
implemented - i.e.
i e with information hiding
A point ADT
A data type to store
and manipulate a
single point on a plane
Manipulations
Initialize
Retrieval
Shift
y
2
p1
0
-1
-2
-2
-1
A point ADT
A data type to store
and manipulate a
single point on a plane
Manipulations
Initialize
(-1, 0.8)
Retrieval coordinates
Shift
y
2
p1
0
-1
-2
-2
-1
A point ADT
A data type to store
and manipulate a
single point on a plane
0.8
Manipulations
Initialize
Retrieval coordinates
Shift
y
2
p1
0
-1
-2
-2
-1
-1.0
7
A point ADT
A data type to store
and manipulate a
single point on a plane
Manipulations
Initialize
Retrieval coordinates
Shift by
b (1.3,
(1 3 -1.4)
1 4)
y
2
p1
0
-1
p2
-2
-2
-1
(0.3, -0.6)
Outline
A Review of C++ Classes (Lecture 2)
OOP,, ADTs and Classes
Class Definition
Definition,, Implementation and Use
Constructors and Value Semantics
More on Classes (Lecture 3)
Namespace and Documentation
Classes and Parameters
Operator Overloading
9
2
1
0
-1
-2
point Definition
-2
We can implement the
point object using a
data type called a
class..
class
-1
class point
{
...
};
D t fforgett the
Dont
th
semicolon at the end
10
2
1
0
-1
-2
point Definition
-2
The class will have
two components called
x and y. These
components are the x
and y coordinates of
this point.
Using a class permits
two new features . . .
-1
class point
{
...
double x;
double y;
};
11
2
1
0
-1
-2
point Definition
-2
The two components
will be private
member variables.
variables.
This ensures that
nobod can directl
nobody
directly
access this
information. The
only access is through
functions that we
provide for the class.
-1
class point
{
...
private:
double x;
double y;
};
12
2
1
0
-1
-2
point Definition
-2
In a class, the
functions which
manipulate the class
are also listed.
Prototypes for the point
functions go here,
after
ft th
the wordd public:
bli
-1
class point
{
public:
...
private:
double x;
double y;
};
13
2
1
0
-1
-2
point Definition
-2
In a class, the
functions which
manipulate the class
are also listed.
Prototypes for the point
member functions go
h
here
-1
class point
{
public:
...
private:
double x;
double y;
};
14
point Definition
2
1
0
-1
-2
-2
-1
Our point has at least four member functions:
class p
point
{
public:
void initialize(double init_x,
init x double init_y);
init y);
void shift(double dx, double dy);
double get_x() const;
double get_y( ) const;
private:
double x;;
double y;
};
15
point Definition
2
1
0
-1
-2
-2
-1
The keyword const appears after two prototypes:
class point
{
public:
void
id initialize(double
i iti li (d bl init_x,
i it
double
d bl init_y);
i it )
void shift(double dx, double dy);
double get_x( ) const;
double get_y( ) const;
private:
double x;
double y;
};
16
Files for the point ADT
2
1
0
-1
-2
-2
The point class definition, which
we have just seen, is placed with
documentation in a file called
point.h,, outlined here.
point.h
The implementations of the
f
four
member
b ffunctions
i
will
ill be
b
placed in a separate file called
point.cxx,, which we will examine
ppoint.cxx
in a few minutes.
-1
Documentation:
(P
(Preconditions
diti
and
d
Postconditions)
Class definition:
point class
d fi iti which
definition
hi h we
have already seen
17
Outline
A Review of C++ Classes (Lecture 2)
OOP,, ADTs and Classes
Class Definition
Definition,, Implementation and Use
Constructors and Value Semantics
More on Classes (Lecture 3)
Namespace and Documentation
Classes and Parameters
Operator Overloading
18
Using the point ADT
2
1
0
-1
-2
-2
A program that
wants to use the
point ADT must
include the
point.h header
p
file (along with
its other header
inclusions).
inclusions)
File
pointmain1.cxx
p
-1
#include <iostream.h>
#include <stdlib.h>
#include point
point.h"
h"
...
19
Using the point ADT
2
1
0
-1
-2
-2
Just for
illustration, the
example program
will declare two
point variables
ariables
named p1 and p2.
-1
#include <iostream.h>
#include <stdlib.h>
#include point
point.h"
h"
int main( )
{
point p1;
point p2;
20
Using the point ADT
2
1
0
-1
-2
-2
Just for
illustration, the
example program
will declare two
point objects
named p1 and p2.
-1
#include <iostream.h>
#include <stdlib.h>
#include point
point.h"
h"
int main( )
{
point p1;
point p2;
21
Using the point ADT
2
1
0
-1
-2
-2
The program
starts by
calling the
initialize
member function
f nction
for p1.
-1
#include <iostream.h>
#include <stdlib.h>
#include point
point.h"
h"
int main( )
{
point p1;
point p2;
p1.initialize(-1.0,
initialize( 1 0 0.8);
0 8);
22
Using the point ADT
2
1
0
-1
-2
-2
The program
starts by
activating the
initialize
member function
f nction
for p1.
-1
#include <iostream.h>
#include <stdlib.h>
#include point
point.h"
h"
int main( )
{
point p1;
point p2;
p1 initialize(-1.0, 0.8);
23
Using the point ADT
2
1
0
-1
-2
-2
The member
function
activation
consists of four
parts starting
parts,
with the object
name.
-1
int main( )
{
point p1;
point p2;
p1 initialize(-1.0, 0.8);
24
Using the point ADT
2
1
0
-1
-2
-2
The instance
(object) name is
followed by a
period.
-1
int main( )
{
point p1;
point p2;
p1 initialize(-1.0, 0.8);
25
Using the point ADT
2
1
0
-1
-2
-2
After the period
is the name of
the member
function that you
are acti
activating.
ating
-1
int main( ) {
point p1;
point p2;
p1 initialize(-1.0, 0.8);
26
Using the point ADT
2
1
0
-1
-2
-2
Finally, the
arguments for
the member
function. In this
example
p the first
argument (x
coordinate) and
the second
argument (y
coordinate)
-1
int main( ) {
point p1;
point p2;
p1 initialize(-1.0, 0.8);
27
2
1
0
-1
-2
A Quiz
-2
How would you
activate p1's get_x
member function ?
What would be the
output of p1's
get_x member
function at this
point in the
program ?
-1
int main( )
{
point p1;
point p2;
p1 initialize(-1.0, 0.8);
28
2
1
0
-1
-2
A Quiz
-2
Notice that the get_x
member function has
no arguments
arguments.
At this point,
point
activating p1.get_x
will return a double
value
-1.0.
-1
int main( ) {
point p1;
point p2;
p1 initialize(-1.0, 0.8);
cout << p1.get_x( ) <<endl;
29
A Quiz
2
1
0
-1
-2
-2
int main( )
{
point p1;
point p2;
-1
Trace through this
program, and tell
me the complete
output.
p
p1.initialize(-1.0,
(
0.8);
)
cout << p1.get_x( ) << p1.get_y() << endl;
p2.initialize(p1.get_x(), p1.get_y());
cout << p2.get_x( ) << p2.get_y() << endl;
p2.shift(1.3, -1.4);
cout << p2.get_x( ) << p2.get_y() << endl;
...
30
A Quiz
2
1
0
-1
-2
-2
int main( )
{
point p1;
point p2;
-1
-1.0 0.8
-1.0 0.8
0.3 -0.6
p
p1.initialize(-1.0,
(
0.8);
)
cout << p1.get_x( ) << p1.get_y() << endl;
p2.initialize(p1.get_x(), p1.get_y());
cout << p2.get_x( ) << p2.get_y() << endl;
p2.shift(1.3, -1.4);
cout << p2.get_x( ) << p2.get_y() << endl;
...
31
What you know about Objects
Class = Data + Member Functions.
You know how to define a new class type,
yp , and place
p
the definition in a header file.
You know how to use the header file in a program
which declares instances of the class type.
You know how to activate member functions.
But you still need to learn how to write the bodies of
a classs member functions.
32
Outline
A Review of C++ Classes (Lecture 2)
OOP,, ADTs and Classes
Class Definition
Definition,, Implementation and Use
Constructors and Value Semantics
More on Classes (Lecture 3)
Namespace and Documentation
Classes and Parameters
Operator Overloading
33
point Implementation
2
1
0
-1
-2
-2
-1
Remember that the member functions bodies
generally appear in a separate point.cxx file.
class point
{
public:
void initialize(double init_x, double init_y);
void shift(double dx, double dy);
double get
get_x(
x( ) const;
double get_y( ) const;
private:
double x;
double y;
};
34
point Implementation
2
1
0
-1
-2
-2
-1
We will look at the body of intialize
intialize,, which must assign
its two arguments to the two private member variables.
class point
{
public:
void initialize(double init_x, double init_y);
void shift(double dx, double dy);
double get
get_x(
x( ) const;
double get_y( ) const;
private:
double x;
double y;
};
35
point Implementation
2
1
0
-1
-2
-2
-1
For the most part, the functions body is no different
than any other function body.
void point::initialize(double init_x, double init_y)
{
x = init_x;
y = init_y;
}
But there are two special features about a
member functions body . . .
36
point Implementation
2
1
0
-1
-2
-2
-1
In the heading, the function's name is preceded by
the class name and :: - otherwise C++ won't realize
this is a classs member function.
void point::initialize(double init_x, double init_y)
{
x = init_x;
i it
y = init_y;
}
37
point Implementation
2
1
0
-1
-2
-2
-1
Within the body of the function, the classs member
variables and other member functions mayy all be
accessed.
void point::initialize(double init_x, double init_y)
{
x = init_x;
i it
y = init_y;
}
38
point Implementation
2
1
0
-1
-2
-2
-1
Within the body of the function, the classs member
variables and other member functions mayy all be
accessed.
But, whose member
variables
i bl are
void point::initialize(double init_x, double init_y)
these? Are they
{
p1 x
p1.x
x = init_x;
i it
y = init_y;
p1.y
}
p2.x
p
p2.y
?
39
point Implementation
2
1
0
-1
-2
-2
-1
Within the body of the function, the classs member
variables and other member functions mayy all be
accessed.
If we activate
p1.initialize:
void point::initialize(double init_x, double init_y)
p1.x
{
p1 y
p1.y
x = init_x;
i it
y = init_y;
}
40
point Implementation
2
1
0
-1
-2
-2
-1
Within the body of the function, the classs member
variables and other member functions mayy all be
accessed.
If we activate
p2.initialize:
void point::initialize(double init_x, double init_y)
p2.x
{
p2 y
p2.y
x = init_x;
i it
y = init_y;
}
41
point Implementation
2
1
0
-1
-2
-2
-1
H iis the
Here
h implementation
i l
i off the
h get_x
t member
b
function, which return the x coordinate:
double point::get_x() const
{
return x;
}
42
point Implementation
2
1
0
-1
-2
-2
-1
H iis the
Here
h implementation
i l
i off the
h get_x
t member
b
function, which return the x coordinate:
double point::get_x() const
{
return x;
}
Notice how this member function implementation
uses the member variable x of the point object.
43
point Implementation
2
1
0
-1
-2
-2
-1
M b ffunctions
Member
i
may activate
i
other
h member
b
functions
void point::origin()
{
x = 0.0;
y = 0.0;
}
N ti this
Notice
thi member
b function
f ti implementation
i l
t ti still
till
directly assign the member variables x and y.
44
point Implementation
2
1
0
-1
-2
-2
-1
M b ffunctions
Member
i
may activate
i
other
h member
b
functions
void point::origin()
{
i iti li (0 0 0.0);
initialize(0.0,
0 0)
}
Notice how this member function implementation
uses the member function initialize.
45
A Common Pattern
Often, one or more member functions will
place data in the member variables...
Initialize & shift
class
l
point
i t
{
public:
void initialize(double init_x, double init_y);
void
id shift(double
hift(d bl dx,
d double
d bl dy);
d )
double get_x( ) const;
double get_y( ) const;
private:
d bl x;
double
get_x & get_y
double y;
};
...so that other member functions may use that
data.
46
Summary of classes
Classes have member variables and member
functions. An object is a variable where the data
type is a class
class.
You should know how to declare a new class type,
how to implement its member functions, how to use
the class type.
Frequently, the member functions of an class type
place information in the member variables, or use
information that's already in the member variables.
Next
Ne t wee will
ill see more feat
features
res of OOP and classes
classes.
47
Assignments
Reading:
Chapter 2.32.3-2.5
Programming assignment 1 - Due June 12
Need all of chapter 2 to finish, but you can start doing it
now
Requirements and guidelines have been posted on the
course web site
C Installation
C++
I
ll i Guide
G id online
li
Linux Users: See the assignment #1 guidelines
Mac/Win Users: Check the course page
48
Break
49
Outline
A Review of C++ Classes (Lecture 2)
OOP,, ADTs and Classes
Class Definition, Implementation and Use
Constructors and Value Semantics
More on Classes (Lecture 3)
Namespace and Documentation
Classes and Parameters
Operator Overloading
50
Constructors: point Initialization
2
1
0
-1
-2
-2
The program
starts by
activating the
initialize
member function
f nction
for p1.
-1
#include <iostream.h>
#include <stdlib.h>
#include point
point.h"
h"
int main( )
{
point p1:
point p2;
p1 initialize(-1.0, 0.8);
First improvement: automatic initialization
without activating the initialize function
51
Constructors: point Initialization
2
1
0
-1
-2
-2
-1
We can provide a normal member function initialize
class p
point
{
public:
void initialize(double init_x,
init x double init_y);
init y);
void shift(double dx, double dy);
double get_x() const;
double get_y( ) const;
private:
double x;;
double y;
};
52
Constructors: point Initialization
2
1
0
-1
-2
-2
-1
Or use a constructor that is automatically called
class p
point
{
public:
point(double init_x,
init x double init_y);
init y);
void shift(double dx, double dy);
double get_x() const;
double get_y( ) const;
private:
-function name same as class name
double x;;
double y;
- no return type, even no void !
};
53
Constructors: Implementation
2
1
0
-1
-2
-2
-1
For the most part, the constructor is no different
than any other member functions.
void point::initialize(double init_x, double init_y)
{
x = init_x;
y = init_y;
}
We only need to replace initialize with point
54
Constructors: Implementation
2
1
0
-1
-2
-2
-1
For the most part, the constructor is no different
than any other member functions.
point::point(double init_x, double init_y)
{
x = init_x;
y = init_y;
}
But there are three special features about constructor . .
.
55
Constructors
Constructor
is a member function which
the name must be the same as the class name
automatically called whenever a variable of the
class is declared
arguments must be given after the variable
name (when declared in user file)
way
a to improve
impro e the initialize
initiali e function
f nction
by providing an initialization function that is
guaranteed to be called
56
Constructors: point Initialization
2
1
0
-1
-2
-2
Automatically
called when
declared.
Parameters after
the
h object
bj names
-1
#include <iostream.h>
#include <stdlib.h>
#include point
point.h"
h"
int main( )
{
point p1:
point p2;
p1 initialize(-1.0, 0.8);
First improvement: automatic initialization
without explicitly activating an initialize
function
57
Constructors: point Initialization
2
1
0
-1
-2
-2
Automatically
called when
declared.
Parameters after
the
h object
bj names
-1
#include <iostream.h>
#include <stdlib.h>
#include point
point.h"
h"
int main( )
{
point p1(-1.0, 0.8):
point p2(0.3, 0.6);
First improvement: automatic initialization
without explicitly activating an initialize
function
58
Default Constructors
2
1
0
-1
-2
-2
Automatically
called when
declared.
Parameters after
the
h object
bj names
-1
#include <iostream.h>
#include <stdlib.h>
#include point
point.h"
h"
int main( )
{
point p1(-1.0, 0.8):
point p2(0.3, 0.6);
Sometime we want to define an object with
no parameters
59
Default Constructors
2
1
0
-1
-2
-2
Automatically
called when
declared.
NO parameters
after
f the
h object
bj
name p2
-1
#include <iostream.h>
#include <stdlib.h>
#include point
point.h"
h"
int main( )
{
point p1(-1.0, 0.8);
point p2;
not even a pair of parentheses
60
Default Constructors
2
1
0
-1
-2
-2
-1
We could provide a second constructor with no parameters
class p
point
{
public:
point();
point(double init_x, double init_y);
private:
double x;
double y;
};
Implementation
point::point()
{
x = 0.0;
y = 0.0;
}
61
Constructors: Function Overloading
You may declare as many constructors as
you like one for each different way of
initializing an object
Each constructor must have a distinct
parameter list so that the compiler can tell
them part
Question: How many default constructor is
allowed?
62
Constructors: automatic default constructor
What happens if you write a class without any
constructors?
The compiler automatically creates a simple
default constructor
which only calls the default constructors for the
member variables that are objects of some other classes
Programming Tip :Always provide your own
constructors, and better with a default constructor
63
Value Semantics of a Class
Value semantics determines how values are
copied from one object to another
Consists of two operations in C++
The assignment operator
The copy constructor
Document the value semantics
When you implement an ADT, the document should
include a comment indicating that the value semantics
is safe to use.
64
Value Semantics: assignment operator
Automatic assignment operator
For a new class, C++ normally carries out assignment
by simply copying each variable from the object on the
right to that on the left
our new class point can use automatic assignment
operator
point p1(-1.0, 0.8), p2;
p2 = p1;
cout << p2.get_x() << << p2.get_y();
When automatic assignment fails
we will see examples in Lecture 4 (pointers and
dynamic arrays)
65
Value Semantics: copy constructor
A
copy constructor
is
a constructor with exactly one parameter
whose data type is the same as the constructors
class
is to initialize a new object as an exact copy of
an existing object
An example
point p1(-1.0, 0.8);
point p2 (p1);
cout << p2.get_x() << << p2.get_y();
66
Value Semantics: copy constructor
A
copy constructor
is
a constructor with exactly one parameter
whose data type is the same as the constructors
class
is to initialize a new object as an exact copy of
an existing object
An alternative syntax
point p1(-1.0, 0.8);
point p2 = p1;
cout << p2.get_x() << << p2.get_y();
67
Value Semantics: discussion
point p2 = p1; versus p2 = p1;
The assignment p2 = p1; merely copies p1 to the
already existing object p2 using the assignment
operator..
operator
The syntax point p2 = p1; looks like an assignment
statement, but actually a declaration that both declare a
new object, and calls the copy constructor to initialize
p2 as a copy of p1.
p2 will be the same iff the assignment operator
and the copy constructor do the same things
68
Copy Constructor: Implementation
You may write a copy constructor much like any
other constructor
Lecture 4 and later
Take advantage of a C++ feature
automatic copy constructor
similar to assignment, the automatic copy constructor
initializes a new object by merely copy all the member
variables from the existing object.
Automatic versions may fail!
69
Constructors etc
Constructors,
etc.
a summary
Constructor is a member function
define your own constructors (including a default)
automatic
i default
d f l constructor
inline member functions ( Ch 2.2)
Place a function definition inside the class definition
for time efficiency
value semantics of a class
assignment operators and copy constructor
automatic assignment op and copy constructor
70
Outline
A Review of C++ Classes (Lecture 2)
OOP,, ADTs and Classes
Class Definition, Implementation and Use
Constructors and Value Semantics
More on Classes (Lecture 3)
Namespace and Documentation
Classes and Parameters
Operator Overloading
71
Assignments
Reading:
Chapter 2.3
2.3--2.5
Programming assignment 1 - Due June 12
Need all of chapter 2 to finish,
finish but you can
start doing it now
Requirements and guidelines have been posted
on the course web site
72
Th first
The
fi t partt (p.3(p.3
( 3-47) off thi
this llecture
t
was adapted
d t d ffrom:
Presentation copyright 1997, Addison Wesley Longman
For use with Data Structures and Other Objects Using C++
by Michael Main and Walter Savitch.
Some artwork in the presentation is used with permission from Presentation Task Force
(copyright New Vision Technologies Inc.) and Corel Gallery Clipart Catalog (copyright
Corel Corporation, 3G Graphics Inc., Archive Arts, Cartesia Software, Image Club
Graphics Inc., One Mile Up Inc., TechPool Studios, Totem Graphics Inc.).
Students and instructors who use Data Structures and Other Objects Using C++ are
welcome to use this presentation however they see fit, so long as this copyright notice
remains intact.
THE END
73