Sunteți pe pagina 1din 16

CompuLer Sclence ll - CCSC 032 - Sprlng 2014

LecLure 3
COSC 052 Spring 2014
ro[ecL #1 Cvervlew
Cb[ecL Composluon
lnformauon Pldlng (8evlew)
Pomework for LecLure 4
8ead Secuon 11.3 Lhrough 11.9
SLudy ro[ecL #1 8equlremenLs
Project #1

Copyright 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Chapter 7: Introduction to Classes and
Starting Out with C++
Early Objects
Eighth Edition

by Tony Gaddis, Judy Walters,
and Godfrey Muganda
7.1 Abstract Data Types
7.2 Object-Oriented Programming
7.3 Introduction to Classes
7.4 Creating and Using Objects
7.5 Defining Member Functions
7.6 Constructors
7.7 Destructors
7.8 Private Member Functions
7.9 Passing Objects to Functions
7.10 Object Composition
7.11 Separating Class Specification,
Implementation, and Client Code
7.12 Structures
7.14 Introduction to Object-Oriented
Analysis and Design
7.15 Screen Control

7.11 Separating Class Specification,
Implementation, and Client Code
Separating class declaration, member function
definitions, and the program that uses the class
into separate files is considered good design

Information Hiding
Using Separate Files
Place class declaration in a header file that serves as the
class specification file.
Name the file classname.h
For example, Square.h
Place member function definitions in a class
implementation file.
Name the file classname.cpp
For example, Square.cpp
This file should #include the class specification file.
A client program (client code) that uses the class must
#include the class specification file and be compiled
and linked with the class implementation file.
Include Guards
Used to prevent a header file from being more than once

#ifndef symbol_name
#define symbol_name
. . . (normal contents of header file)
symbol_name is usually the name of the header file, in
all capital letters:

#ifndef SQUARE_H
#define SQUARE_H
. . .
What Should Be Done
Inside vs. Outside the Class
Class should be designed to provide functions to
store and retrieve data
In general, input and output (I/O) should be done
by functions that use class objects, rather than by
class member functions
7.10 Object Composition
Occurs when an object is a member variable of
another object.
It is often used to design complex objects whose
members are simpler objects
ex. (from book): Define a rectangle class. Then,
define a carpet class and use a rectangle object
as a member of a carpet object.
11.9 Aggregation and Composition
Class aggregation: An object of one class owns an
object of another class

Class composition: A form of aggregation where the
enclosing class controls the lifetime of the objects of
the enclosed class

Supports the modeling of has-a relationship
between classes enclosing class has a(n)
instance of the enclosed class
Object Composition
class StudentInfo
string firstName, LastName;
string address, city, state, zip;

class Student
StudentInfo personalData;
Member Initialization Lists
Used in constructors for classes involved in aggregation.

Allows constructor for enclosing class to
Invoke the constructor of the enclosed class
Pass arguments to the constructor of the enclosed class

//body may be empty
Member Initialization Lists
class StudentInfo

class Student
StudentInfo personalData;

Student(string fname, string lname); //prototype

Student::Student(string fname, string lname) :
personalData(fname, lname)
//other statements go here if applicable

Member Initialization Lists
Member Initialization lists can be used to simplify
the coding of constructors

Should keep the entries in the initialization list in
the same order as they are declared in the class
Aggregation Through Pointers
A has-a relationship can be implemented by
owning a pointer to an object
Can be used when multiple objects of a class may
have the same attribute for a member
Students who have the same major
Employees who have the same corporate division
Using pointers minimizes data duplication and
saves space
Aggregation, Composition, and Object Lifetimes
Aggregation represents the owner/owned
relationship between objects.
Composition is a form of aggregation in which
the lifetime of the owned object is the same as
that of the owner object
Owned object is usually created as part of the
owning objects constructor, destroyed as part of
owning objects destructor