1 / 38

Welcome

Welcome. CSCI 3233.01 Object-Oriented Design and Programming Mon. and Wed., 6:00–8:29 p.m. Instructor: Charles Moen Email – crmoen@juno.com 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.

urbano
Download Presentation

Welcome

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Welcome • CSCI 3233.01Object-Oriented Design and Programming • Mon. and Wed., 6:00–8:29 p.m. • Instructor: Charles Moen • Email – crmoen@juno.com • 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 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. 31-May-2006 Today’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

  4. Course Introduction

  5. Course Introduction Required 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

  6. Course Introduction Course Web Pages • Course page http://sce.cl.uh.edu/moenc/csci3233summer06/index.html • Schedule http://sce.cl.uh.edu/moenc/csci3233summer06/schedule.html • Syllabus http://sce.cl.uh.edu/moenc/csci3233summer06/syllabus.html • Resources http://sce.cl.uh.edu/moenc/csci3233summer06/resources.html

  7. Course Introduction How 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!

  8. Course Introduction Bonus 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

  9. C++ andObject-Oriented Programming Some brief facts

  10. C++ and Object-Oriented Programming (Deitel; Randell; Wirth) Historical 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

  11. C++ and Object-Oriented Programming (Deitel; Wirth) Structured Programming • Programs should be composed of pieces that have only one starting point and one terminating point • Use only three kinds of “pieces” • Sequence • Selection control structures • e.g., if, if/else, switch • Repetition control structures • e.g., while, do/while, for

  12. C++ and Object-Oriented Programming (Deitel; Eckel) Object-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

  13. C++ and Object-Oriented Programming (Deitel) Object-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

  14. C++ and Object-Oriented Programming (Deitel; Josuttis) 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

  15. Using C++ Compilers at UHCL

  16. Using C++ Compilers at UHCL Compilers • 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

  17. Using C++ Compilers at UHCL Using Microsoft Visual Studio http://sce.cl.uh.edu/moenc/usingMSVS.html

  18. Introduction toC++ Programming C++ Features in Chapter 1

  19. Introduction to C++ Programming (Deitel) A 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

  20. 1 // Fig. 2.4: fig02_04.cpp 2 // A first program in C++. 3 #include <iostream> 4 5 // function main begins program execution 6 int main() 7 { 8 std::cout << "Welcome to C++!\n"; 9 10 return0; // indicate that program ended successfully 11 12 } // end function main Function main returns an integer value. Preprocessor directive to include input/output stream header file <iostream>. Left brace { begins function body. Function main appears exactly once in every C++ program.. Statements end with a semicolon ;. Corresponding right brace } ends function body. Stream insertion operator. Single-line comments. Name cout belongs to namespace std. Keyword return is one of several means to exit function; value 0 indicates program terminated successfully. Welcome to C++!

  21. Introduction to C++ Programming (Deitel) Another 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

  22. 1 // Fig. 2.5: fig02_05.cpp 2 // Addition program. 3 #include <iostream> 4 5 // function main begins program execution 6 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 stored 11 12 std::cout << "Enter first integer\n"; // prompt 13 std::cin >> integer1; // read an integer 14 15 std::cout << "Enter second integer\n"; // prompt 16 std::cin >> integer2; // read an integer 17 18 sum = integer1 + integer2; // assign result to sum 19 20 std::cout << "Sum is " << sum << std::endl; // print sum 21 22 return0; // indicate that program ended successfully 23 24 } // end function main Declare integer variables. Use stream extraction operator with standard input stream to obtain user input. Calculations can be performed in output statements: alternative for lines 18 and 20: std::cout << "Sum is " << integer1 + integer2 << std::endl; Stream manipulator std::endl outputs a newline, then “flushes output buffer.” Concatenating, chaining or cascading stream insertion operations.

  23. Introduction to C++ Programming (Deitel) Equality Operators andRelational Operators • Page 53, Fig. 2.13 • C++ features • if • equality operator == (don’t confuse with = ) • operator != • operator < • operator > • operator <= • operator >=

  24. Introduction toClasses and Objects Chapter 3

  25. Introduction to Classes and Objects (Deitel, 76) Class • 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

  26. Introduction to Classes and Objects (Deitel, 77–99) The 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

  27. Introduction to Classes and Objects (Deitel, 96) Declaring a Class Name class GradeBook{ public: GradeBook(string name="CSCI 3233"); void setCourseName(string name); string getCourseName(); void displayMessage(); private: string courseName; }; Member functions (also called operations) Data Member Remember the ‘;’ !

  28. Introduction to Classes and Objects (Deitel, 96) Access Specifiers Access specifier All members after “public:” are accessible by any user in your program wherever there is an object of this class. class GradeBook{ public: GradeBook(string name="CSCI 3233"); void setCourseName(string name); string getCourseName(); void displayMessage(); private: string courseName; }; All members after “private:” are only accessible to the member functions of this class. Access specifier Class members are private by default.

  29. Introduction to Classes and Objects (Deitel, 76) Information 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 );

  30. Introduction to Classes and Objects (Deitel, 76) Get 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

  31. Introduction to Classes and Objects (Deitel, 99) The 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

  32. Introduction to Classes and Objects (Deitel, 90) Encapsulation • 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.

  33. Introduction to Classes and Objects (Deitel, 96) Defining 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; };

  34. Introduction to Classes and Objects (Deitel, 95) 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()

  35. Introduction to Classes and Objects (Deitel, 80) Using 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 object int main(){ GradeBook myGradeBook; } Object

  36. Introduction to Classes and Objects (Deitel, 93) Constructor • 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); }

  37. Introduction to Classes and Objects (Deitel, 103) The 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; }

  38. References 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.

More Related