Documente Academic
Documente Profesional
Documente Cultură
CS-2303
System Programming Concepts
(Slides include materials from The C Programming Language, 2nd edition, by Kernighan and Ritchie and
from C: How to Program, 5th and 6th editions, by Deitel and Deitel)
CS-2303, Introduction to 1
C-Term 2010 Classes and
Reading
CS-2303, Introduction to 2
C-Term 2010 Classes and
Classes and Objects
CS-2303, Introduction to 3
C-Term 2010 Classes and
C++ Program Structure
CS-2303, Introduction to 4
C-Term 2010 Classes and
19.4 Defining a Class
With a Member Function
• Class definition
– Tells the compiler what member functions and data
members belong to the class.
– Keyword class followed by the class’s name.
– Class body is enclosed in braces ({})
• Specifies data members and member functions
• Access-specifier public:
– Indicates that a member function or data member is
accessible to other functions and member functions of
other classes.
CS-2303, Introduction to 5
C-Term 2010 Classes and
C++ Gradebook Example
Deitel & Deitel Fig 19.1
CS-2303, Introduction to 6
C-Term 2010 Classes and
C++ Gradebook Example
CS-2303, Introduction to 7
C-Term 2010 Systems Programming:
Classes andClasses and Objects 7
C++ Gradebook Example
s cl ass
o f thi a i n
ob ject ble of m
g an va ria
e clarin omatic
D
n a ut
as a I.e., allocated on The Stack
CS-2303, Introduction to 8
C-Term 2010 Classes and
Member Functions with Parameters
Member function
parameter
CS-2303, Introduction to 9
C-Term 2010 Classes and
Member Functions with Parameters (cont.)
CS-2303, Introduction to 10
C-Term 2010 Classes and
Useful Tidbits
• A string
– Represents a string of characters.
– An object of C++ Standard Library class std::string
• Defined in header file <string>.
– Not a character array as in C.
CS-2303, Introduction to 11
C-Term 2010 Classes and
Data Members of a Class
CS-2303, Introduction to 12
C-Term 2010 Classes and
Access-specifier private
CS-2303, Introduction to 13
C-Term 2010 Classes and
Public and Private Members
CS-2303, Introduction to 14
C-Term 2010 Classes and
Public and Private Members (continued)
default constructor
CS-2303, Introduction to 15
C-Term 2010 Classes and
Public and Private Members (continued)
CS-2303, Introduction to 16
C-Term 2010 Classes and
Software Engineering Observation 19.1
• Called automatically
– When function exits scope of automatic class
object
– By deleteCompiler’s
operatordefault simply calls destructors of
data members of the class.
CS-2303, Introduction to 19
C-Term 2010 Classes and
Constructors and Destructors (continued)
• Purpose of Destructors
• Free dynamic storage pointed to only by members of
object
• Reduce reference count when object disappears
• Safely close things – e.g., files
•…
CS-2303, Introduction to 20
C-Term 2010 Classes and
Constructor Example
CS-2303, Introduction to 21
C-Term 2010 Classes and
Constructor Example
b je ct
l a s s o emory
t h is c ted m
e d so ll o ca
r ne ically a
c t o
e stru dynam
D ree
n f
ca
CS-2303, Introduction to 22
C-Term 2010 Classes and
Constructor Example
CS-2303, Introduction to 23
C-Term 2010 Classes and
Class in a Separate Header File for
Reusability
• .cpp files for source-code implemenations
– Class implementations
– Main programs
– Test programs
– …
• Header files
– Separate files in which class definitions are placed.
– Allow compiler to recognize the classes when used elsewhere.
– Generally have .h filename extensions
• Driver file
– A program used to test software (such as classes).
– Contains a main function so it can be executed.
CS-2303, Introduction to 24
C-Term 2010 Classes and
Interfaces versus Implementation
• Interface
– Describes what services a class’s clients can use and
how to request those services.
• without revealing how the class carries out the services.
• a class definition listing only public member function
prototypes.
– A class’s interface consists of the class’s public
member functions (services).
CS-2303, Introduction to 25
C-Term 2010 Classes and
Interfaces versus Implementation
CS-2303, Introduction to 26
C-Term 2010 Classes and
Interfaces versus Implementation (Example)
CS-2303, Introduction to 27
C-Term 2010 Classes and
Interfaces versus Implementation (continued)
CS-2303, Introduction to 28
C-Term 2010 Classes and
Interfaces versus Implementation (continued)
CS-2303, Introduction to 29
C-Term 2010 Classes and
Client of the Interface
CS-2303, Introduction to 30
C-Term 2010 Classes and
Summary
Reading:–
Deitel & Deitel, Chapter 19
CS-2303, Introduction to 32
C-Term 2010 Classes and