Starting Out With C Tony Gaddis - PowerPoint PPT Presentation

1 / 22
About This Presentation
Title:

Starting Out With C Tony Gaddis

Description:

Procedural programming: Centered on procedures, or actions that take place in a program ... item2.setInfo ( 'Pliers', 25 ); Uses default constructor ... – PowerPoint PPT presentation

Number of Views:398
Avg rating:3.0/5.0
Slides: 23
Provided by: sueell5
Category:
Tags: gaddis | out | pliers | starting | tony

less

Transcript and Presenter's Notes

Title: Starting Out With C Tony Gaddis


1
Starting Out With CTony Gaddis
Chapter 13 Introduction to Classes
2
Methods of Writing Software
Procedural programming Centered on procedures,
or actions that take place in a program Data and
functions are separate
Object-oriented programming Centered around
objects, created from abstract data types Data
and functions are packaged together
Rectangle objects have associated data and
functions as a single unit
float width float length float area void
setData ( float w, float l ) void calcArea (
) float getWidth ( ) float getLength ( ) float
getArea ( )
Member variables
Member Functions
3
Two Programming Paradigms
Structural (Procedural) Object-Oriented
PROGRAM PROGRAM
4
Data Hiding
Objects have associated data and functions
Objects restrict other parts of the program from
accessing their member variables called data
hiding
Data hiding allows the creation of objects whose
critical data is protected from corruption
Other programs
Object
Public Interface
Internal data of object
Private data Private functions
Inner workings of object
5
Introduction to Classes
A C class is used to create objects defined by
the programmer, consisting of variables and
functions
Define the class Rectangle Tell the compiler
what the class is made of
class Rectangle private float width
float length float area public
void setData ( float, float ) void
calcArea ( ) float getWidth ( )
float getLength ( ) float getArea ( )
Access specifiers
3 member variables
Not accessible outside the class
Accessible outside the class
5 member functions Prototypes used for
manipulating member variables
6
Defining Member Functions
Definitions of member functions are written
outside the class declaration
Scope resolution operator
void Rectangle setData ( float w, float l )
width w length l void Rectangle
calcArea ( ) area width
length float Rectangle getWidth ( )
return width float Rectangle getLength (
) return length
Return type
Member function name
Name of class
Initializing private member variables
Member functions can access member variables
within the class
Initializing private member variable
Accessing the value of a member variable
Accessing the value of a member variable
7
An Instance of a Class
After a class is defined, objects of the class
can be declared
Defines the class
Declare box an object of class Rectangle
Class name
class Rectangle private float width
float length float area public
void setData ( float, float ) void
calcArea ( ) float getWidth ( )
float getLength ( ) float getArea ( )
Rectangle box box.setData ( 10.0, 12.5
) box.calcArea ( ) cout ltlt box.getWidth( )
Initialize member variables width and length of
box
Calling object
Member function
Calculates area member variable of box
Displays width member variable of box
8
Why Have Private Members?
  • In object-oriented programming
  • An object should protect its important data by
  • making it private
  • An object should provide a public interface to
  • access the private data

Objects usually have some variables and functions
that are only used internally and should not be
modified from outside the class
  • If a member variable is declared as private, an
    application must use a public member function to
  • Store values in the private member variable
  • Retrieve values from the private member variable

9
Separate Specification and Implementation
Class declarations are stored in their own header
file called the class specification file
Class specification file for Rectangle class
Name of header file
rectangle.h
Use an identifier similar to class name
Extension
Member functions definitions are stored in a
separate .cpp file called the class
implementation file
Name of implementation file
Class implementation file for Rectangle class
rectangle.cpp
Extension
An application that uses the class should
include the specification file and link the
implementation file with the main program
UNIX command line compile
g appProg.cpp rectangle.cpp
Application file
Implementation file
10
Separate Specification and Implementation
Specification file rectangle.h
class Rectangle private float width
. . . public void setData (
float, float ) . . . float getArea
( )
include rectangle.h void Rectangle setData
( float w, float l ) width w
length l . . . void Rectangle getArea (
) return area
include rectangle.h void main ( )
Rectangle box box.setData ( 10.0, 12.5 )
. . . cout ltlt box.getArea ( ) .
. .
Implementation file rectangle.cpp
Application program appProg.cpp
11
Private Member Functions
Private member functions can be called from
member functions of the same class
Sometimes member functions are necessary for
internal processing but not useful to outside
programs
class Rectangle private float width
float length float area void
calcArea ( ) public void setData (
float, float ) float getWidth ( )
float getLength ( ) float getArea ( )
void Rectangle setData (float w, float l )
width w length l calcArea (
)
Set values of private member variables
Calling a private member function to initialize
member variable area
Private member function called from setData
12
Inline Member Functions
If the body of a member function is small, define
it inline within the class definition
Inline functions are not called like regular
functions The compiler replaces the inline
function calls with the actually code of the
function This increases the size of the .exe but
improves performance by cutting overhead
class Rectangle private float width
float length float area void
calcArea ( ) area width length
public void setData ( float, float )
float getWidth ( ) float getLength ( )
float getArea ( )
Function body
Inline instead of a separate function definition
13
Constructors
A constructor is a member function automatically
called when a class object is created
Constructors have the same name as the class and
are generally used for initialization purposes
class Demo public Demo (
) Demo Demo ( ) cout ltlt In the
constructor\n void main ( ) Demo
demoObj cout ltlt In main\n
Program output
In the constructor In main
Prototype
Default constructor
No return type
Definition of constructor
Class name
Class name
A default constructor has no arguments
Constructor is executed here when object is
declared
14
Constructors
This constructor dynamically allocates memory for
the desc array
void main ( ) InvItem stock
stock.setInfo ( Hammer, 20 ) cout ltlt
stock.getDesc( ) ltlt endl cout ltlt
stock.getUnits ( ) ltlt endl
Constructor is executed Memory allocated
class InvItem private char
desc int units public
InvItem ( ) desc new char 51
void setInfo ( char dscr, int un ) strcpy
( desc, dscr ) units un
char getDesc ( ) return desc int
getUnits ( ) return units
Inline default constructor
Program output
Inline member functions
Hammer 20
15
Destructors
A destructor is a member function automatically
called when an object is destroyed
Destructors have the same name as the class and
are preceded by a tilde character ( )
class Demo public Demo ( )
Demo ( ) Demo Demo ( ) cout
ltlt In the constructor\n Demo Demo ( )
cout ltlt In the destructor\n
void main ( ) Demo demoObj cout ltlt
In main\n
Constructor executed
Constructor
Prototype for Destructor
Destructor
Destructor executed
Program output
In the constructor In main In the destructor
No return type
Definition for destructor
Class name
16
Destructors
The constructor dynamically allocates memory The
destructor frees allocated memory
void main ( ) InvItem stock
stock.setInfo ( Hammer, 20 ) cout ltlt
stock.getDesc( ) ltlt endl cout ltlt
stock.getUnits ( ) ltlt endl
Constructor is executed Memory allocated
class InvItem private char
desc int units public
InvItem ( ) desc new char 51
InvItem ( ) delete desc void
setInfo ( char dscr, int un ) strcpy (
desc, dscr ) units un char
getDesc ( ) return desc int
getUnits ( ) return units
Destructor is executed Memory freed
Inline default destructor
Program output
Inline member functions
Hammer 20
17
Constructors Accept Arguments
Initialization information can be passed as
arguments to an objects constructor
class Sale private float
taxRate float total public
Sale ( float rate ) taxRate rate
void calcSale ( float cost )
total cost ( cost taxRate )
float getTotal ( ) return total void main
( ) Sale cashier ( 0.06 ) cashier.
calcSale ( 125.00 ) cout ltlt ltlt
cashier.getTotal( )
Program output

