1 CMSC 202 ADTs and C++ Classes. 2 Announcements Project 1 due Sunday February 25 th at midnight –...

28
1 CMSC 202 ADTs and C++ Classes

description

3 OOP and Classes OOP encapsulates data (characteristics) and functions (behaviors) into packages called classes. Classes support information hiding –Users of a class know how to communicate with the class using well defined interfaces, they do not know how the classes are implemented – implementation details are hidden within each class

Transcript of 1 CMSC 202 ADTs and C++ Classes. 2 Announcements Project 1 due Sunday February 25 th at midnight –...

Page 1: 1 CMSC 202 ADTs and C++ Classes. 2 Announcements Project 1 due Sunday February 25 th at midnight – don’t be late! Notes and clarifications for Project.

1

CMSC 202

ADTs and C++ Classes

Page 2: 1 CMSC 202 ADTs and C++ Classes. 2 Announcements Project 1 due Sunday February 25 th at midnight – don’t be late! Notes and clarifications for Project.

2

Announcements

• Project 1 due Sunday February 25th at midnight – don’t be late!

• Notes and clarifications for Project 1• HelpCenter (ECS 332) is open for business• Quiz #2 this week• Exam 1 – Next Wed/Thurs

– Up to and including Monday/Tuesday Lecture– Chapters 2 – 8 and 14 of the text

Page 3: 1 CMSC 202 ADTs and C++ Classes. 2 Announcements Project 1 due Sunday February 25 th at midnight – don’t be late! Notes and clarifications for Project.

3

OOP and Classes

• OOP encapsulates data (characteristics) and functions (behaviors) into packages called classes.

• Classes support information hiding– Users of a class know how to communicate

with the class using well defined interfaces, they do not know how the classes are implemented – implementation details are hidden within each class

Page 4: 1 CMSC 202 ADTs and C++ Classes. 2 Announcements Project 1 due Sunday February 25 th at midnight – don’t be late! Notes and clarifications for Project.

4

User Defined Types

• In C, software components are functions• In OOP (C++), software components are

user defined data types called classes.• Each class contains data as well as the

functions to manipulate the data

• An object is the instantiation of a class

Page 5: 1 CMSC 202 ADTs and C++ Classes. 2 Announcements Project 1 due Sunday February 25 th at midnight – don’t be late! Notes and clarifications for Project.

5

The TIME ADT in Ctypedef struct time

{int hour;int minute;int second;} TIME;

void InitTime (int h, int m, int s);void PrintMilitary (TIME *tp);void PrintStd (TIME *tp);void Increment (TIME *tp);

Page 6: 1 CMSC 202 ADTs and C++ Classes. 2 Announcements Project 1 due Sunday February 25 th at midnight – don’t be late! Notes and clarifications for Project.

6

The TIME ADT in C++class Time {

public:Time ( );void InitTime (int h, int m, int s);void PrintMilitary ( void );void PrintStd ( void );void Increment ( void ); private:int hour;int minute;int second;

}

Page 7: 1 CMSC 202 ADTs and C++ Classes. 2 Announcements Project 1 due Sunday February 25 th at midnight – don’t be late! Notes and clarifications for Project.

7

Terminology

• This is a class, not an object.• private and public are “member access

specifications”. (protected later)• Time ( ); is a special function called a constructor.

Same name as the class.• Class functions also known as member functions

or methods.• In this course, ALL DATA MEMBERS OF ALL

CLASSES WILL BE PRIVATE.

Page 8: 1 CMSC 202 ADTs and C++ Classes. 2 Announcements Project 1 due Sunday February 25 th at midnight – don’t be late! Notes and clarifications for Project.

8

Member Functions

// should have error checkingvoid Time::InitTime (int h, int m, int s){

hour = h;minute = m;second = s;

}

Page 9: 1 CMSC 202 ADTs and C++ Classes. 2 Announcements Project 1 due Sunday February 25 th at midnight – don’t be late! Notes and clarifications for Project.

9

Another Member Function

void Time::PrintMilitary ( void ){

cout << (hour < 10 ? “0” : “”) << hour;cout << “:”cout << (minute < 10 ? “0” : “”) << minute;

}

Page 10: 1 CMSC 202 ADTs and C++ Classes. 2 Announcements Project 1 due Sunday February 25 th at midnight – don’t be late! Notes and clarifications for Project.

10

Accessing Private Members

main ( ){ Time t2;

t2.hour = 11; // compiler error

}

Page 11: 1 CMSC 202 ADTs and C++ Classes. 2 Announcements Project 1 due Sunday February 25 th at midnight – don’t be late! Notes and clarifications for Project.

11

Manipulating Private Data

• Public “Accessor” functions– int GetMinute ( void );– int GetHour ( void );– int GetSecond ( void );

• Public “Mutator” functions– void SetMinute (int m);– void SetHour (int h);– void SetSecond (int s);

Page 12: 1 CMSC 202 ADTs and C++ Classes. 2 Announcements Project 1 due Sunday February 25 th at midnight – don’t be late! Notes and clarifications for Project.

12

A Subtle Trap

• This is okayint Time::GetHour ( ){

return hour;}

• This is wrongint& Time::GetHour ( ){

return hour;}

Page 13: 1 CMSC 202 ADTs and C++ Classes. 2 Announcements Project 1 due Sunday February 25 th at midnight – don’t be late! Notes and clarifications for Project.

13

The constructor

• A special member function which is called automatically whenever an object is instantiated

Time::Time ( ) {hour = minute = second = 0;

}• NO RETURN TYPE (not even “void”)• There can be more than one• Compiler provides a default constructor

Page 14: 1 CMSC 202 ADTs and C++ Classes. 2 Announcements Project 1 due Sunday February 25 th at midnight – don’t be late! Notes and clarifications for Project.

