1 / 44

Object-Oriented Programming Using C++ Third Edition

Object-Oriented Programming Using C++ Third Edition. Chapter 1 An Overview of Object-Oriented Programming and C++. Objectives. Learn about the task of programming Examine programming universals Explore procedural programming Be introduced to object-oriented programming

idra
Télécharger la présentation

Object-Oriented Programming Using C++ Third Edition

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. Object-Oriented Programming Using C++Third Edition Chapter 1 An Overview of Object-Oriented Programming and C++

  2. Objectives • Learn about the task of programming • Examine programming universals • Explore procedural programming • Be introduced to object-oriented programming • Get started in the C++ programming environment Object-Oriented Programming Using C++, Third Edition

  3. Objectives (continued) • Work with variables and the const qualifier • Create comments • Examine the differences between ANSI/ISO C++ and Standard C++ • Produce C++ output with cout and provide input with cin • Begin to work with data structures and classes Object-Oriented Programming Using C++, Third Edition

  4. The Task of Programming • Programming: writing instructions that enable a computer to carry out tasks • Programs are frequently called applications • Learning a computer programming language requires learning both vocabulary and syntax • The rules of any language make up its syntax • Types of errors: • Syntax errors • Logical errors • Semantic errors Object-Oriented Programming Using C++, Third Edition

  5. The Task of Programming (continued) • Machine language: language that computers can understand; it consists of 1s and 0s • Interpreter: program that translates programming language instructions one line at a time • Compiler: translates entire program at one time • Run a program by issuing a command to execute the program statements • Test a program by using sample data to determine whether the program results are correct Object-Oriented Programming Using C++, Third Edition

  6. Programming Universals • Language provides methods for directing output to an object and for sending input into the program • Variables: named locations in computer memory • “What is yourAge?” • Languages define rules for naming variables • Should have meaningful names • May have only one value at a time • Must be explicitly declared sometimes Object-Oriented Programming Using C++, Third Edition

  7. Programming Universals (continued) • Data type: defineswhat kind of values may be stored in a variable and what kind of operations can be performed on it • Numeric • -6 • Character • ‘&’ • Floating point • 37.56 • Some languages let you create your own types Object-Oriented Programming Using C++, Third Edition

  8. Procedural Programming • Procedural programs: consist of a series of steps or procedures that take place one after the other • Procedural Languages: • COBOL • BASIC • FORTRAN • RPG • C++ • Procedural programming techniques have evolved into object-oriented techniques Object-Oriented Programming Using C++, Third Edition

  9. Early Procedural Programs Object-Oriented Programming Using C++, Third Edition

  10. Control Structures • Control structures: logic components in programs • Sequence structure: steps execute one after another, without interruption • Selection structure: used to perform different tasks based on a condition • Loop structure: repeats actions while some condition remains unchanged Object-Oriented Programming Using C++, Third Edition

  11. Selection Structure Object-Oriented Programming Using C++, Third Edition

  12. Loop Structure Object-Oriented Programming Using C++, Third Edition

  13. Modularity and Abstraction • Modules: functions, procedures, methods, subprograms, subroutines, or simply routines Object-Oriented Programming Using C++, Third Edition

  14. Modularity and Abstraction (continued) • Using the method’s name to cause execution of the statements within the method is known as calling a method Object-Oriented Programming Using C++, Third Edition

  15. Modularity and Abstraction (continued) • The program in Figure 1-6 is more concise that the previous program; it is also more abstract • Abstraction: paying attention to important properties while ignoring details Object-Oriented Programming Using C++, Third Edition

  16. Encapsulation • The variables and instructions within a module are encapsulated, which helps make the module independent of other modules and reusable • You can interact with an encapsulated module by using its interface, without knowing its inner details • Reusing existing systems improves reliability • To call a module, you need to know some details • A better approach is object-oriented programming Object-Oriented Programming Using C++, Third Edition

  17. Object-Oriented Programming • Objects have attributes and can take actions • You can pass messages to objects, so that they take action • The same message works differently when applied to the various objects • A method can work appropriately with different types of data • Objects can inherit traits of previously created objects • Information hiding is more complete than in procedural programs Object-Oriented Programming Using C++, Third Edition

  18. Objects and Classes • An object is any thing • A class consists of a category of things • An object is an instance of a class • Is-a relationship: • “myBlueCerealBowl is a Dish” • Convention is to begin object names with a lowercase letter and class names with an uppercase letter Object-Oriented Programming Using C++, Third Edition

  19. Inheritance • Classes are extensible • You can create new classes that extend or are descendents of existing classes • The descendent classes can inherit all the attributes of the parent class, or they can override inappropriate attributes • In geometry, a Cube is a descendent of a Square • A Cube has all of a Square’s attributes, plus one additional characteristic: depth Object-Oriented Programming Using C++, Third Edition

  20. Polymorphism • Programming modules are sometimes needed to change the way they operate depending on the context • Object-oriented programs use polymorphism to carry out the same operation in a manner customized to the object • Without polymorphism, you would have to create separate module names for a method that cleans a Dish object, one that cleans a Car object, and one that cleans a Baby object • With polymorphism, you create a single “clean” method Object-Oriented Programming Using C++, Third Edition

  21. Getting Started in the C++ Programming Environment • Use an editor to type your source code • Compile a program to transform it to machine language • Produces object code • An executable program needs the object code and code from outside sources to which it refers • Integrating these outside references is called linking • When compiling, error messages or warnings may appear Object-Oriented Programming Using C++, Third Edition

  22. Creating a main() Function • Function parts: header and body • Function header: • Return type of the function • Name of the function • Types and names of any variables enclosed in parentheses, and which the function receives Object-Oriented Programming Using C++, Third Edition

  23. camel casing Working with Variables and the const Qualifier • Must provide an identifier to each variable before you can use it • Also provide identifiers for functions, structures, and classes • Identifiers can include letters, numbers, and underscores, but they must begin with a letter or underscore • Examples: Age, lastName, tax_2006, ready2go, salary, Salary, and SALARY Object-Oriented Programming Using C++, Third Edition

  24. Object-Oriented Programming Using C++, Third Edition

  25. Simple Data Types in C++ • int • For example, 4, -7, 15000 • Also, short intandlong int • char • For example, ‘A’ or ‘&’ • bool • For example, true or false • Some older compilers do not support this data type • float, double, and long double • For example, 12.25 Object-Oriented Programming Using C++, Third Edition

  26. Or:int myAge, yourAge; Declaring Variables • Variables may be declared anywhere but cannot be used until after they are declared int main() { int myAge; int yourAge; char myMiddleInitial; double myMoney, yourMoney; } Object-Oriented Programming Using C++, Third Edition

  27. Lvalue Declaring Variables (continued) Object-Oriented Programming Using C++, Third Edition

  28. The const Qualifier • A quantity that does not change in a program should not be declared as a variable • Instead, it should be a named constant const double MINIMUM_WAGE = 5.75; • const is a qualifier: a word that qualifies, or restricts, the ordinary capabilities of the named type (such as double) Object-Oriented Programming Using C++, Third Edition

  29. Creating Comments Object-Oriented Programming Using C++, Third Edition

  30. ANSI/ISO Standard C++ • C++ evolved from a language named C • 1980s: C++ was designed by Bjarne Stoustrup at Bell Labs • Several compilers were developed for C++, and the language evolved in slightly different ways • 1990s: a joint committee of the American National Standards Institute (ANSI) and the International Standard Organization (ISO) standardized the syntax • ANSI/ISO Standard • Supported by most newer compilers Object-Oriented Programming Using C++, Third Edition

  31. Using Libraries, Preprocessor Directives, and namespace • Header files: files that contain predefined values and routines, such as sqrt() • Usually have no extension or end in .h • Must include a preprocessor directive in the program • Preprocessor directives begin with a pound sign (#) • #include preprocessor directive tells the compiler to include a file as part of the finished product • For example, #include <iostream> • Namespace: mechanism for grouping features you want to include in a program • For example, using namespace std; Object-Oriented Programming Using C++, Third Edition

  32. Object-Oriented Programming Using C++, Third Edition

  33. Producing C++ Output • C++ provides several objects for producing output • The simplest object is called cout • The name comes from Console OUTput • For example, cout<<"Hi there"; • Must include iostream • And using namespace std; in ANSI/ISO C++ • You can use ’\n’ or endl to insert a newline character • cout<<"Hi"<<endl<<"there"; Object-Oriented Programming Using C++, Third Edition

  34. Producing C++ Output (continued) Object-Oriented Programming Using C++, Third Edition

  35. Providing C++ Input • Interactive programs must provide a way for the user to enter responses to program prompts • The cin object fetches values from the keyboard • Used with the extraction operator (>>) • For example, cin>>quantity; • Must include iostream • Can enter more than one value: int score1, score2, score3; cout<<"Please enter 3 scores. Use a space between them. "; cin>>score1>>score2>>score3; • Whitespace consists of any number of spaces, tabs, and Enter characters Object-Oriented Programming Using C++, Third Edition

  36. Providing C++ Input (continued) Object-Oriented Programming Using C++, Third Edition

  37. A First Look at Data Structures and Classes • Primitive or scalar types: int, char, double • C++ supports two ways to create your own complex data types: structures and classes • For example, you might create an Employee structure or class with components such as firstName, lastName, hourlySalary, numberOfDependents, and hireDate • The relationship between these fields of the Employee class is often called a has-a relationship Object-Oriented Programming Using C++, Third Edition

  38. fields are private fields are public A First Look at Data Structures and Classes (continued) Object-Oriented Programming Using C++, Third Edition

  39. Object-Oriented Programming Using C++, Third Edition

  40. You Do It Object-Oriented Programming Using C++, Third Edition

  41. Modifying a Program to Accept Input Values cout<<"Please enter your credit hours "; cin>>creditHours; cout<<"Please enter your grade point average "; cin>>gradePointAverage; Object-Oriented Programming Using C++, Third Edition

  42. Creating a Simple Structure struct Student { int creditHours; double gradePointAverage; }; Object-Oriented Programming Using C++, Third Edition

  43. Summary • Programming a computer includes: • Learning the syntax of a programming language • Resolving logical errors • Programming languages provide methods for input and output of variable values • Procedural programs consist of a series of steps or procedures that take place one after the other • Object-oriented programming adds several new programming concepts including objects, classes, inheritance, and polymorphism Object-Oriented Programming Using C++, Third Edition

  44. Summary (continued) • You write a C++ program by typing source code into an editor and compiling the program • C++ modules are called functions, and each function contains a header and a body • C++ variables must be given a type and a name • Comments are nonexecuting program statements • A preprocessor directive tells the compiler to do something before compiling the program • Use cout and cin to display and read values • When you create a data structure or class, you create your own C++ data type Object-Oriented Programming Using C++, Third Edition

More Related