1 CMSC 202 ADTs and C++ Classes. 2 Announcements Project 1 due Sunday February 25 th at midnight –...
-
Upload
eleanore-howard -
Category
Documents
-
view
219 -
download
0
description
Transcript of 1 CMSC 202 ADTs and C++ Classes. 2 Announcements Project 1 due Sunday February 25 th at midnight –...
1
CMSC 202
ADTs and C++ Classes
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
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
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
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);
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;
}
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.
8
Member Functions
// should have error checkingvoid Time::InitTime (int h, int m, int s){
hour = h;minute = m;second = s;
}
9
Another Member Function
void Time::PrintMilitary ( void ){
cout << (hour < 10 ? “0” : “”) << hour;cout << “:”cout << (minute < 10 ? “0” : “”) << minute;
}
10
Accessing Private Members
main ( ){ Time t2;
t2.hour = 11; // compiler error
}
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);
12
A Subtle Trap
• This is okayint Time::GetHour ( ){
return hour;}
• This is wrongint& Time::GetHour ( ){
return hour;}
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
14
Default Arguments with Constructors
class Time {public:
Time (int h = 0, int m = 0, int s = 0);. . .
}
• Any function can have default arguments
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
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;
}
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;
}
18
Member Initialization List
• An alternative for constructors
Time::Time (int h, int m, int s): hour(h), minute(m), second(s)
{// no code
}
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
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
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.
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
23
PrintTime with Accessors
void PrintTime (Time& t){
cout << t.GetHour( ) << “:”;cout << t.GetMinute( ) << “:”;cout << t.GetSecond( );
}
24
PrintTime as a friend
void PrintTime (const Time& t){
cout << t.hour << “:”;cout << t.minute << “:”;cout << t.second;
}
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
26
A philosophical Question
• Does the concept of friends violate the principles of OOP (i.e. data hiding)?
• Advantages of friends
• Disadvantages of friends
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
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