1.classes and Data Abstraction
1.classes and Data Abstraction
imranihsan.com
+ 2.1 Introduction
Information hiding
imranihsan.com
OOP 02 - Classes - I
5/14/2014
Structures
Structure Members
Structure tag
imranihsan.com
OOP 02 - Classes - I
Self-referential structure
struct definition
Examples:
Time timeObject;
Time timeArray[ 10 ];
Time *timePtr;
Time &timeRef = timeObject;
imranihsan.com
OOP 02 - Classes - I
5/14/2014
Arrow operator (->) for structure and class members via pointer
to object
imranihsan.com
OOP 02 - Classes - I
C-style structures
No interface
If implementation changes, all programs using that struct
must change accordingly
imranihsan.com
OOP 02 - Classes - I
5/14/2014
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//
// Create a structure, set its members, and print it.
#include <iostream>
using std::cout;
using std::endl;
#include <iomanip>
using std::setfill;
using std::setw;
// structure definition
struct Time {
int hour;
// 0-23 (24-hour clock format)
int minute;
// 0-59
int second;
// 0-59
}; // end struct Time
void printUniversal( const Time & );
void printStandard( const Time & );
// prototype
// prototype
OOP 02 - Classes - I
imranihsan.com
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
int main()
{
Time dinnerTime;
dinnerTime.hour = 18;
dinnerTime.minute = 30;
dinnerTime.second = 0;
} // end main
imranihsan.com
OOP 02 - Classes - I
5/14/2014
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
OOP 02 - Classes - I
Classes
Model objects
Attributes (data members)
Behaviors (member functions)
Defined using keyword class
Member functions
Methods
Invoked in response to messages
public:
Accessible wherever object of class in scope
private:
Accessible only to member functions of class
protected:
imranihsan.com
OOP 02 - Classes - I
5/14/2014
Constructor function
Several constructors
Function overloading
No return type
imranihsan.com
OOP 02 - Classes - I
class Time {
public:
Member access specifiers.
Time();
// constructor
void setTime( int, int, int ); // set hour, minute, second
void printUniversal();
// print universal-time format
void printStandard();
// print standard-time format
private:
int hour;
int minute;
int second;
imranihsan.com
OOP 02 - Classes - I
5/14/2014
Objects of class
sunset;
arrayOfTimes[ 5 ];
*pointerToTime;
&dinnerTime = sunset;
//
//
//
//
imranihsan.com
OOP 02 - Classes - I
imranihsan.com
OOP 02 - Classes - I
5/14/2014
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//
// Time class.
#include <iostream>
using std::cout;
using std::endl;
#include <iomanip>
using std::setfill;
using std::setw;
// Time abstract data type (ADT) definition
class Time {
public:
Time();
// constructor
void setTime( int, int, int ); // set hour, minute, second
void printUniversal();
// print universal-time format
void printStandard();
// print standard-time format
OOP 02 - Classes - I
imranihsan.com
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
private:
int hour;
int minute;
int second;
imranihsan.com
OOP 02 - Classes - I
5/14/2014
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
No arguments (implicitly
int main()
{
Time t;
OOP 02 - Classes - I
imranihsan.com
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// change time
imranihsan.com
OOP 02 - Classes - I
5/14/2014
imranihsan.com
OOP 02 - Classes - I
Simplify programming
Interfaces
Hide implementation
Software reuse
Composition (aggregation)
Class objects included as members of other classes
Inheritance
New classes derived from old
imranihsan.com
OOP 02 - Classes - I
10
5/14/2014
Class scope
File scope
Nonmember functions
imranihsan.com
OOP 02 - Classes - I
Function scope
imranihsan.com
OOP 02 - Classes - I
11
5/14/2014
imranihsan.com
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
OOP 02 - Classes - I
void print()
{
cout << x << endl;
}
}; // end class Count
imranihsan.com
OOP 02 - Classes - I
12
5/14/2014
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
int main()
operator for counter object.
{
Count counter;
// create counter object
Count *counterPtr = &counter; // create pointer to counter
Count &counterRef = counter; // create reference to counter
cout << "Assign 1 to x and print using the object's name: ";
counter.x = 1;
// assign 1 to data member x
counter.print();
// call member function print
cout << "Assign 2 to x and print using a reference: ";
counterRef.x = 2;
// assign 2 to data member x
counterRef.print(); // call member function print
cout << "Assign 3 to x and print using a pointer: ";
counterPtr->x = 3;
// assign 3 to data member x
counterPtr->print(); // call member function print
return 0;
} // end main
imranihsan.com
OOP 02 - Classes - I
Advantage
Easier to modify programs
Disadvantage
Header files
Portions of implementation
Inline member functions
Hints about other implementation
private members
Can hide more with proxy class
imranihsan.com
OOP 02 - Classes - I
13
5/14/2014
Header files
Source-code files
imranihsan.com
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
OOP 02 - Classes - I
// time1.h
multiple inclusions.
// Declaration of class Time.
// Member functions are defined in time1.cpp
// prevent multiple inclusions of header file
#ifndef TIME1_H
#define TIME1_H
// Time abstract data type definition
class Time {
Preprocessor directive
defines name TIME1_H.
Naming convention:
header file name with
underscore replacing period.
public:
Time();
// constructor
void setTime( int, int, int ); // set hour, minute, second
void printUniversal();
// print universal-time format
void printStandard();
// print standard-time format
private:
int hour;
int minute;
int second;
imranihsan.com
OOP 02 - Classes - I
14
5/14/2014
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// time1.cpp
// Member-function definitions for class Time.
#include <iostream>
using std::cout;
#include <iomanip>
using std::setfill;
using std::setw;
imranihsan.com
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
OOP 02 - Classes - I
imranihsan.com
OOP 02 - Classes - I
15
5/14/2014
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// program.cpp
// Program to test class Time.
// NOTE: This file must be compiled with time1.cpp.
#include <iostream>
using std::cout;
using std::endl;
// include definition of class Time from time1.h
#include "time1.h"
int main()
{
Time t;
// change time
OOP 02 - Classes - I
imranihsan.com
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
imranihsan.com
OOP 02 - Classes - I
16
5/14/2014
Access modes
private
Default access mode
Accessible to member functions and friends
public
Accessible to any function in program with handle to class
object
protected
Will be discussed in later lectures
imranihsan.com
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
OOP 02 - Classes - I
//
// Demonstrate errors resulting from attempts
// to access private class members.
#include <iostream>
using std::cout;
// include definition of class Time from time1.h
#include "time1.h"
int main()
{
Time t;
t.hour = 7;
OOP 02 - Classes - I
17
5/14/2014
Default private
Explicitly set to private, public, protected
Default public
Explicitly set to private, public, protected
imranihsan.com
OOP 02 - Classes - I
Access functions
public
Read/display data
Predicate functions
Check conditions
private
Support operation of public member functions
Not intended for direct client use
imranihsan.com
OOP 02 - Classes - I
18
5/14/2014
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// salesp.h
// SalesPerson class definition.
// Member functions defined in salesp.cpp.
#ifndef SALESP_H
#define SALESP_H
class SalesPerson {
public:
SalesPerson();
//
void getSalesFromUser();
//
void setSales( int, double ); //
void printAnnualSales();
//
private:
double totalAnnualSales();
double sales[ 12 ];
constructor
input sales from keyboard
set sales for a month
summarize and print sales
// utility function
// 12 monthly sales figures
OOP 02 - Classes - I
imranihsan.com
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// salesp.cpp
// Member functions for class SalesPerson.
#include <iostream>
using
using
using
using
std::cout;
std::cin;
std::endl;
std::fixed;
#include <iomanip>
using std::setprecision;
// include SalesPerson class definition from salesp.h
#include "salesp.h"
// initialize elements of array sales to 0.0
SalesPerson::SalesPerson()
{
for ( int i = 0; i < 12; i++ )
sales[ i ] = 0.0;
} // end SalesPerson constructor
imranihsan.com
OOP 02 - Classes - I
19
5/14/2014
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
OOP 02 - Classes - I
imranihsan.com
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
return total;
} // end function totalAnnualSales
imranihsan.com
OOP 02 - Classes - I
20
5/14/2014
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// program.cpp
// Demonstrating a utility function.
// Compile this program with salesp.cpp
// include SalesPerson class definition from salesp.h
#include "salesp.h"
int main()
{
SalesPerson s;
s.getSalesFromUser();
s.printAnnualSales();
return 0;
} // end main
OOP 02 - Classes - I
imranihsan.com
Enter
Enter
Enter
Enter
Enter
Enter
Enter
Enter
Enter
Enter
Enter
Enter
sales
sales
sales
sales
sales
sales
sales
sales
sales
sales
sales
sales
amount
amount
amount
amount
amount
amount
amount
amount
amount
amount
amount
amount
for
for
for
for
for
for
for
for
for
for
for
for
month
month
month
month
month
month
month
month
month
month
month
month
1: 5314.76
2: 4292.38
3: 4589.83
4: 5534.03
5: 4376.34
6: 5698.45
7: 4439.22
8: 5893.57
9: 4909.67
10: 5123.45
11: 4024.97
12: 5923.92
imranihsan.com
OOP 02 - Classes - I
21
5/14/2014
Constructors
Initializers
imranihsan.com
OOP 02 - Classes - I
+ 2.11
Constructors
imranihsan.com
OOP 02 - Classes - I
22
5/14/2014
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// time2.h
// Declaration of class Time.
// Member functions defined in time2.cpp.
// prevent multiple inclusions of header file
#ifndef TIME2_H
#define TIME2_H
imranihsan.com
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
OOP 02 - Classes - I
// time2.cpp
// Member-function definitions for class Time.
#include <iostream>
using std::cout;
#include <iomanip>
using std::setfill;
using std::setw;
// include definition of class Time from time2.h
#include "time2.h"
// Time constructor initializes each data member to zero;
// ensures all Time objects start in a consistent state
Time::Time( int hr, int min, int sec )
{
setTime( hr, min, sec ); // validate and set time
imranihsan.com
OOP 02 - Classes - I
23
5/14/2014
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
imranihsan.com
42
43
44
45
46
47
48
49
50
OOP 02 - Classes - I
imranihsan.com
OOP 02 - Classes - I
24
5/14/2014
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// code.cpp
// Demonstrating a default constructor for class Time.
#include <iostream>
using std::cout;
using std::endl;
// include definition of class Time from time2.h
#include "time2.h"
int main()
{
Time t1;
Time t2(
Time t3(
Time t4(
Time t5(
//
2 );
//
21, 34 );
//
12, 25, 42 ); //
27, 74, 99 ); //
OOP 02 - Classes - I
imranihsan.com
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
";
";
";
";
return 0;
} // end main
imranihsan.com
OOP 02 - Classes - I
25
5/14/2014
Constructed with:
all default arguments:
00:00:00
12:00:00 AM
hour specified; default minute and second:
02:00:00
2:00:00 AM
hour and minute specified; default second:
21:34:00
9:34:00 PM
hour, minute, and second specified:
12:25:42
12:25:42 PM
all invalid values specified:
00:00:00
12:00:00 AM
imranihsan.com
OOP 02 - Classes - I
+ 2.12 Destructors
Destructors
imranihsan.com
OOP 02 - Classes - I
26
5/14/2014
+ 2.13
imranihsan.com
OOP 02 - Classes - I
+ 2.13
imranihsan.com
OOP 02 - Classes - I
27
5/14/2014
+ 2.13
imranihsan.com
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
OOP 02 - Classes - I
// create.h
// Definition of class CreateAndDestroy.
// Member functions defined in create.cpp.
#ifndef CREATE_H
#define CREATE_H
class CreateAndDestroy {
public:
CreateAndDestroy( int, char * );
~CreateAndDestroy();
private:
int objectID;
char *message;
// constructor
// destructor
imranihsan.com
OOP 02 - Classes - I
28
5/14/2014
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// create.cpp
// Member-function definitions for class CreateAndDestroy
#include <iostream>
using std::cout;
using std::endl;
// include CreateAndDestroy class definition from create.h
#include "create.h"
// constructor
CreateAndDestroy::CreateAndDestroy(
int objectNumber, char *messagePtr )
{
objectID = objectNumber;
message = messagePtr;
cout << "Object " << objectID << "
<< message << endl;
Output message to
demonstrate timing of
constructor function calls.
constructor runs
"
OOP 02 - Classes - I
imranihsan.com
23
24
25
26
27
28
29
30
31
32
// destructor
CreateAndDestroy::~CreateAndDestroy()
{
// the following line is for pedagogic purposes only
cout << ( objectID == 1 || objectID == 6 ? "\n" : "" );
cout << "Object " << objectID << "
<< message << endl;
} // end ~CreateAndDestroy destructor
imranihsan.com
destructor runs
"
Output message to
demonstrate timing of
destructor function calls.
OOP 02 - Classes - I
29
5/14/2014
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// code.cpp
// Demonstrating the order in which constructors and
// destructors are called.
#include <iostream>
using std::cout;
using std::endl;
// include CreateAndDestroy class definition from create.h
#include "create.h"
void create( void );
// prototype
// global object
CreateAndDestroy first( 1, "(global before main)" );
int main()
Create local automatic object.
{
cout << "\nMAIN FUNCTION: EXECUTION BEGINS" << endl;
CreateAndDestroy second( 2, "(local automatic in main)" );
OOP 02 - Classes - I
imranihsan.com
create();
imranihsan.com
OOP 02 - Classes - I
30
5/14/2014
Object 1
constructor runs
(local
(local
(local
(local
Object 1
destructor runs
automatic
automatic
static in
static in
in main)
in main)
create)
main)
Global object
constructed before
main execution
and destroyed last.
+ 2.14
Set functions
OOP 02 - Classes - I
Get functions
Query functions
Control format of data returned
imranihsan.com
OOP 02 - Classes - I
31
5/14/2014
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// time3.h
// Declaration of class Time.
// Member functions defined in time3.cpp
// prevent multiple inclusions of header file
#ifndef TIME3_H
#define TIME3_H
class Time {
public:
Time( int = 0, int = 0, int = 0 );
// default constructor
Set functions.
// set functions
void setTime( int, int, int ); // set hour, minute, second
void setHour( int );
// set hour
void setMinute( int ); // set minute
void setSecond( int ); // set second
// get functions
int getHour();
int getMinute();
int getSecond();
Get functions.
// return hour
// return minute
// return second
OOP 02 - Classes - I
imranihsan.com
25
26
27
28
29
30
31
32
33
34
35
imranihsan.com
OOP 02 - Classes - I
32
5/14/2014
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// time3.cpp
// Member-function definitions for Time class.
#include <iostream>
using std::cout;
#include <iomanip>
using std::setfill;
using std::setw;
// include definition of class Time from time3.h
#include "time3.h"
// constructor function to initialize private data;
// calls member function setTime to set variables;
// default values are 0 (see class definition)
Time::Time( int hr, int min, int sec )
{
setTime( hr, min, sec );
} // end Time constructor
OOP 02 - Classes - I
imranihsan.com
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
imranihsan.com
OOP 02 - Classes - I
33
5/14/2014
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
OOP 02 - Classes - I
imranihsan.com
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
imranihsan.com
OOP 02 - Classes - I
34
5/14/2014
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// code.cpp
// Demonstrating the Time class set and get functions
#include <iostream>
using std::cout;
using std::endl;
// include definition of class Time from time3.h
#include "time3.h"
void incrementMinutes( Time &, const int );
int main()
{
Time t;
// prototype
OOP 02 - Classes - I
imranihsan.com
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// set time
// increment t's minute by 3
return 0;
} // end main
imranihsan.com
OOP 02 - Classes - I
35
5/14/2014
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
OOP 02 - Classes - I
imranihsan.com
imranihsan.com
OOP 02 - Classes - I
36
5/14/2014
+ 2.15
Assigning objects
imranihsan.com
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
OOP 02 - Classes - I
// progtam.cpp
// Demonstrating that class objects can be assigned
// to each other using default memberwise assignment.
#include <iostream>
using std::cout;
using std::endl;
// class Date definition
class Date {
public:
Date( int = 1, int = 1, int = 1990 ); // default constructor
void print();
private:
int month;
int day;
int year;
}; // end class Date
imranihsan.com
OOP 02 - Classes - I
37
5/14/2014
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
OOP 02 - Classes - I
imranihsan.com
44
45
46
47
48
49
50
51
52
53
54
55
56
57
Default memberwise
assignment assigns each
member of date1
individually to each
member of date2.
date1 = 7-4-2002
date2 = 1-1-1990
After default memberwise assignment, date2 = 7-4-2002
imranihsan.com
OOP 02 - Classes - I
38
5/14/2014
+ 2.16
Software Reusability
Software reusability
Class libraries
Well-defined
Carefully tested
Well-documented
Portable
Widely available
Speeds development of powerful, high-quality software
Rapid applications development (RAD)
Resulting problems
Cataloging schemes
Licensing schemes
Protection mechanisms
imranihsan.com
OOP 02 - Classes - I
39