SlideShare une entreprise Scribd logo
1  sur  33
An Introduction To Software
Development Using C++
Class #5:
Introduction To
Classes, Objects, &
Strings
Let’s Talk About Art!
Baroque Expressionist
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
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
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
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
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
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
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
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
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
{}
Common Programming Errors!
• Forgetting the semicolon at the end of a class definition is
a syntax error.
Image Credit: www.crosstest.com
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
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
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++.
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++).
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.
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
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
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
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;
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
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
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
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
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
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.
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/
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/
An Introduction To Software
Development Using C++
Class #5:
Checkbook Challenge
What’s In Your C++ Toolbox?
cout / cin #include if Math Class String getline
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
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/

Contenu connexe

Tendances

Notes of java first unit
Notes of java first unitNotes of java first unit
Notes of java first unitgowher172236
 
SMI - Introduction to Java
SMI - Introduction to JavaSMI - Introduction to Java
SMI - Introduction to JavaSMIJava
 
Abap object-oriented-programming-tutorials
Abap object-oriented-programming-tutorialsAbap object-oriented-programming-tutorials
Abap object-oriented-programming-tutorialscesarmendez78
 
Remote method invocation
Remote method invocationRemote method invocation
Remote method invocationDew Shishir
 
Intro To C++ - Class 07 - Headers, Interfaces, & Prototypes
Intro To C++ - Class 07 - Headers, Interfaces, & PrototypesIntro To C++ - Class 07 - Headers, Interfaces, & Prototypes
Intro To C++ - Class 07 - Headers, Interfaces, & PrototypesBlue Elephant Consulting
 
Ooabap notes with_programs
Ooabap notes with_programsOoabap notes with_programs
Ooabap notes with_programsKranthi Kumar
 
C++ classes tutorials
C++ classes tutorialsC++ classes tutorials
C++ classes tutorialsakreyi
 
Operator Overloading and Scope of Variable
Operator Overloading and Scope of VariableOperator Overloading and Scope of Variable
Operator Overloading and Scope of VariableMOHIT DADU
 
Remote Method Invocation
Remote Method InvocationRemote Method Invocation
Remote Method InvocationPaul Pajo
 
Bt0074 oops with java
Bt0074 oops with javaBt0074 oops with java
Bt0074 oops with javaTechglyphs
 
Distributed Programming using RMI
Distributed Programming using RMIDistributed Programming using RMI
Distributed Programming using RMIbackdoor
 
OBJECT ORIENTED PROGRAMMING LANGUAGE - SHORT NOTES
OBJECT ORIENTED PROGRAMMING LANGUAGE - SHORT NOTESOBJECT ORIENTED PROGRAMMING LANGUAGE - SHORT NOTES
OBJECT ORIENTED PROGRAMMING LANGUAGE - SHORT NOTESsuthi
 
Object oriented concepts & programming (2620003)
Object oriented concepts & programming (2620003)Object oriented concepts & programming (2620003)
Object oriented concepts & programming (2620003)nirajmandaliya
 
Introduction to Polymer and Firebase - Simon Gauvin
Introduction to Polymer and Firebase - Simon GauvinIntroduction to Polymer and Firebase - Simon Gauvin
Introduction to Polymer and Firebase - Simon GauvinSimon Gauvin
 
A Short Java RMI Tutorial
A Short Java RMI TutorialA Short Java RMI Tutorial
A Short Java RMI TutorialGuo Albert
 
Std 12 Computer Chapter 7 Java Basics (Part 1)
Std 12 Computer Chapter 7 Java Basics (Part 1)Std 12 Computer Chapter 7 Java Basics (Part 1)
Std 12 Computer Chapter 7 Java Basics (Part 1)Nuzhat Memon
 

Tendances (20)

Notes of java first unit
Notes of java first unitNotes of java first unit
Notes of java first unit
 
SMI - Introduction to Java
SMI - Introduction to JavaSMI - Introduction to Java
SMI - Introduction to Java
 
Abap object-oriented-programming-tutorials
Abap object-oriented-programming-tutorialsAbap object-oriented-programming-tutorials
Abap object-oriented-programming-tutorials
 
Remote method invocation
Remote method invocationRemote method invocation
Remote method invocation
 