14

Default Arguments with Constructors

class Time {public:

Time (int h = 0, int m = 0, int s = 0);. . .

}

• Any function can have default arguments

Page 15: 1 CMSC 202 ADTs and C++ Classes. 2 Announcements Project 1 due Sunday February 25 th at midnight – don’t be late! Notes and clarifications for Project.

15

Using the Time Constructor

• Time t1 (3, 5, 9);

• Time t2 (4, 7); // second = 0

• Time t3 ( 5); // minute = second = 0

• Time t4; // all are 0

Page 16: 1 CMSC 202 ADTs and C++ Classes. 2 Announcements Project 1 due Sunday February 25 th at midnight – don’t be late! Notes and clarifications for Project.

16

int main ( ) {

Time t1; // instantiates a Time object – constructor

cout << “Initial Military Time is :” ; t1.PrintMilitary ( ); cout << endl;

t1.InitTime (13, 9, 10 ); cout << “New Military Time is:” t1.PrintMilitary ( ); cout << endl;

t1.Increment ( ); cout << “New Military Time is:” t1.PrintMilitary ( ); cout << endl;

}

Page 17: 1 CMSC 202 ADTs and C++ Classes. 2 Announcements Project 1 due Sunday February 25 th at midnight – don’t be late! Notes and clarifications for Project.

17

// main( ) with pointers

int main ( ) {

Time t1; // instantiates a Time object – constructor Time *tp = &t1; // pointer to Time object

cout << “Initial Military Time is :” ; tp->PrintMilitary ( ); cout << endl;

tp->InitTime (13, 9, 10 ); tp->Increment ( ); cout << “New Military Time is:” tp->PrintMilitary ( ); cout << endl;

}

Page 18: 1 CMSC 202 ADTs and C++ Classes. 2 Announcements Project 1 due Sunday February 25 th at midnight – don’t be late! Notes and clarifications for Project.

18

Member Initialization List

• An alternative for constructors

Time::Time (int h, int m, int s): hour(h), minute(m), second(s)

{// no code

}

Page 19: 1 CMSC 202 ADTs and C++ Classes. 2 Announcements Project 1 due Sunday February 25 th at midnight – don’t be late! Notes and clarifications for Project.

19

Destructor

• A special member function that is automatically called when an object goes out of scope to “deallocate” the object

• Special Naming -- Time::~Time ( );• NO RETURN TYPE• CAN BE ONLY ONE• Compiler provides a default destructor

– Not always sufficient

Page 20: 1 CMSC 202 ADTs and C++ Classes. 2 Announcements Project 1 due Sunday February 25 th at midnight – don’t be late! Notes and clarifications for Project.

20

Default Assignment

• The compiler provides a default assignment operator for every class Time t1, t2;

t1 = t2; // assignment• A member-by-member copy

• Not always sufficient

Page 21: 1 CMSC 202 ADTs and C++ Classes. 2 Announcements Project 1 due Sunday February 25 th at midnight – don’t be late! Notes and clarifications for Project.

21

friend functions

• A friend function of a class is a non-member function yet still has the right to access the right to access the private (and protected) members of the class.

Page 22: 1 CMSC 202 ADTs and C++ Classes. 2 Announcements Project 1 due Sunday February 25 th at midnight – don’t be late! Notes and clarifications for Project.

22

A friendly example

• Within the Time class, we can allow the PrintTime( ) function to be our friend

friend void PrintTime (const Time &);

• Now PrintTime doesn’t need to use the accessors

Page 23: 1 CMSC 202 ADTs and C++ Classes. 2 Announcements Project 1 due Sunday February 25 th at midnight – don’t be late! Notes and clarifications for Project.

23

PrintTime with Accessors

void PrintTime (Time& t){

cout << t.GetHour( ) << “:”;cout << t.GetMinute( ) << “:”;cout << t.GetSecond( );

}

Page 24: 1 CMSC 202 ADTs and C++ Classes. 2 Announcements Project 1 due Sunday February 25 th at midnight – don’t be late! Notes and clarifications for Project.

24

PrintTime as a friend

void PrintTime (const Time& t){

cout << t.hour << “:”;cout << t.minute << “:”;cout << t.second;

}

Page 25: 1 CMSC 202 ADTs and C++ Classes. 2 Announcements Project 1 due Sunday February 25 th at midnight – don’t be late! Notes and clarifications for Project.

25

Classes can be friends too

• We can allow all the functions of a class to be our friend.

• AlarmClock can let Time be it’s friend by declaring

friend class Time;within its class definition

Page 26: 1 CMSC 202 ADTs and C++ Classes. 2 Announcements Project 1 due Sunday February 25 th at midnight – don’t be late! Notes and clarifications for Project.

26

A philosophical Question

• Does the concept of friends violate the principles of OOP (i.e. data hiding)?

• Advantages of friends

• Disadvantages of friends

Page 27: 1 CMSC 202 ADTs and C++ Classes. 2 Announcements Project 1 due Sunday February 25 th at midnight – don’t be late! Notes and clarifications for Project.

27

Separating Interface and Implementation

• Interface – the header file (Time.H)– Given to the user– Substantial documentation– NO CODE– public section first

• Implementation – member function code (Time.C)– Only for the programmer– Programmer-oriented documentation

Page 28: 1 CMSC 202 ADTs and C++ Classes. 2 Announcements Project 1 due Sunday February 25 th at midnight – don’t be late! Notes and clarifications for Project.

28

Good Programming Practices

1. Use private and public just once in a class2. List public first3. ALL DATA is private4. Interface in .H and implementation in .C5. Don’t return a (non-const) reference to

private data