1 Welcome CSCI 3233.01 Object-Oriented Design and Programming Mon. and Wed., 6:00–8:29 p.m. ...
-
Upload
constance-cain -
Category
Documents
-
view
214 -
download
0
Transcript of 1 Welcome CSCI 3233.01 Object-Oriented Design and Programming Mon. and Wed., 6:00–8:29 p.m. ...
1
Welcome Welcome
CSCI 3233.01Object-Oriented Design and Programming• Mon. and Wed., 6:00–8:29 p.m.
Instructor: Charles Moen• Email – [email protected]• Web page – http://sce.cl.uh.edu/moenc/• Office – Delta Building 232• Office hours – Mon. and Wed. 5:30–6 and 8:30–9 p.m.
– Phone me at (281) 283-3848 so that I can open the hall door.
• Home – (713) 880-2924
2
CSCI 3233CSCI 3233
Understand the fundamentals of C++
Understand the elements of object-oriented design and programming in the context of C++
• Classes and objects• Encapsulation, inheritance• Using UML
Know advanced C++ techniques• Polymorphism• Exception handling• Templates
Develop programs using C++
3
Today’s ObjectivesToday’s Objectives
Class roster Course introduction
• Required text, web site, syllabus, schedule • How to succeed
Brief facts about C++ and object-oriented programming Using C++ Compilers at UHCL – Microsoft Visual Studio Introduction to C++ programming (Ch. 1 and Ch. 2)
• A simple example• Arithmetic operators, equality operators, relational operators
Introduction to classes and objects (Ch. 3)• Data members and member functions• Example: the GradeBook class
Next class – Intro to UML (Ch. 1) and Control Structures
31-May-200631-May-2006
5
Required TextbookRequired Textbook
H. M. Deitel and P. J. Deitel, C++ How to Program, Fifth Edition. Upper Saddle River, NJ: Prentice Hall, 2005. ISBN 0-13-185757-6
Textbook web page:http://www.deitel.com/books/cppHTP5/index.html
A recommended book (not required):
Ann R. Ford and Toby J. Teorey, Practical Debugging in C++. Upper Saddle River, NJ: Prentice Hall, 2002. ISBN 0-13-065394-2
Course IntroductionCourse Introduction
6
Course Web PagesCourse Web Pages
Course pagehttp://sce.cl.uh.edu/moenc/csci3233summer06/index.html
Schedulehttp://sce.cl.uh.edu/moenc/csci3233summer06/schedule.html
Syllabushttp://sce.cl.uh.edu/moenc/csci3233summer06/syllabus.html
Resourceshttp://sce.cl.uh.edu/moenc/csci3233summer06/resources.html
Course IntroductionCourse Introduction
7
How to SucceedHow to Succeed
Read the chapter before class Participate
• In class and on the discussion board Learn object-oriented programming by
writing a lot of small programs• To learn C++ programming skills• To apply those skills in problem solving
Start the assignments early Invest time and work hard!
Course IntroductionCourse Introduction
8
Bonus LabsBonus Labs
Optional guided lab assignment• At least 5 scheduled during the semester• During regular class time, but at the end of the period• Simple assignments that will give you some hands-on C++ experience
Procedure• First, a demo by the instructor• Then you will complete the assignment on your own• Print it and hand it in before the end of class
Rules• Do your own work, but you may help each other• You may ask the instructor for help• You may leave if you finish early or if you do not wish to do this
assignment
1 bonus point added to a quiz grade for each lab correctly completed and handed in before the end of class
Course IntroductionCourse Introduction
10
Historical PerspectiveHistorical Perspective
1968 – The “software crisis”• NATO Software Engineering Conference in
Garmisch, Germany• Late projects, high costs, code was hard to
maintain• Ad hoc programming
Structured programming• Adapted in the late 1960s to produce programs
that were easy to understand, develop, test, and modify
C++ and Object-Oriented Programming (Deitel; Randell; Wirth)C++ and Object-Oriented Programming (Deitel; Randell; Wirth)
11
Structured ProgrammingStructured Programming
Programs should be composed of pieces that have only one starting point and one terminating point
Use only three kinds of “pieces”1. Sequence2. Selection control structures
– e.g., if, if/else, switch3. Repetition control structures
– e.g., while, do/while, for
C++ and Object-Oriented Programming (Deitel; Wirth)C++ and Object-Oriented Programming (Deitel; Wirth)
12
Object-Oriented ProgrammingObject-Oriented Programming
Became widely used in the 1990s• Provided the foundation for real progress in creating software
that is easy to understand, develop, test, and modify
A way of solving a problem by writing a program that models the elements in the problem space as objects
• For example, a program used to support a video rental business would have objects such as a store, videos, and customers
• Programs are collections of objects• Each object can provide a set of services• Objects interact by sending messages telling each other what
to do
C++ and Object-Oriented Programming (Deitel; Eckel)C++ and Object-Oriented Programming (Deitel; Eckel)
13
Object-Oriented ProgrammingObject-Oriented Programming
Some advantages• Easy to understand and develop because the objects directly
model real-world elements• Faster development• Easier to maintain• Object-oriented software is easier to reuse
Object-based programming• Class – program element that contains both data and the
functions that manipulate it• Object –instance of a class (like a “variable”)
Object-oriented programming• Inheritance• Polymorphism
C++ and Object-Oriented Programming (Deitel)C++ and Object-Oriented Programming (Deitel)
14
Brief Facts About C++Brief Facts About C++
Evolved from C Designed and implemented by Bjarne Stroustrup
at the Bell Labs in the early 1980s “C with classes” Standardized by ISO in 1997
• Includes the C++ standard library• Standard Template Library (STL)
– Part of the C++ standard library– Readymade classes for data structures and algorithms
C++ and Object-Oriented Programming (Deitel; Josuttis)C++ and Object-Oriented Programming (Deitel; Josuttis)
16
CompilersCompilers
Microsoft Visual Studio• IDE (Integrated Development Environment)• PC Lab (Delta Bldg., second floor)
– Available in MS Visual Studio .NET (MSVS)• Home use
– MSVS can be purchased from David Webb– Delta D120 to purchase it
GNU g++ compiler• Sun Lab (Delta Bldg., first floor)• Home use
– Log in using Telnet– diamond.rocks.cl.uh.edu
Using C++ Compilers at UHCLUsing C++ Compilers at UHCL
17
Using Microsoft Visual StudioUsing Microsoft Visual Studio
http://sce.cl.uh.edu/moenc/usingMSVS.html
Using C++ Compilers at UHCLUsing C++ Compilers at UHCL
19
A Simple ExampleA Simple Example
Page 42, Fig. 2.4 C++ features
• Comments• Include directive• main()• Statements end with semicolons ;• cout and << for output• std namespace• Escape characters, e.g. \n
Introduction to C++ Programming (Deitel)Introduction to C++ Programming (Deitel)
201 // Fig. 2.4: fig02_04.cpp2 // A first program in C++.3 #include <iostream>4 5 // function main begins program execution6 int main()7 {8 std::cout << "Welcome to C++!\n";9 10 return 0; // indicate that program ended successfully11 12 } // end function main
Welcome to C++!
Single-line comments.
Preprocessor directive to include input/output stream header file <iostream>.
Function main appears exactly once in every C++ program..
Function main returns an integer value.Left brace { begins function body.
Corresponding right brace } ends function body.
Statements end with a semicolon ;.
Name cout belongs to namespace std.
Stream insertion operator.
Keyword return is one of several means to exit function; value 0 indicates program terminated successfully.
21
Another ExampleAnother Example
Page 43, Fig. 2.5 C++ features
• Declaring variables• Input using cin and >>• C++ supports many arithmetic operators
+, -, *, /, %Precedence, p. 33: parentheses first; then *,/, and %; and last are + and –. All from left to right.
• endl to output a newline and flush output buffer
• << can be concatenated
Introduction to C++ Programming (Deitel)Introduction to C++ Programming (Deitel)
22
1 // Fig. 2.5: fig02_05.cpp2 // Addition program.3 #include <iostream>4 5 // function main begins program execution6 int main()7 {8 int integer1; // first number to be input by user 9 int integer2; // second number to be input by user 10 int sum; // variable in which sum will be stored11 12 std::cout << "Enter first integer\n"; // prompt13 std::cin >> integer1; // read an integer14 15 std::cout << "Enter second integer\n"; // prompt16 std::cin >> integer2; // read an integer17 18 sum = integer1 + integer2; // assign result to sum19 20 std::cout << "Sum is " << sum << std::endl; // print sum21 22 return 0; // indicate that program ended successfully23 24 } // end function main
Declare integer variables.
Use stream extraction operator with standard input stream to obtain user input.
Stream manipulator std::endl outputs a newline, then “flushes output buffer.”
Concatenating, chaining or cascading stream insertion operations.
Calculations can be performed in output statements: alternative for lines 18 and 20:
std::cout << "Sum is " << integer1 + integer2 << std::endl;
23
Equality Operators andRelational Operators
Equality Operators andRelational Operators
Page 53, Fig. 2.13 C++ features
• if• equality operator == (don’t confuse with = )• operator !=• operator <• operator >• operator <=• operator >=
Introduction to C++ Programming (Deitel)Introduction to C++ Programming (Deitel)
25
ClassClass
A programming construct that contains
• “Data elements” that are related – they may have different data types
• “Member functions” – all the operations that can be performed with the data
Definition• Class = a collection of related elements with
different data types and the operations that can be performed on them
Introduction to Classes and Objects (Deitel, 76)Introduction to Classes and Objects (Deitel, 76)
26
The GradeBook ClassThe GradeBook Class
Example of a class in Ch. 3
Problem: We want to write a program that a teacher can use to keep track of students’ test scores
Solution: Create a GradeBook class that contains the data and the functions for keeping track of the scores
• In this first example, the GradeBook class will:– Store the name of the course in memory– It also will have member functions to initialize the course name,
set the name, and print a message containing the name
• Later we’ll add the following features:– Stores the students’ test scores in memory– Sets the scores, displays the scores, calculates the class average
Introduction to Classes and Objects (Deitel, 77–99)Introduction to Classes and Objects (Deitel, 77–99)
27
Declaring a ClassDeclaring a Class
class GradeBook{public: GradeBook(string name="CSCI 3233"); void setCourseName(string name); string getCourseName(); void displayMessage();private: string courseName;};
Name
Data Member
Remember the ‘;’ !
Member functions(also calledoperations)
Introduction to Classes and Objects (Deitel, 96)Introduction to Classes and Objects (Deitel, 96)
28
Access SpecifiersAccess Specifiers
class GradeBook{public: GradeBook(string name="CSCI 3233"); void setCourseName(string name); string getCourseName(); void displayMessage();private: string courseName;};
Introduction to Classes and Objects (Deitel, 96)Introduction to Classes and Objects (Deitel, 96)
Access specifier All members after “public:” are accessible by any user in your program wherever there is an object of this class.
Access specifierAll members after “private:” are only accessible to the member functions of this class.
Class members are private by default.
29
Information HidingInformation Hiding
Data members are always made private• The user of an object in your program cannot change the data by
accessing it directly• So that we can control how the data can be changed
– Make sure that the new data is okay– Verify that a requested change is allowed– Example: An “courseID” member has to be an non-negative integer,
and it should not be a number that is already in use
• Also, so we can control how we store the data inside the class– Example: We may decide to store the ID as a string instead of an int
The user of an object can still get the data and set it, but only by using a member function
• Example: void setCourseID( int id );
Introduction to Classes and Objects (Deitel, 76)Introduction to Classes and Objects (Deitel, 76)
30
Get and Set Member FunctionsGet and Set Member Functions
Public member functions are provided to give users access to the private data
Member functions that return the data values• Called “accessors”• Usually named with the prefix “get”
string getCourseName();
Member functions that change the data values• Usually named with the prefix “set”
void setCourseName();• Must ensure that the new value is appropriate• May need to convert the argument to a different type
Introduction to Classes and Objects (Deitel, 76)Introduction to Classes and Objects (Deitel, 76)
31
The Public InterfaceThe Public Interface
All of the public member functions are collectively called the “public interface” of the class
Includes all the operations that a user can do with the class – sometimes called the “services”
As long as the interface to your class never changes, you can change the implementation inside your class and your program will not break – easy to improve your program
• You can change the way the data is stored, e.g. from an unsorted array to a sorted array
• You can change the way the operation is performed, e.g. finding a target with a binary search instead of a linear search
Introduction to Classes and Objects (Deitel, 99)Introduction to Classes and Objects (Deitel, 99)
32
EncapsulationEncapsulation
One of the principle advantages of object-oriented programming
Means that we keep both the data and the operations that can be done with that data inside a single entity, the class
In addition, the user of the class has an outside view, and has access only to the operations.
• The user knows what the class can do because the names of the operations are public.
• But the user does not know how it is done because the implementation of the class is not accessible.
• The programmer has the freedom to implement the operations and change them in any way, as long as the public interface does not change.
Introduction to Classes and Objects (Deitel, 90)Introduction to Classes and Objects (Deitel, 90)
33
Defining the Member FunctionsDefining the Member Functions
class GradeBook{public: GradeBook(string name="CSCI 3233"){ setCourseName(name); } void setCourseName(string name){ courseName = name; } string getCourseName(){ return courseName; } void displayMessage(){ cout << "GradeBook for " << getCourseName() << endl; }private: string courseName;};
Introduction to Classes and Objects (Deitel, 96)Introduction to Classes and Objects (Deitel, 96)
34
Separatingthe Class from main()
Separatingthe Class from main()
In C++, this is the normal approach• The class declaration and definition goes in a header
file (GradeBook.h)• A preprocessor directive includes it in the cpp file
#include "GradeBook.h"
Advantage• Reusability – if the GradeBook class is in its own file,
it can be used in any program where we need it, just by “including it”
Not required – it is legal to put the class in the same file as main()
Introduction to Classes and Objects (Deitel, 95)Introduction to Classes and Objects (Deitel, 95)
35
Using a Class to Create an ObjectUsing a Class to Create an Object
After a class is declared, it defines a new data type
We can use it to “declare a variable,” but we use a different terminology. We say that a class is used to create an “object” that is an instance of the class.
Instantiating an objectint main(){ GradeBook myGradeBook;} Object
Introduction to Classes and Objects (Deitel, 80)Introduction to Classes and Objects (Deitel, 80)
36
ConstructorConstructor
Public member function with the same name as the class and with no return value
Called automatically whenever an object is instantiated
Initializes the data members
Often, there are several overloaded constructors
GradeBook(){ setCourseName("CSCI 3233"); }
GradeBook(string name){ setCourseName(name); }
Introduction to Classes and Objects (Deitel, 93)Introduction to Classes and Objects (Deitel, 93)
37
The Main FunctionThe Main Function
#include <iostream>#include "GradeBook.h"using namespace std;
int main(){
GradeBook myFirstGradeBook; cout << "GradeBook created for " << myFirstGradeBook.getCourseName() << endl;
GradeBook mySecondGradeBook("CSCI 3333"); cout << "GradeBook created for " << mySecondGradeBook.getCourseName() << endl;}
Introduction to Classes and Objects (Deitel, 103)Introduction to Classes and Objects (Deitel, 103)
38
ReferencesReferences
Deitel, H. M., and P. J. Deitel, C++ How to Program, Fifth Edition. Upper Saddle River, NJ: Prentice Hall, 2005.
Eckel, B., Thinking in C++, Second Edition. Upper Saddle River, NJ: Prentice Hall, 2002.
Goodrich, M. T., R. Tamassia, and D. Mount, Data Structures and Algorithms in C++. Hoboken, NJ: John Wiley & Sons, Inc., 2004.
Josuttis, Nicolai M., The C++ Standard Library, A Tutorial and Reference. Boston: Addison-Wesley, 1999.
Randell, B., “Software Engineering in 1968,” Proceedings of the 4th International Conference on Software Engineering. Piscataway, NJ: IEEE Press, 1979.
Wirth, N., “On the Composition of Well-Structured Programs,” Computing Surveys. Vol. 6, No. 4, December 1974.