Public vs. Private and Protected Class Members - PowerPoint PPT Presentation

1 / 27
About This Presentation
Title:

Public vs. Private and Protected Class Members

Description:

sleepless.initialize('Sleepless in Seattle', 'Tom Hanks', 'Meg Ryan' ... movie sleepless('Sleepless in Seattle', 'Tom Hanks', 'Meg Ryan'); fugitive.show_movie ... – PowerPoint PPT presentation

Number of Views:230
Avg rating:3.0/5.0
Slides: 28
Provided by: chen127
Category:

less

Transcript and Presenter's Notes

Title: Public vs. Private and Protected Class Members


1
Public vs. Private and Protected Class Members
  • Professor Jason Chen
  • School of Business Administration
  • Gonzaga University
  • Spokane, WA 99258
  • chen_at_gonzaga.edu

2
TECHNOLOGY SHIFT
  • Right idea, at the right time

From customized software products to
  • Commercially viable and widely available
  • Last for years or decades

reusable software modules to
Highly configurable software products without
additional intervention from a programmer.
Change focus from information access to
Emerging Technology
value creation.
N
3
What is a Class?
  • A class is a user-defined type.
  • It is also a generic specification, or template,
    for producing an object at an abstract,
    conceptual level.
  • When classes that do not create instances of
    themselves, the classes are called abstract.

4
Why Classes?
  • The classes exist so that a programmer can
    extract and collect common behavior among classes
    in one location.
  • An abstract class specifies its default behavior
    so that subclass method will refine, augment, or
    overload its implementation.
  • A concrete subclass adds new abilities to the
    behaviors inherited from its abstract superclass
    when the subclasss function and purpose require.

5
Public vs. Private Class Members
  • When you declare a class, you can define members
    as public or private (or protected).
  • Public class members are available throughout
    your program using the object name and the dot
    (class.member) or indirection
  • (class_ptr-member) operators.

6
Public vs. Private Class Members (cont.)
  • Private class members, on the other hand, can
    only be accessed using class member functions
    (i.e., functions declared in the base class).
    Using private class members, your programs can
    better control the values assigned to class
    members and how those values are used. By
    default, all class members are private. Once you
    include a public label within your program,
    however, you can specify public members. If you
    later want to declare private members, you must
    use the private label.

7
Public vs. Private Class Members (cont.)
  • Protected class members are accessible within the
    base or derived class member functions, but not
    outside of such functions.

8
Information Hiding
  • Information hiding is the process of designing
    functions or classes as black box. In other
    words, to use a function or class, a programmer
    does not need to know the boxs inner workings,
    since it brings with it all of its methods, but
    rather, simply the operation the box performs and
    how to interface with the box.

9
  • Within your C programs, private class members
    support information hiding.

10
First Class Example Employees
TYPES OF EMPLOYEES
Salaried Hourly Permanent
Hourly Temporary
name name
name
home phone home phone
home phone
office phone office phone
office phone
salary level wage
wage
bonus level
reports to reports to
reports to
assistant

YOUR TASK Draw a UML diagram that best describes
the relationship of these three types of employees
.
11
employee class
12
How to construct objects?
movie fugitive, sleepless
13
movie class
void movieshow_movie(void) cout name " movie_name, char first, char second)
strcpy(name, movie_name) strcpy(first_star,
first) strcpy(second_star, second)
See movies.cpp
include include
include using namespace std class
movie public char name64 char
first_star64 char second_star64 void
show_movie(void) void initialize(char name,
char first, char second)
int main(void) movie fugitive, sleepless
fugitive.initialize("The Fugitive", "Harrison
Ford", "Tommy Lee Jones") sleepless.initialize
("Sleepless in Seattle", "Tom Hanks", "Meg
Ryan") fugitive.show_movie()
sleepless.show_movie() return 0
14
Figure Conventional vs. Object - Oriented
Conventional
Object-Oriented
Decomposed by
Functions/process
Objects/concepts
Data type e.g., int num_of_employees
Class type class employee ? employee
full-time, part-time
Types
One variable contains one value. e.g. number_of
_employees 20
Content
One class contains many objects One object has
many attributes, operations
15
Compare two files movies.cpp and public.cpp
//file name movies.cpp int main(void)
movie fugitive, sleepless fugitive.initialize
("The Fugitive", "Harrison Ford", "Tommy Lee
Jones") sleepless.initialize("Sleepless in
Seattle", "Tom Hanks", "Meg Ryan")
fugitive.show_movie() sleepless.show_movie()
return 0
Question What will happen if we declare all
three attributes to be private?
//file name public.cpp int main(void)
movie fugitive, sleepless fugitive.initialize
("The Fugitive", "Harrison Ford", "Tommy Lee
Jones") sleepless.initialize("Sleepless in
Seattle", "Tom Hanks", "Meg Ryan") cout "The last two movies I've watched are " fugitive.name fugitive.first_star return 0
16
//File name private_w_public.cpp include
include include
using namespace std class movie
public void show_movie(void) void
initialize(char name, char first, char
second) private char name64 char
first_star64 char second_star64 void
movieshow_movie(void) cout name " movie_name, char first, char second)
strcpy(name, movie_name) strcpy(first_star,
first) strcpy(second_star, second)
int main(void) movie fugitive,
sleepless fugitive.initialize("The
Fugitive", "Harrison Ford", "Tommy Lee Jones")
sleepless.initialize("Sleepless in Seattle",
"Tom Hanks", "Meg Ryan") cout two movies I've watched are " cout was great!" //sleepless.show_movie() return 0
Question What will happen if we declare all
three attributes to be private?
Answer The program will NOT be working. Why?
see private_w_public.cpp)
17
void main(void) movie fugitive,
sleepless fugitive.initialize("The
Fugitive", "Harrison Ford", "Tommy Lee Jones")
sleepless.initialize("Sleepless in Seattle",
"Tom Hanks", "Meg Ryan") fugitive.show_movie
() sleepless.show_movie() getch()
//File name private.cpp include
include include
class movie public void
show_movie(void) void initialize(char name,
char first, char second) private char
name64 char first_star64 char
second_star64 void movieshow_movie(void)
cout cout second_star movieinitialize(char movie_name, char first,
char second) strcpy(name, movie_name)
strcpy(first_star, first) strcpy(second_star,
second)
Homework What should be changed if a string
type is used in the program?
18
int main(void) movie fugitive,
sleepless fugitive.initialize("The
Fugitive", "Harrison Ford", "Tommy Lee Jones")
sleepless.initialize("Sleepless in Seattle",
"Tom Hanks", "Meg Ryan") fugitive.show_movie(
) sleepless.show_movie() return 0
//file name movies_string.cpp //use of string
type include include
using namespace std class movie public
string name string first_star string
second_star void show_movie(void) void
initialize(string name,string first,string
second) void movieshow_movie(void)
cout second_star movieinitialize(string movie_name,string
first,string second) name movie_name
first_star first second_star second
19
ConstructorConstruct objects automatically
show_move()
20
//File name constructor.cpp include
include using namespace
std class movie public void
show_movie(void) movie(char name, char
first, char second) private char
name64 char first_star64 char
second_star64 void movieshow_movie(void)
cout cout second_star name, char first_star, char second_star)
strcpy(moviename, name) strcpy(moviefirst
_star, first_star) strcpy(moviesecond_star,
second_star)
int main(void) movie fugitive("The
Fugitive", "Harrison Ford", "Tommy Lee Jones")
movie sleepless("Sleepless in Seattle", "Tom
Hanks", "Meg Ryan") fugitive.show_movie()
sleepless.show_movie() return 0
See construct_string.cpp
21
DestructorDestruct objects automatically
22
//File name destructor.cpp include
include include
using namespace std class movie
public void show_movie(void) movie(char
name, char first, char second)
movie(void) private char name64 char
first_star64 char second_star64 void
movieshow_movie(void) cout name " first_star, char second_star)
strcpy(moviename, name) strcpy(moviefirst_
star, first_star) strcpy(moviesecond_star,
second_star)
moviemovie(void) cout destructor for " int main(void) movie fugitive("The
Fugitive", "Harrison Ford", "Tommy Lee Jones")
movie sleepless("Sleepless in Seattle", "Tom
Hanks", "Meg Ryan") fugitive.show_movie()
sleepless.show_movie() return 0
23
INLINE FUNCTION
//File name inline.cpp include
include using namespace
std class movie public char name64
char first_star64 char second_star64
void show_movie(void) cout name " " movie_name, char first, char second)
strcpy(name, movie_name)
strcpy(first_star, first)
strcpy(second_star, second)
void main(void) movie fugitive,
sleepless fugitive.initialize("The
Fugitive", "Harrison Ford", "Tommy Lee Jones")
sleepless.initialize("Sleepless in Seattle",
"Tom Hanks", "Meg Ryan") cout two movies I've watched are " cout was great!" 24
Inline vs. Out-of-line Functions
Sharing Member Function Code When you declare
class member functions, you have two choices. You
can declare the function statements as inline
statements (inside of the class declaration
itself), or you can declare the statements
out-of-line. The advantage of using out-of-line
function declarations is that each object you
create shares a copy of the program code (rather
than each object you create gets its own unique
copy of the function code). For example, if you
create 100 objects, only one function is created,
which objects share. In this way, your programs
memory requirements is reduced.
Criteria of choosing inline or out-of-line
functions 1. If the function is larger or many
objects will be created for that class, then
out-of-line function should be used. (Reduce the
size of the program). 2. If the function might
be called very often, then inline function should
be employed. (It saves CPU time for calling
function and returns values however, the size of
the program my be bigger).
25
HW4
  • 4b
  • use of Rational Rose Case Tool
  • try to generate the code and then modify 4a to
    this assignment
  • Email me the following four (4) files
  • movie_L_F.mdl (UML file)
  • mymoviedr_L_F.cpp
  • movie_L_F.cpp
  • movie_L_F.h
  • Due on March. 22, Monday midnight.
  • 4a
  • use data type of char (i.e., char title80)
  • use cin.getline(title,80)

26
include include
include class movie public char
name64 char first_star64 char
second_star64 void show_movie(void)
cout cout second_star initialize(char movie_name, char first, char
second) strcpy(name, movie_name)
strcpy(first_star, first)
strcpy(second_star, second)
void main(void) movie fugitive,
sleepless fugitive.initialize("The
Fugitive", "Harrison Ford", "Tommy Lee Jones")
sleepless.initialize("Sleepless in Seattle",
"Tom Hanks", "Meg Ryan") cout two movies I've watched are " cout was great!"
27
movie class
See movies.cpp
Write a Comment
User Comments (0)
About PowerShow.com