Download presentation
Presentation is loading. Please wait.
Published byDelilah Ackland Modified over 9 years ago
1
Object-Oriented Programming Using C++ Third Edition Chapter 1 An Overview of Object-Oriented Programming and C++
2
Object-Oriented Programming Using C++, Third Edition2 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
3
Object-Oriented Programming Using C++, Third Edition3 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
4
Object-Oriented Programming Using C++, Third Edition4 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
5
Object-Oriented Programming Using C++, Third Edition5 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
6
Object-Oriented Programming Using C++, Third Edition6 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
7
Object-Oriented Programming Using C++, Third Edition7 Programming Universals (continued) Data type: defines what 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
8
Object-Oriented Programming Using C++, Third Edition8 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
9
Object-Oriented Programming Using C++, Third Edition9 Early Procedural Programs
10
Object-Oriented Programming Using C++, Third Edition10 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
11
Object-Oriented Programming Using C++, Third Edition11 Selection Structure
12
Object-Oriented Programming Using C++, Third Edition12 Loop Structure
13
Object-Oriented Programming Using C++, Third Edition13 Modularity and Abstraction Modules: functions, procedures, methods, subprograms, subroutines, or simply routines
14
Object-Oriented Programming Using C++, Third Edition14 Modularity and Abstraction (continued) Using the method’s name to cause execution of the statements within the method is known as calling a method
15
Object-Oriented Programming Using C++, Third Edition15 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
16
Object-Oriented Programming Using C++, Third Edition16 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
17
Object-Oriented Programming Using C++, Third Edition17 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
18
Object-Oriented Programming Using C++, Third Edition18 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
19
Object-Oriented Programming Using C++, Third Edition19 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
20
Object-Oriented Programming Using C++, Third Edition20 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
21
Object-Oriented Programming Using C++, Third Edition21 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
22
Object-Oriented Programming Using C++, Third Edition22 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
23
Object-Oriented Programming Using C++, Third Edition23 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 camel casing
24
Object-Oriented Programming Using C++, Third Edition24
25
Object-Oriented Programming Using C++, Third Edition25 Simple Data Types in C++ int –For example, 4, -7, 15000 –Also, short int and long 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
26
Object-Oriented Programming Using C++, Third Edition26 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; } Or: int myAge, yourAge;
27
Object-Oriented Programming Using C++, Third Edition27 Declaring Variables (continued) Lvalue
28
Object-Oriented Programming Using C++, Third Edition28 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)
29
Object-Oriented Programming Using C++, Third Edition29 Creating Comments
30
Object-Oriented Programming Using C++, Third Edition30 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
31
Object-Oriented Programming Using C++, Third Edition31 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 Namespace: mechanism for grouping features you want to include in a program –For example, using namespace std;
32
Object-Oriented Programming Using C++, Third Edition32
33
Object-Oriented Programming Using C++, Third Edition33 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";
34
Object-Oriented Programming Using C++, Third Edition34 Producing C++ Output (continued)
35
Object-Oriented Programming Using C++, Third Edition35 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
36
Object-Oriented Programming Using C++, Third Edition36 Providing C++ Input (continued)
37
Object-Oriented Programming Using C++, Third Edition37 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
38
Object-Oriented Programming Using C++, Third Edition38 A First Look at Data Structures and Classes (continued) fields are public fields are private
39
Object-Oriented Programming Using C++, Third Edition39
40
Object-Oriented Programming Using C++, Third Edition40 You Do It
41
Object-Oriented Programming Using C++, Third Edition41 Modifying a Program to Accept Input Values cout<<"Please enter your credit hours "; cin>>creditHours; cout<<"Please enter your grade point average "; cin>>gradePointAverage;
42
Object-Oriented Programming Using C++, Third Edition42 Creating a Simple Structure struct Student { int creditHours; double gradePointAverage; };
43
Object-Oriented Programming Using C++, Third Edition43 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
44
Object-Oriented Programming Using C++, Third Edition44 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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.