Professional Documents
Culture Documents
1 2
7 8
Information Hiding Encapsulation
9 10
11 12
Overloading and Binding Time Inheritance
13 14
• Is written documentation
about the program.
15
Heart of Object-Oriented Programming Modeling
• Don't make a new language, extend • All phases of software life-cycle are
your old one. modeling
• analysis - modeling of problem
• Objects should be abstractions of • design - modeling of solution
problem domain. • implementation - making model run on a
computer
• maintenance - fixing/extending your model
17 18
• Basing system design on structure of • Claim: people model the world with
problem makes system "objects"
• more reusable • objects
• more extensible • classes
• relationships between objects
• relationships between classes
19 20
Modeling Objects and Relationships
21 22
23 24
What Really is an Object? What Really is an Object?
25 26
Classification Classification
27 28
C++ class data type C++ Classes
29 30
31 32
Member Access Modifiers Member Access Modifiers
• access modifiers set how a member may be • protected – like private, but can be
accessed
• all members in a class are given an access inherited
modifier • all members of a class are private by
• public – indicates that the member may be default
accessed by code outside of the class and
can be inherited • access is always explicitly stated by
• private – indicates that the member may convention
only be accessed by code inside of the
class and cannot be inherited
33 34
accesses (does not modify) the value(s) of the class DateType // declares a class data type
{
data member(s)
public : // 4 public member functions
• Mutator function member: function that
void Initialize (int newMonth, int newDay, int newYear ) ;
modifies the value(s) of the data member(s) int YearIs( ) const ; // returns year
int MonthIs( ) const ; // returns month
• Constant function member: function that int DayIs( ) const ; // returns day
cannot modify data members and includes the private : // 3 private data members
reserved word const at the end of the
int year ;
function heading int month ;
int day ;
} ;
35 36
Use of C++ data type class Client Code Using DateType
#include <iostream>
• Variables of a class type are called objects #include “datetype.h” // includes specification of the class
(or instances) of that particular class. using namespace std;
int main ( void )
{
• Software that declares and uses objects of DateType startDate ; // declares 2 objects of DateType
the class is called a client. DateType endDate ;
bool retired = false ;
startDate.Initialize ( 6, 30, 1998 ) ;
• Client code uses public member functions endDate.Initialize ( 10, 31, 2002 ) ;
(called methods in OOP) to handle its class cout << startDate.MonthIs( ) << “/” << startDate.DayIs( )
objects. << “/” << startDate.YearIs( ) << endl;
while ( ! retired )
{ finishSomeTask( ) ;
• Sending a message means calling a public . . .
member function. }
}
37 38
private:
. . . Private data: Private data:
} ; Initialize Initialize
year 1998 year 2002
YearIs YearIs
// IMPLEMENTATION FILE ( datetype.cpp ) month 6 month 10
MonthIs MonthIs
day 30 day 31
// Implements the DateType member functions.
DayIs DayIs
. . .
39 40
Implementation of DateType
member functions
int DateType :: MonthIs ( ) const
// IMPLEMENTATION FILE (datetype.cpp) // Accessor function for data member month
#include “datetype.h” // also must appear in client code {
return month ;
void DateType :: Initialize ( int newMonth, int newDay, }
int newYear )
// Post: year is set to newYear. int DateType :: YearIs ( ) const
// month is set to newMonth. // Accessor function for data member year
// day is set to newDay. {
{ return year ;
year = newYear ; }
month = newMonth ;
day = newDay ; int DateType :: DayIs ( ) const
} // Accessor function for data member day
{
return day ;
41 42
42
}
• The member selection operator ( . ) selects either data • C++ programs typically use several class types.
members or member functions.
• Different classes can have member functions with the
• Header files iostream and fstream declare the istream, same identifer, like Write( ).
ostream,and ifstream, ofstream I/O classes.
• Member selection operator is used to determine the
• Both cin and cout are class objects and get and ignore class whose member function Write( ) is invoked.
are member functions. currentDate .Write( ) ; // class DateType
cin.get (someChar) ; numberZ .Write( ) ; // class ComplexNumberType
cin.ignore (100, ‘\n’) ;
• In the implementation file, the scope resolution
• These statements declare myInfile as an instance of operator is used in the heading before the member
class ifstream and invoke member function open. function’s name to specify its class.
ifstream myInfile ; void DateType :: Write ( ) const
myInfile.open ( “A:\\mydata.dat” ) ; { . . .
43 } 44
Three Ways to Access Members
Namespace
within a Namespace
namespace mySpace
• Qualify each reference:
{
mySpace::name with every reference.
// All variables and functions within this
• Using declaration:
// block must be accessed using scope using mySpace::name;
// resolution operator (::). All future references to name refer to mySpace::name.
} • Using directive:
Purpose: Avoid namespace pollution. using namespace mySpace;
All members of mySpace can be referenced without
qualification.
45 46
47 48
try, catch, and throw Exception Example
Exception Example
try
{
inFile >> d;
if (d == 0)
throw string(“Not acceptable");
e = 1.0/d;
}
catch (string message)
{
cout << "Bad Data! Program aborted!" << endl;
exit(1);
}
inFile.close();
cout << e << endl;
return 0;
}
51