Java Basics
Java BasicsJava Basics
Java Basics
 
Intro To C++ - Class 07 - Headers, Interfaces, & Prototypes
Intro To C++ - Class 07 - Headers, Interfaces, & PrototypesIntro To C++ - Class 07 - Headers, Interfaces, & Prototypes
Intro To C++ - Class 07 - Headers, Interfaces, & Prototypes
 
C by balaguruswami - e.balagurusamy
C   by balaguruswami - e.balagurusamyC   by balaguruswami - e.balagurusamy
C by balaguruswami - e.balagurusamy
 
Ooabap notes with_programs
Ooabap notes with_programsOoabap notes with_programs
Ooabap notes with_programs
 
C++ classes tutorials
C++ classes tutorialsC++ classes tutorials
C++ classes tutorials
 
Operator Overloading and Scope of Variable
Operator Overloading and Scope of VariableOperator Overloading and Scope of Variable
Operator Overloading and Scope of Variable
 
Remote Method Invocation
Remote Method InvocationRemote Method Invocation
Remote Method Invocation
 
Bt0074 oops with java
Bt0074 oops with javaBt0074 oops with java
Bt0074 oops with java
 
Distributed Programming using RMI
Distributed Programming using RMIDistributed Programming using RMI
Distributed Programming using RMI
 
OBJECT ORIENTED PROGRAMMING LANGUAGE - SHORT NOTES
OBJECT ORIENTED PROGRAMMING LANGUAGE - SHORT NOTESOBJECT ORIENTED PROGRAMMING LANGUAGE - SHORT NOTES
OBJECT ORIENTED PROGRAMMING LANGUAGE - SHORT NOTES
 
Object oriented concepts & programming (2620003)
Object oriented concepts & programming (2620003)Object oriented concepts & programming (2620003)
Object oriented concepts & programming (2620003)
 
DS
DSDS
DS
 
Introduction to Polymer and Firebase - Simon Gauvin
Introduction to Polymer and Firebase - Simon GauvinIntroduction to Polymer and Firebase - Simon Gauvin
Introduction to Polymer and Firebase - Simon Gauvin
 
A Short Java RMI Tutorial
A Short Java RMI TutorialA Short Java RMI Tutorial
A Short Java RMI Tutorial
 
Modular programming
Modular programmingModular programming
Modular programming
 
Std 12 Computer Chapter 7 Java Basics (Part 1)
Std 12 Computer Chapter 7 Java Basics (Part 1)Std 12 Computer Chapter 7 Java Basics (Part 1)
Std 12 Computer Chapter 7 Java Basics (Part 1)
 

Similaire à OO Programming Explained: Imperative vs Object-Oriented

Oop lec 4(oop design, style, characteristics)
Oop lec 4(oop design, style, characteristics)Oop lec 4(oop design, style, characteristics)
Oop lec 4(oop design, style, characteristics)Asfand Hassan
 
Presentation 1st
Presentation 1stPresentation 1st
Presentation 1stConnex
 
Object-oriented programming (OOP) with Complete understanding modules
Object-oriented programming (OOP) with Complete understanding modulesObject-oriented programming (OOP) with Complete understanding modules
Object-oriented programming (OOP) with Complete understanding modulesDurgesh Singh
 
Interoduction to c++
Interoduction to c++Interoduction to c++
Interoduction to c++Amresh Raj
 
Programming Fundamentals With OOPs Concepts (Java Examples Based)
Programming Fundamentals With OOPs Concepts (Java Examples Based)Programming Fundamentals With OOPs Concepts (Java Examples Based)
Programming Fundamentals With OOPs Concepts (Java Examples Based)indiangarg
 
Mastering Python lesson 4_functions_parameters_arguments
Mastering Python lesson 4_functions_parameters_argumentsMastering Python lesson 4_functions_parameters_arguments
Mastering Python lesson 4_functions_parameters_argumentsRuth Marvin
 
Chapter One Function.pptx
Chapter One Function.pptxChapter One Function.pptx
Chapter One Function.pptxmiki304759
 
Typescript language extension of java script
Typescript language extension of java scriptTypescript language extension of java script
Typescript language extension of java scriptmichaelaaron25322
 
