Classes in C++
Classes in C++
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
Class
Mechanism
functions
Support information hiding
Abstract
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
-1
class point
{
...
};
D t fforgett the
Dont
th
semicolon at the end
10
2
1
0
-1
-2
point Definition
-2
-1
class point
{
...
double x;
double y;
};
11
2
1
0
-1
-2
point Definition
-2
-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.
-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.
-1
class point
{
public:
...
private:
double x;
double y;
};
14
point Definition
2
1
0
-1
-2
-2
-1
15
point Definition
2
1
0
-1
-2
-2
-1
16
2
1
0
-1
-2
-2
-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
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
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
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
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
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
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
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
2
1
0
-1
-2
-2
-1
int main( ) {
point p1;
point p2;
p1 initialize(-1.0, 0.8);
26
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
-1
int main( )
{
point p1;
point p2;
p1 initialize(-1.0, 0.8);
28
2
1
0
-1
-2
A Quiz
-2
-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
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
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
34
point Implementation
2
1
0
-1
-2
-2
-1
35
point Implementation
2
1
0
-1
-2
-2
-1
36
point Implementation
2
1
0
-1
-2
-2
-1
point Implementation
2
1
0
-1
-2
-2
-1
point Implementation
2
1
0
-1
-2
-2
-1
?
39
point Implementation
2
1
0
-1
-2
-2
-1
point Implementation
2
1
0
-1
-2
-2
-1
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;
}
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)
}
A Common Pattern
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;
};
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
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
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);
2
1
0
-1
-2
-2
-1
52
2
1
0
-1
-2
-2
-1
53
Constructors: Implementation
2
1
0
-1
-2
-2
-1
Constructors: Implementation
2
1
0
-1
-2
-2
-1
55
Constructors
Constructor
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
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);
57
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);
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);
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;
Default Constructors
2
1
0
-1
-2
-2
-1
private:
double x;
double y;
};
Implementation
point::point()
{
x = 0.0;
y = 0.0;
}
61
62
63
p2 = p1;
cout << p2.get_x() << << p2.get_y();
copy constructor
is
An example
copy constructor
is
An alternative syntax
point p1(-1.0, 0.8);
point p2 = p1;
cout << p2.get_x() << << p2.get_y();
67
69
Constructors etc
Constructors,
etc.
a summary
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
72
Th first
The
fi t partt (p.3(p.3
( 3-47) off thi
this llecture
t
was adapted
d t d ffrom:
THE END
73