C CIS 161 - PowerPoint PPT Presentation

1 / 40
About This Presentation
Title:

C CIS 161

Description:

Instantiated: Salesperson s; Object s is. instantiated. The constructor. Salesperson. is called and the. data member sales. is initialized. 7. SFG. Sal F. Gambino ... – PowerPoint PPT presentation

Number of Views:27
Avg rating:3.0/5.0
Slides: 41
Provided by: salga5
Category:

less

Transcript and Presenter's Notes

Title: C CIS 161


1
C CIS 161
Object Oriented Design
2
C
  • Procedural
  • programming
  • languages
  • the unit of programming
  • is a FUNCTION
  • action -oriented
  • Object
  • Oriented Design
  • languages
  • the unit of programming
  • is a CLASS
  • object are instantiated (created)
  • operations
  • member functions
  • data members

3
Accessible to all elements
Class example
Public data
Private data
NOT accessible outside the class boundary
Public functions
Private functions
Classes encapsulate data and functions together
4
Implementing a class
constructor
void setTime(int, int, int)
void printMilitaryTime() void
printStandard() int hour int min int
sec
class time
Member functions
public time()
interface
private
Data members
5
Constructor
  • A member function with the same name as the
    class
  • initializes the data members of a class object
  • is called automatically when an object of that
    class
  • is created or instantiated
  • cannot specify return types or return values
  • can contain default arguments

6
Example of using a constructor
class Salesperson public Salesperson() --
--- --- -- -- private double
sales12 ---- --- -- ---
The constructor Salesperson is called and the
data member sales is initialized
Implementation SalespersonSalesperson()
for( int c 0 c lt 12
c) salesc0.0
Object s is instantiated
Instantiated Salesperson s
7
Example of using a constructor
class Time public Time() void setTime(int,
int, int) -- --- --- -- -- private int
hr int min int sec
The constructor Time is called and the data
members are initialized
Implementation TimeTime() hr min sec 0

Object t is instantiated
Instantiated Time t
8
default arguments constructors
class Time public Time( int 0, int0,
int0) void setTime(int, int, int) -- ---
--- -- -- private int hr int min
int sec
Default values of zeros
0 0 0
Implementation TimeTime(int hr, int min, int
sec) setTime( hr, min, sec )
void TimesetTime(int h, int m, int s
) hr ( h gt 0 h lt 24 ) ? h
0 min ( m gt 0 m lt 60 ) ? m
0 sec ( s gt 0 s lt 60 ) ?
s 0
hr 0 min 0 sec 0
No arguments passed
Instantiated Time t1
9
default arguments constructors
2
class Time public Time( int 0, int0,
int0) void setTime(int, int, int) -- ---
--- -- -- private int hr int min
int sec
2 0 0
Implementation TimeTime(int hr, int min, int
sec ) setTime( hr, min, sec )
void TimesetTime(int h, int m, int s
) hr ( h gt 0 h lt 24 ) ? h
0 min ( m gt 0 m lt 60 ) ? m
0 sec ( s gt 0 s lt 60 ) ?
s 0
hr 2 min 0 sec 0
Instantiated Time t2(2)
10
default arguments constructors
21 34
class Time public Time( int 0, int0,
int0) void setTime(int, int, int) -- ---
--- -- -- private int hr int min
int sec
21 34 0
Implementation TimeTime(int hr, int min, int
sec ) setTime( hr, min, sec )
void TimesetTime(int h, int m, int s
) hr ( h gt 0 h lt 24 ) ? h
0 min ( m gt 0 m lt 60 ) ? m
0 sec ( s gt 0 s lt 60 ) ?
s 0
hr 21 min 34 sec 0
Instantiated Time t3( 21, 34 )
11
arguments constructors
25 14 42
class Time public Time( int 0, int0,
int0) void setTime(int, int, int) -- ---
--- -- -- private int hr int min
int sec
25 14 42
Implementation TimeTime(int hr, int min, int
sec ) setTime( hr, min, sec )
void TimesetTime(int h, int m, int s
) hr ( h gt 0 h lt 24 ) ? h
0 min ( m gt 0 m lt 60 ) ? m
0 sec ( s gt 0 s lt 60 ) ?
s 0
hr 0 min 14 sec 42
Instantiated Time t4( 25, 14, 42 )
12
arguments constructors
27 74 99
class Time public Time( int 0, int0,
int0) void setTime(int, int, int) -- ---
--- -- -- private int hr int min
int sec
27 74 99
Implementation TimeTime(int hr, int min, int
sec ) setTime( hr, min, sec )
void TimesetTime(int h, int m, int s
) hr ( h gt 0 h lt 24 ) ? h
0 min ( m gt 0 m lt 60 ) ? m
0 sec ( s gt 0 s lt 60 ) ?
s 0
hr 0 min 0 sec 0
Instantiated Time t5( 27, 74, 99 )
13
A simple class
The following example shows the syntax for
specifying a two-dimensional vector class
vector. A two dim vector has a pair of
coordinates(x,y), where x and y are some real
numbers. The addition of two vectors v1( x1,
y1 ) and v2 ( x2, y2 ) is defined as v1 v2
( x1 x2, y1 y2 ) v3.
Ex if v1 ( 3.0 , 2.0 ) v2
( 3.5 , 4.5 ) then v3 ( 3.0 3.5 , 2.0
4.5 ) or v3 ( 6.5 , 6.5 )
14
A simple class
  • Operations of class vector
  • constructor with NO arguments
  • constructor/s with arguments
  • set data
  • print data
  • add vectors
  • Data of class vector
  • x coordinate
  • y coordinate