132.50
Inline constructor
Parameter
Member variable taxRate is initialized to value
of parameter rate
Declares cashier as object of class Sale,
executes constructor which initializes taxRate
Multiplies by taxRate, assigns result to member
variable total
Displays value of member variable total
18
Input Validation Class
Design general-purpose objects to be used by a
variety of applications
CharRange CharRange ( char low, char high,
char str) lower toupper ( low )
upper toupper ( high ) errMsg new char
strlen ( str )1 strcpy ( errMsg, str
)
class CharRange private char
input char lower char
upper char errMsg public
CharRange ( char, char ) char
getChar( )
char CharRange getChar ( ) cin.get (
input ) input toupper ( input )
while ( input lt lower input gt upper )
cout ltlt errMsg cin.get ( input
) input toupper ( input )
return input
Class CharRange allows the user to enter a
character, and then validates that character
within a specified range of characters
Declares an object of CharRange class
CharRange in ( A, D, Msg )
19
Overloaded Constructors
More than one constructor can be defined for a
class
A function name is overloaded when multiple
functions with the same name exist
void main ( ) InvItem item1 ( Hammer
) InvItem item2 item1.setUnits ( 15
) item2.setInfo ( Pliers, 25 )
Uses constructor with character pointer argument
class InvItem private char
desc int units public
InvItem ( int size 51 ) desc new char
size InvItem ( char d ) desc
new char strlen(d) 1
strcpy( desc, d )
void setInfo ( char dscr, int un ) strcpy (
desc, dscr )
units un void setUnits (
int u ) units u . . . .
Uses default constructor
Default constructor with one default argument
Constructor with a character pointer argument
Same function name
Different parameter lists
20
Only One Default Constructor and Destructor
When an object is declared without an argument
list for its constructor, the compiler
automatically calls the objects default
constructor
A constructor with all default arguments is
considered a default constructor
In this case, if an object is declared with no
argument list, the compiler will not know which
constructor to execute
class InvItem private char
desc int units public
InvItem ( ) desc new char 80
InvItem ( int size 51 ) desc new char
size InvItem ( char d ) desc
new char strlen(d) 1
strcpy( desc, d )
InvItem ( ) delete desc .
. . .
Default constructor with no arguments
ILLEGAL also a default constructor
Three constructors?
Constructor with a character pointer argument
Destructor
21
Array of Objects
An array of InvItem objects represents all of the
types of items in an inventory
Array of 40 InvItem objects
InvItem inventory 40
Compiler uses the default constructor in this case
Class
Array name
When an array of objects is created, the
constructor is called for every element in the
array
To initialize each object in the array to a
value, specify the arguments for each constructor
individually in an initializer list
InvItem inventory 3 Hammer, Pliers,
35
Uses first constructor
Uses second constructor
Constructors InvItem Class
InvItem ( int size 51) desc new char size
InvItem ( char d ) desc new char
strlen(d) 1
strcpy( desc, d )
22
Array of Objects
Objects in an array are accessed with subscripts
void main ( ) InvItem inventory 3
inventory 2.setInfo ( Pliers, 7 ) cout
ltlt inventory 2.getDesc( ) ltlt \t
ltlt inventory 2.getUnits( )
Uses default constructor for each array element
Sets desc and units variable of inventory2
class InvItem private char
desc int units public
InvItem ( int size 51 ) desc new char
size InvItem ( char d ) desc
new char strlen(d) 1
strcpy( desc, d )
void setInfo ( char dscr, int un ) strcpy (
desc, dscr )
units un char getDesc (
) return desc int getUnits ( )
return units . . . .
Displays desc and units variables of inventory2
Calling object
Member function
Write a Comment
User Comments (0)
About PowerShow.com