This presentation is a part of the COP2272C college level course taught at the Florida Polytechnic University located in Lakeland Florida. The purpose of this course is to introduce students to the C++ language and the fundamentals of object orientated programming..
The course is one semester in length and meets for 2 hours twice a week. The Instructor is Dr. Jim Anderson.
3. Let’s Talk About Software!
Imperative Programming:
Uses a sequence of statements to
determine how to reach a certain
goal. These statements are said to
change the state of the program
as each one is executed in turn.
Object-Orientated Programming:
Object-oriented programming
(OOP) is a programming paradigm
based on the concept of "objects",
which are data structures that
contain data, in the form of fields,
often known as attributes; and code,
in the form of procedures, often
known as methods.
In OO programming, computer
programs are designed by making
them out of objects that interact with
one another
4. Why OO?
• Object-Oriented Programming has the following advantages
over conventional approaches:
– OOP provides a clear modular structure for programs which makes it
good for defining abstract datatypes where implementation details are
hidden and the unit has a clearly defined interface.
– OOP makes it easy to maintain and modify existing code as new
objects can be created with small differences to existing ones.
– OOP provides a good framework for code libraries where supplied
software components can be easily adapted and modified by the
programmer. This is particularly useful for developing graphical user
interfaces.
Image Credit: betanews.com
5. Our First Program
• We begin with an example that consists of class
GradeBook will represent a grade book that an
instructor can use to maintain student test scores.
• And a main function that creates a GradeBook
object.
• Function main uses this object and its member
function to display a message on the screen
welcoming the instructor to the grade-book program.
Image Credit: inthekeyofh.com
6. The GradeBook Class Program
// Title: 0501 - The GradeBook Class Program
// Description: Define class GradeBook with a member function displayMessage
// create a GradeBook object, and call its displayMessage function.
#include <iostream>
using namespace std;
// GradeBook class definition
class GradeBook
{
public:
// function that displays a welcome message to the GradeBook user
void displayMessage()
{
cout << "Welcome to the Grade Book !" << endl;
} // end function displayMessage
}; // end class GradeBook
// function main begins program execution
int main()
{
GradeBook myGradeBook; // create a GradeBook object named myGradeBook
myGradeBook.displayMessage(); // call object's displayMessage function
} // end main
7. Let’s Get Some Class
• Class GradeBook
– Before function main can create a GradeBook object, we must tell the
compiler what member functions and data members belong to the class. The
GradeBook class definition contains a member function called displayMessage
that displays a message on the screen.
– We need to make an object of class GradeBook and call its displayMessage
member function to display the welcome message.
– By convention, the name of a user-defined class begins with a capital letter,
and for readability, each subsequent word in the class name begins with a
capital letter. The class definition terminates with a semicolon.
// GradeBook class definition
class GradeBook
{
public:
// function that displays a welcome message to the
GradeBook user
void displayMessage()
{
cout << "Welcome to the Grade Book!" << endl;
} // end function displayMessage
}; // end class GradeBook
Image Credit: simpsons.wikia.com
8. Let’s Get Some Class
• Class GradeBook
– The class definition begins with the keyword class followed by the class name
GradeBook. By convention, the name of a user-defined class begins with a
capital letter, and for readability, each subsequent word in the class name
begins with a capital letter.
– Every class’s body is enclosed in a pair of left and right braces ({ and })
– The class definition terminates with a semicolon.
// GradeBook class definition
class GradeBook
{
public:
// function that displays a welcome message to the
GradeBook user
void displayMessage()
{
cout << "Welcome to the Grade Book!" << endl;
} // end function displayMessage
}; // end class GradeBook
Image Credit: www.aptce.org
9. Let’s Get Some Class
• Class GradeBook
– The next line contains the keyword public, which is an access specifier. After
this we define member function displayMessage. This member function
appears after access specifier public: to indicate that the function is “available
to the public”—that is, it can be called by other functions in the program (such
as main)
– Access specifiers are always followed by a colon (:).
// GradeBook class definition
class GradeBook
{
public:
// function that displays a welcome message to the
GradeBook user
void displayMessage()
{
cout << "Welcome to the Grade Book!" << endl;
} // end function displayMessage
}; // end class GradeBook
Image Credit: sahamparsa.blogspot.com
10. Let’s Get Some Class
• Each function in a program performs a task and may return a value when it
completes its task—for example, a function might perform a calculation, then
return the result of that calculation.
• When you define a function, you must specify a return type to indicate the type of
the value returned by the function when it completes its task.
• The keyword void to the left of the function name displayMessage is the function’s
return type.
• Return type void indicates that displayMessage will not return any data to its
calling function when it completes its task.
void displayMessage()
Return type Function
Image Credit: www.iconshut.com
11. Let’s Get Some Class
• The name of the member function, displayMessage, follows the return type. By
convention, function names begin with a lowercase first letter and all subsequent
words in the name begin with a capital letter.
• The parentheses after the member function name indicate that this is a function.
An empty set of parentheses indicates that this member function does not require
additional data to perform its task.
• Every function’s body is delimited by left and right braces ({ and }). The body of a
function contains statements that perform the function’s task. In this case,
member function displayMessage contains one statement that displays the
message "Welcome to the Grade Book!". After this statement executes, the
function has completed its task.
void displayMessage()
{
cout << "Welcome to the Grade Book!" << endl;
} // end function displayMessage
{}
12. Common Programming Errors!
• Forgetting the semicolon at the end of a class definition is
a syntax error.
Image Credit: www.crosstest.com
13. Running The Class Program
• The function main begins the execution of every program.
• In this program, we’d like to call class GradeBook’s displayMessage member
function to display the welcome message. Typically, you cannot call a member
function of a class until you create an object of that class.
• We create an object of class GradeBook called myGradeBook. The variable’s type
is GradeBook. When we declare variables of type int, the compiler knows what int
is: a fundamental type that’s “built into” C++. However, the compiler does not
automatically know what type GradeBook is—it’s a user-defined type.
• We tell the compiler what GradeBook is by including the class definition. If we
omitted these lines, the compiler would issue an error message. Each class you
create becomes a new type that can be used to create objects. You can define new
class types as needed; this is one reason why C++ is known as an extensible
language.
Image Credit: www.blogtrw.com
14. Running The Class Program
• We then call the member function displayMessage using variable myGradeBook
followed by the dot operator (.), the function name displayMessage and an empty
set of parentheses.
• This call causes the displayMessage function to perform its task. “myGradeBook.”
indicates that main should use the GradeBook object that was created.
• The empty parentheses indicate that member function displayMessage does not
require additional data to perform its task, which is why we called this function
with empty parentheses.
• When displayMessage completes its task, the program reaches the end of main
and terminates.
Image Credit: www.corbisimages.com
15. The UML
(Unified Modeling Language)
• Although many different OOAD processes exist, a single graphical language for
communicating the results of any OOAD process has come into wide use. This
language, known as the Unified Modeling Language (UML), is now the most widely
used graphical scheme for modeling object-oriented systems.
• In the UML, each class is modeled in a UML class diagram as a rectangle with three
compartments. The top compartment contains the class’s name centered
horizontally and in boldface type. The middle compartment contains the class’s
attributes, which correspond to data members in C++.
• This compartment is currently empty, because class GradeBook does not have any
attributes. The bottom compartment contains the class’s operations, which
correspond to member functions in C++.
16. The UML
(Unified Modeling Language)
• The UML models operations by listing the operation name followed by a set of
parentheses. Class GradeBook has only one member function, displayMessage, so
the bottom compartment lists one operation with this name.
• Member function displayMessage does not require additional information to
perform its tasks, so the parentheses following displayMessage in the class
diagram are empty, just as they are in the member function’s header.
• The plus sign (+) in front of the operation name indicates that displayMessage is a
public operation in the UML (i.e., a public member function in C++).
17. Defining a Member Function
with a Parameter
Pressing on a car’s gas pedal sends two
messages to the car:
1. Go faster
2. Go how much faster?
So the message to the car includes both the task
to perform and additional information that helps
the car perform the task.
This additional information is known as a
parameter — the value of the parameter helps
the car determine how fast to accelerate.
18. Next (Real) Program
• Our next example redefines class GradeBook with a
displayMessage member function that displays the
course name as part of the welcome message.
• The new version of displayMessage requires a
parameter (courseName) that represents the course
name to output.
Image Credit: www.whitewaterfreight.com
19. The GradeBook Class Program
with a Parameter
// Title: 0502 - The GradeBook Class Program with a
Parameter
// Description: Define class GradeBook with a member
function that takes a parameter,
// create a GradeBook object and call its displayMessage
function.
#include <iostream>
#include <string> // program uses C++ standard string class
using namespace std;
// GradeBook class definition
class GradeBook
{
public:
// function that displays a welcome message to the
GradeBook user
void displayMessage( string courseName )
{
cout << "Welcome to the Grade Book forn" <<
courseName << "!"
<< endl;
} // end function displayMessage
}; // end class GradeBook
// function main begins program execution
int main()
{
string nameOfCourse; // string of characters to store the
course name
GradeBook myGradeBook; // create a GradeBook object
named myGradeBook
// prompt for and input course name
cout << "Please enter the course name:" << endl;
getline( cin, nameOfCourse ); // read a course name with
blanks
cout << endl; // output a blank line
// call myGradeBook's displayMessage function
// and pass nameOfCourse as an argument
myGradeBook.displayMessage( nameOfCourse );
} // end main
20. Functions, Parameters, & Arguments
• A member function can require one or more parameters that represent additional
data it needs to perform its task.
• A function call supplies values—called arguments—for each of the function’s
parameters.
• For example, to make a deposit into a bank account, suppose a deposit member
function of an Account class specifies a parameter that represents the deposit
amount.
• When the deposit member function is called, an argument value representing
the deposit amount is copied to the member function’s parameter.
• The member function then adds that amount to the account balance.
int makeDeposit(depositAmount)
Image Credit: uldissprogis.com
21. Let’s Talk About Strings
• We create a variable of type string called nameOfCourse that will be used to store
the course name entered by the user.
• A variable of type string represents a string of characters such as
“CS101 Introduction to C++ Programming".
• A string is actually an object of the C++ Standard Library class string.
• This class is defined in header <string>, and the name string, like cout, belongs to
namespace std.
• To enable the program to compile, we include the <string> header. The using
directive allows us to simply write string rather than std::string. For now, you
can think of string variables like variables of other types such as int.
#include <string> // program uses C++ standard string class using namespace std;
22. Why Make Things Harder?
• First we create an object of class GradeBook named myGradeBook. Next we
prompt the user to enter a course name.
• We read the name from the user and assigns it to the nameOfCourse variable,
using the library function getline to perform the input.
• Why are we making this hard? Why not simply use CIN to obtain the course name?
• In our program execution, we use the course name “COP2272 Introduction to C++
Programming,” which contains multiple words separated by blanks.
• When cin is used with the stream extraction operator, it reads characters until the
first white-space character is reached. Thus, only “CS101” would be read by the
preceding statement. The rest of the course name would have to be read by
subsequent input operations.
Image Credit: hopesrising.com
23. Why Make Things Harder?
• In this example, we’d like the user to type the complete course name and
press Enter to submit it to the program, and we’d like to store the entire
course name in the string variable nameOfCourse.
• The function call getline( cin, nameOfCourse ) reads characters (including
the space characters that separate the words in the input) from the
standard input stream object cin (i.e., the keyboard) until the newline
character is encountered, places the characters in the string variable
nameOfCourse and discards the newline character.
• When you press Enter while typing program input, a newline is inserted in
the input stream.
• The <string> header must be included in the program to use function
getline, which belongs to namespace std.
getline( cin, nameOfCourse ); // read a course name with blanks
Image Credit: www.amazon.com
24. Functions, Parameters, & Arguments
• The next line calls myGradeBook’s displayMessage member function.
• The nameOfCourse variable in parentheses is the argument that’s passed to
member function displayMessage so that it can perform its task.
• The value of variable nameOfCourse in main is copied to member function
displayMessage’s parameter courseName.
• When you execute this program, member function displayMessage outputs as
part of the welcome message the course name you type (in our sample execution,
“COP2271 Introduction to C++ Programming”).
myGradeBook.displayMessage(nameOfCourse);
// call object's displayMessage function
Argument
Member Function
Image Credit: mathinsight.org
25. Functions, Parameters, & Arguments
• To specify in a function definition that the function requires data to perform its
task, you place additional information in the function’s parameter list, which is
located in the parentheses following the function name.
• The parameter list may contain any number of parameters, including none at all
to indicate that a function does not require any parameters.
• Member function displayMessage’s parameter list declares that
the function requires one parameter.
• Each parameter specifies a type and an identifier. The type string and the identifier
courseName indicate that member function displayMessage requires a string to
perform its task.
• The member function body uses the parameter courseName to access the value
that’s passed to the function in the function call.
void displayMessage( string courseName )
Image Credit: commons.wikimedia.org
26. Functions, Parameters, & Arguments
• A function can specify multiple parameters by separating each from the next with
a comma.
• The number and order of arguments in a function call must match the number
and order of parameters in the parameter list of the called member function’s
header.
• Also, the argument types in the function call must be consistent with the types of
the corresponding parameters in the function header.
• In our example, the one string argument in the function call (i.e., nameOfCourse)
exactly matches the one string parameter in the member-function definition (i.e.,
courseName).
void displayMessage( string courseName )
myGradeBook.displayMessage(nameOfCourse); // call object's displayMessage
function Image Credit: sbkb.org
27. Updated UML Class Diagram for
Class GradeBook
• This GradeBook class contains public member function displayMessage. However, this version
of displayMessage has a parameter.
• The UML models a parameter by listing the parameter name, followed by a colon and the
parameter type in the parentheses following the operation name.
• The UML has its own data types similar to those of C++. The UML is language independent—
it’s used with many different programming languages—so its terminology does not exactly
match that of C++.
• For example, the UML type String corresponds to the C++ type string.
• Member function displayMessage of class GradeBook has a string parameter named
courseName, so the diagram lists courseName : String between the parentheses following
the operation name displayMessage.
• This version of the GradeBook class still does not have any data members.
28. What We Covered Today
1. We created user-defined classes, and created
and used objects of those classes.
2. We declared data members of a class to
maintain data for each object of the class.
3. We also defined member functions that
operate on that data. You learned how to call
an object’s member functions to request the
services the object provides and how to pass
data to those member functions as
arguments.
Image Credit: http://www.tswdj.com/blog/2011/05/17/the-grooms-checklist/
29. What We’ll Be Covering Next Time
1. How to define a class and
use it to create an object.
2. How to implement a class’s
behaviors as member
functions.
3. How to implement a class’s
attributes as data
members.
Image Credit: http://merchantblog.thefind.com/2011/01/merchant-newsletter/resolve-to-take-advantage-of-these-5-e-commerce-trends/attachment/crystal-ball-fullsize/
30. An Introduction To Software
Development Using C++
Class #5:
Checkbook Challenge
31. What’s In Your C++ Toolbox?
cout / cin #include if Math Class String getline
32. Today’s Programming Challenge
• Create a checkbook program that permits: deposits, withdrawals, and account
balance checks.
• This program will use one class: Checkbook
• This program will use three methods: depositMoney, withdrawMoney, and
checkMyBalance.
• When you start the program, deposit $100 into the account.
• Print out how much money is in the account.
• Withdraw $75.
• Print out how much money is in the account.
• Attempt to withdraw $50 – what happens?
Image Credit: www.balancetrack.org
33. What We’ll Be Covering Next Time
1. Part II of the checkbook
challenge.
Image Credit: http://merchantblog.thefind.com/2011/01/merchant-newsletter/resolve-to-take-advantage-of-these-5-e-commerce-trends/attachment/crystal-ball-fullsize/
Notes de l'éditeur
New name for the class
I know what this means
Technical professionals are who get hired
This means much more than just having a narrow vertical knowledge of some subject area.
It means that you know how to produce an outcome that I value.
I’m willing to pay you to do that.
New name for the class
I know what this means
Technical professionals are who get hired
This means much more than just having a narrow vertical knowledge of some subject area.
It means that you know how to produce an outcome that I value.
I’m willing to pay you to do that.