15
class vector public vector()
vector(float x, float y ) void
setdata() void printdata() void
add_vectors() private float xCo, yCo
16
Vector header
// vecthead.h // --------- doc. For each member
functions ------ // ---------- pre or post
etc. ifndef vecthead_h define vecthead_h class
vector public vector() vector(float ,
float ) void setdata() void
printdata() void add_vector(vector
,vector) private float xCo, yCo endif
17
Vector implementation
include ltiostream.hgt include ltconio.hgt include
"vecthead.h" vectorvector()
vectorvector(float x, float y) xCo x
yCo y void vectorsetdata() cout
ltlt "\n Enter X-Coord. " cin gtgt xCo cout ltlt
"\n Enter Y-Coord. " cin gtgt yCo
18
Vector implementation
void vectorprintdata() cout ltlt "\n (X,Y)
ltlt "(" ltlt xCo ltlt "," ltlt yCo ltlt
")" void vectoradd_vector(vector a, vector
b) xCo a.xCo b.xCo yCo a.yCo
b.yCo
19
Vector driver
include ltiostream.hgt include ltconio.hgt include
"vecthead.h" // -------------------driver of
class vector void main() vector v1, v3
// objects v1,v3 are defined vector v2(
3.5, 4.5 ) // object v2 is instantiated v1.se
tdata() // calls member
function v1.printdata() // to get
data v2.printdata() v3.add_vector(v1,v2)
// add v1 and v2 giving v3.printdata() //
vectors v3 getch()
20
Main() vector v1, v2 vector v2( 3.5, 4.5 )
v1.setdata()
v1.printdata() v2.printdata() v3.add_vector(
v1,v2) v3.printdata()
Vector implementation
include ltiostream.hgt include ltconio.hgt include
"vecthead.h" vectorvector()
vectorvector(float x, float y) xCo x
yCo y void vectorsetdata() cout ltlt
"\n Enter X-Coord. " cin gtgt xCo cout ltlt "\n
Enter Y-Coord. " cin gtgt yCo
3.5 4.5
v2.xCo 3.5 v2.yCo 4.5
Object v1,v3 are defined
vector v1, v3 vector v2( 3.5, 4.5 )
Object v2 is instantiated
21
Main() v1.setdata()
v1.printdata() v2.printdata()
v3.add_vector(v1,v2) v3.printdata()
Vector implementation
include ltiostream.hgt include ltconio.hgt include
"vecthead.h" void vectorsetdata() cout ltlt
"\n Enter X-Coord. " cin gtgt xCo cout ltlt "\n
Enter Y-Coord. " cin gtgt yCo void
vectorprintdata() cout ltlt "\n (X,Y) " ltlt
"(" ltlt xCo ltlt "," ltlt yCo ltlt ")"
v1.xCo 3 v1.yCo 2
Call v1.setdata()
v2.xCo 3.5 v2.yCo 4.5
Output (X,Y) ( 3.0,2.0 )
Call v1.printdata()
v1.xCo 3.0 v1.yCo 2.0
(X,Y) ( 3.5,4.5 )
Call v2.printdata()
22
Vector implementation
Call v3.printdata()
include ltiostream.hgt include ltconio.hgt include
"vecthead.h" void vectorprintdata() cout ltlt
"\n (X,Y) " ltlt "(" ltlt xCo ltlt "," ltlt yCo ltlt ")"
void vectoradd_vector( vector a, vector
b) xCo a.xCo b.xCo yCo a.yCo
b.yCo
Main() ------ v3.add_vector(v1,v2)
v3.printdata()
Output (X,Y) (3.0,2.0)
(X,Y) (3.5,4.5) (X,Y) (6.5,
6.5)
v1(3.0, 2.0)
v2(3.5, 4.5)
v3
v3.xCo 3.0 3.5 6.5 v3.yCo 2.0 4.5
6.5
Call v3.add_vector(v1,v2)
v2.xCo 3.5 v2.yCo 4.5
v1.xCo 3.0 v1.yCo 2.0
23
Rectangle problem
Create a Class rectangle. The class has
attributes LENGTH and WIDTH, each of which
defaults to 1. It has member functions that
calculate the PERIMETER and the AREA of the
rectangle and a PRINTALL function to display the
length, width, area, and perimeter. It has SET
and GET functions for both LENGTH and WIDTH. The
set functions should VERIFY that length and width
are each floating point numbers larger than 0.0
and less than 20.0 Create a project file, compile
and execute.
24
A simple class
  • Operations of class rectangle
  • constructor with NO arguments
  • constructor/s with arguments
  • setLen
  • setWid
  • computeArea
  • printAll