Cble assignment powerpoint activity for moodle 1
Cble assignment powerpoint activity for moodle 1Cble assignment powerpoint activity for moodle 1
Cble assignment powerpoint activity for moodle 1LK394
 
OOP in C# Classes and Objects.
OOP in C# Classes and Objects.OOP in C# Classes and Objects.
OOP in C# Classes and Objects.Abid Kohistani
 
Class objects oopm
Class objects oopmClass objects oopm
Class objects oopmShweta Shah
 
Classes, objects and methods
Classes, objects and methodsClasses, objects and methods
Classes, objects and methodsfarhan amjad
 
iOS development introduction
iOS development introduction iOS development introduction
iOS development introduction paramisoft
 
Session 3 - Object oriented programming with Objective-C (part 1)
Session 3 - Object oriented programming with Objective-C (part 1)Session 3 - Object oriented programming with Objective-C (part 1)
Session 3 - Object oriented programming with Objective-C (part 1)Vu Tran Lam
 

Similaire à OO Programming Explained: Imperative vs Object-Oriented (20)

Oop lec 4(oop design, style, characteristics)
Oop lec 4(oop design, style, characteristics)Oop lec 4(oop design, style, characteristics)
Oop lec 4(oop design, style, characteristics)
 
Presentation 1st
Presentation 1stPresentation 1st
Presentation 1st
 
Object-oriented programming (OOP) with Complete understanding modules
Object-oriented programming (OOP) with Complete understanding modulesObject-oriented programming (OOP) with Complete understanding modules
Object-oriented programming (OOP) with Complete understanding modules
 
Intro To C++ - Class 14 - Midterm Review
Intro To C++ - Class 14 - Midterm ReviewIntro To C++ - Class 14 - Midterm Review
Intro To C++ - Class 14 - Midterm Review
 
Interoduction to c++
Interoduction to c++Interoduction to c++
Interoduction to c++
 
Programming Fundamentals With OOPs Concepts (Java Examples Based)
Programming Fundamentals With OOPs Concepts (Java Examples Based)Programming Fundamentals With OOPs Concepts (Java Examples Based)
Programming Fundamentals With OOPs Concepts (Java Examples Based)
 
Mastering Python lesson 4_functions_parameters_arguments
Mastering Python lesson 4_functions_parameters_argumentsMastering Python lesson 4_functions_parameters_arguments
Mastering Python lesson 4_functions_parameters_arguments
 
Chapter One Function.pptx
Chapter One Function.pptxChapter One Function.pptx
Chapter One Function.pptx
 
Typescript language extension of java script
Typescript language extension of java scriptTypescript language extension of java script
Typescript language extension of java script
 
Cble assignment powerpoint activity for moodle 1
Cble assignment powerpoint activity for moodle 1Cble assignment powerpoint activity for moodle 1
Cble assignment powerpoint activity for moodle 1
 
OOP in C# Classes and Objects.
OOP in C# Classes and Objects.OOP in C# Classes and Objects.
OOP in C# Classes and Objects.
 
Class 1 blog
Class 1 blogClass 1 blog
Class 1 blog
 
Inheritance
InheritanceInheritance
Inheritance
 
Class objects oopm
Class objects oopmClass objects oopm
Class objects oopm
 
My c++
My c++My c++
My c++
 
Classes, objects and methods
Classes, objects and methodsClasses, objects and methods
Classes, objects and methods
 
OOP Programs
OOP ProgramsOOP Programs
OOP Programs
 
iOS development introduction
iOS development introduction iOS development introduction
iOS development introduction
 
Overview of c#
Overview of c#Overview of c#
Overview of c#
 
Session 3 - Object oriented programming with Objective-C (part 1)
Session 3 - Object oriented programming with Objective-C (part 1)Session 3 - Object oriented programming with Objective-C (part 1)
Session 3 - Object oriented programming with Objective-C (part 1)
 

Dernier

APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...anjaliyadav012327
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...fonyou31
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 

Dernier (20)

APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 

OO Programming Explained: Imperative vs Object-Oriented

  • 1. An Introduction To Software Development Using C++ Class #5: Introduction To Classes, Objects, & Strings
  • 2. Let’s Talk About Art! Baroque Expressionist
  • 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

  1. 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.
  2. 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.