Input l , w output a , p process a l
w p 2l 2w decision 0.0 lt l,w lt
20.0
  • getLen
  • getWid
  • computePeri
  • Data of class rectangle
  • len, wid
  • area, peri

25
//rech.h ifndef rech_H define rech_H class
rectangle public rectangle(float 1, float
1) void setLen(float) void
setWid(float) void computeArea() void
computePeri() void printAll() float
getLen() float getWid() private float len,
wid, area, peri endif
rectangle header
26
rectangle implementation
// recimpl.cpp includeltiostream.hgt
include "rech.h rectanglerectangle(float l,
float w) setLen(l) setWid(w) void
rectanglesetLen(float l) len (l gt
0.0 l lt 20.0) ? l1 void
rectanglesetWid(float w) wid (w gt 0.0
w lt 20.0)? w1
27
rectangle implementation
void rectanglecomputeArea() area len
wid void rectanglecomputePeri()
peri 2 len 2 wid void
rectangleprintAll() cout ltlt "\nThe Area
of the Rectangle is " ltlt area cout
ltlt \n The Perimeter of the Rectangle is " ltlt
peri
28
rectangle implementation
float rectanglegetLen() return
len float rectanglegetWid()
return wid
29
rectangle driver
includeltiostream.hgt //recdriv.cpp include
"rech.h main() rectangle t
t.computeArea() t.computePeri()
t.printAll() cout ltlt "\n (" ltltt.getLen()
ltlt ", " ltlt t.getWid() ltlt " ) \n"
Output The Area of the Rectangle is 1
The Perimeter of the Rectangle is 4 (1, 1 )
30
rectangle driver
includeltiostream.hgt //recdriv.cpp include
"rech.h main() rectangle t1(9,5)
t1.computeArea() t1.computePeri()
t1.printAll() cout ltlt "\n (" ltltt1.getLen()
ltlt ", " ltlt t1.getWid() ltlt " )\n"
Output The Area of the Rectangle is
45 The Perimeter of the Rectangle is 28
( 9, 5 )
31
rectangle driver
includeltiostream.hgt //recdriv.cpp include
"rech.h main() rectangle t2 ( 3,7)
t2.computeArea() t2.computePeri()
t2.printall() cout ltlt "\n (" ltltt2.getLen()
ltlt ", " ltlt t2.getWid() ltlt " )\n"
Output The Area of the Rectangle is
21 The Perimeter of the Rectangle is 20
( 3, 7 )
32
includeltiostream.hgt //recdriv.cpp include
"rech.h main() rectangle t3(28,99)
cout ltlt "\n InvalidParameters"
t3.computeArea() t3.computePeri()
t3.printAll() cout ltlt "\n (" ltltt3.getLen()
ltlt ", " ltlt t3.getWid() ltlt " )"
rectangle driver
Output InvalidParameters" The
Area of the Rectangle is 1 The
Perimeter of the Rectangle is 4 ( 1, 1 )
33
//rech.h ifndef rech_H define rech_H class
rectangle public rectangle(float 1,
float 1) void setLen(float) void
setWid(float) void printAll() float
getArea() float getPeri() float
getLen() float getWid() private float len,
wid, area, peri endif
rectangle header 2
34
rectangle implementation 2
include "rech.h
float rectanglegetArea() area len
wid return area
float rectanglegetPeri() peri
2 len 2 wid return peri
void rectangleprintAll() cout ltlt
"\nThe Area of the Rect.. " ltlt getArea
cout ltlt \n The Perim. of the Rect.. " ltlt
getPeri
35
rectangle driver 2
includeltiostream.hgt //recdriv.cpp include
"rech.h main() rectangle t2 ( 3,7)
t2.printall()
Output The Area of the Rectangle is
21 The Perimeter of the Rectangle is 20
( 3, 7 )
36
Typical constructor with no arguments
default is 0 time() hrs 0 min
0 sec 0
default is 0 vector() xCo 0 yCo
0
default is 1 rectangle() length 1 width
1 area 1 perim 4
Main driver time t
Main driver vector v
Main driver rectangle r
37
lttypegt ltclassgt getfunctionName() some
process return functionName
float rectanglegetArea() area len
width return area
Typical get function
Driver pgm rectangle r cout ltlt The area is
ltlt r.getArea()
38
Typical set function or input data
  • There are many ways of entering data
  • by using CIN, input from the keyboard
  • by using a random number generator function
  • by using a constant or an assign operator

Rectangle r r.setLength()
Void rectanglesetLength() // 0lt
lengthlt20 length rand()19 1
Private float length
include lttime.hgt
39
Typical set function or input data
  • There are many ways of entering data
  • by using CIN, input from the keyboard
  • by using a random number generator function
  • by using a constant or an assign operator

Rectangle r r.setLength()
Void rectanglesetLength() float len cout ltlt
\n enter length cinltlt len length (len gt
0.0 len lt 20.0) ? len 1
Private float length
40
Typical set function or input data
  • There are many ways of entering data
  • by using CIN, input from the keyboard
  • by using a random number generator function
  • by using a constant or an assign operator

Rectangle r r.setLength(15.0)
Void rectanglesetLength(float len) length
(len gt 0.0 len lt 20.0) ? len 1
Private float length
Write a Comment
User Comments (0)
About PowerShow.com