37
1 Object-Oriented Program Development Using C++ Computer Science and Programming Languages Computers are ubiquitous Computer literacy is essential Computer science: computers as an object of study Hardware Software Theory Algorithm development: the core of computer science

1Object-Oriented Program Development Using C++ Computer Science and Programming Languages Computers are ubiquitous Computer literacy is essential Computer

Embed Size (px)

Citation preview

Page 1: 1Object-Oriented Program Development Using C++ Computer Science and Programming Languages Computers are ubiquitous Computer literacy is essential Computer

1Object-Oriented Program Development Using C++

Computer Science and Programming Languages

• Computers are ubiquitous• Computer literacy is essential• Computer science: computers as an object of study

– Hardware

– Software

– Theory

• Algorithm development: the core of computer science

Page 2: 1Object-Oriented Program Development Using C++ Computer Science and Programming Languages Computers are ubiquitous Computer literacy is essential Computer

2Object-Oriented Program Development Using C++

A Brief History of Computers• Computational tools to manage time and space

– Stone tablets and the abacus• Mechanical calculators from Pascal and Leibniz• Nineteenth century developments

– The Jacquard Loom – Babbage’s Analytical Engine

• Mid-twentieth century– The ABC (Atanasoff-Berry Computer)– ENIAC, MARK I and EDSAC

Page 3: 1Object-Oriented Program Development Using C++ Computer Science and Programming Languages Computers are ubiquitous Computer literacy is essential Computer

3Object-Oriented Program Development Using C++

Figure 1-1Charles Babbage’s Analytical Engine

Page 4: 1Object-Oriented Program Development Using C++ Computer Science and Programming Languages Computers are ubiquitous Computer literacy is essential Computer

4Object-Oriented Program Development Using C++

Figure 1-2ENIAC (Courtesy IBM archives)

Page 5: 1Object-Oriented Program Development Using C++ Computer Science and Programming Languages Computers are ubiquitous Computer literacy is essential Computer

5Object-Oriented Program Development Using C++

Computer Hardware

• Hardware: physical components of computer• Von Neumann machine (architecture)

– Main memory unit

– Secondary Storage Devices

– Central Processing Unit (CPU)

– Input/output (I/O) devices

• Bit: fundamental bi-valued unit of memory• Byte: grouping of eight bits

Page 6: 1Object-Oriented Program Development Using C++ Computer Science and Programming Languages Computers are ubiquitous Computer literacy is essential Computer

6Object-Oriented Program Development Using C++

Figure 1-4Basic Hardware Units of a Computer

Page 7: 1Object-Oriented Program Development Using C++ Computer Science and Programming Languages Computers are ubiquitous Computer literacy is essential Computer

7Object-Oriented Program Development Using C++

Figure 1-5A Pentium Microprocessor Chip

Page 8: 1Object-Oriented Program Development Using C++ Computer Science and Programming Languages Computers are ubiquitous Computer literacy is essential Computer

8Object-Oriented Program Development Using C++

Figure 1-6Internal Structure of a Hard Disk Drive

Page 9: 1Object-Oriented Program Development Using C++ Computer Science and Programming Languages Computers are ubiquitous Computer literacy is essential Computer

9Object-Oriented Program Development Using C++

Programming Languages

• Programming language: set of rules and valid identifiers used to construct computer programs

• Computer program: set of instructions directing manipulation of data by hardware

• Software: a set of programs• Low-level languages

– Machine language: binary code

– Assembly: first layer of symbolic abstraction

Page 10: 1Object-Oriented Program Development Using C++ Computer Science and Programming Languages Computers are ubiquitous Computer literacy is essential Computer

10Object-Oriented Program Development Using C++

Figure 1-9Assembly Programs Must Be Translated

Page 11: 1Object-Oriented Program Development Using C++ Computer Science and Programming Languages Computers are ubiquitous Computer literacy is essential Computer

11Object-Oriented Program Development Using C++

Low- and High-Level Languages

• Low-level languages are close to hardware– Machine language touches circuits with binary code– Assembly

• Symbolic code in 1:1 correspondence with machine• Programs assembled (converted) into machine code

• High-level languages– Syntax and semantics approach human languages– Examples: C, C++, Java, FORTRAN– Source code: is either interpreted or compiled

Page 12: 1Object-Oriented Program Development Using C++ Computer Science and Programming Languages Computers are ubiquitous Computer literacy is essential Computer

12Object-Oriented Program Development Using C++

Figure 1-10Creating an Executable C++ Program

Page 13: 1Object-Oriented Program Development Using C++ Computer Science and Programming Languages Computers are ubiquitous Computer literacy is essential Computer

13Object-Oriented Program Development Using C++

Procedural and Object Orientations

• Procedural Orientation– Modules of code (procedures) transform data– Procedures known by different names

• Functions (C and C++) and methods (Java and C++)

• Object Orientation– Objects as software constructs modeling real things– Design origins: the GUI (graphical user interface)– Three defining characteristics

• Encapsulation, inheritance, polymorphism

Page 14: 1Object-Oriented Program Development Using C++ Computer Science and Programming Languages Computers are ubiquitous Computer literacy is essential Computer

14Object-Oriented Program Development Using C++

Application and System Software

• Application software: set of programs that interface with the user

• System software– Manages internal operation of the physical device– Insulates user from hardware

• Operating System (OS): critical system software• Manages system resources• Supports concurrency • Provides security and protection

Page 15: 1Object-Oriented Program Development Using C++ Computer Science and Programming Languages Computers are ubiquitous Computer literacy is essential Computer

15Object-Oriented Program Development Using C++

The Development of C++ • Post WWII programming language design goals:

– Structure, usability, flexibility, and customization• Early high-level languages oriented to procedures

– Examples: FORTRAN, ALGOL, COBOL• Rationalization through structured programming

– Pascal and C • C++ as a superset of C

– Incorporates C language into object-oriented paradigm

Page 16: 1Object-Oriented Program Development Using C++ Computer Science and Programming Languages Computers are ubiquitous Computer literacy is essential Computer

16Object-Oriented Program Development Using C++

Figure 1-11Basic Procedural Operations

Page 17: 1Object-Oriented Program Development Using C++ Computer Science and Programming Languages Computers are ubiquitous Computer literacy is essential Computer

17Object-Oriented Program Development Using C++

Figure 1-12Fortran Was Developed for Scientific and Engineering Applications

Page 18: 1Object-Oriented Program Development Using C++ Computer Science and Programming Languages Computers are ubiquitous Computer literacy is essential Computer

18Object-Oriented Program Development Using C++

Objects and Classes

• Objects model real world things and abstractions– GUI– Data packages

• Classes: design template or blueprint for object• Skills to master in object-oriented programming

– Designing classes– Employing objects in a program– Utilizing user-defined classes in standard template

library (STL)

Page 19: 1Object-Oriented Program Development Using C++ Computer Science and Programming Languages Computers are ubiquitous Computer literacy is essential Computer

19Object-Oriented Program Development Using C++

A Class is a Plan

• Class: plan with complete set of parts and instructions

• Classes can be analogized to recipes– Ingredients and method for preparation

– Attributes and behaviors

– A food dish is an instance of a recipe

– An object is an instance of a class

Page 20: 1Object-Oriented Program Development Using C++ Computer Science and Programming Languages Computers are ubiquitous Computer literacy is essential Computer

20Object-Oriented Program Development Using C++

Figure 1-15Using Named Ingredients for Gary’s Sardine Spread

Page 21: 1Object-Oriented Program Development Using C++ Computer Science and Programming Languages Computers are ubiquitous Computer literacy is essential Computer

21Object-Oriented Program Development Using C++

From Recipe to Class

• Class ingredients: data declaration section– Includes variable data type and name

– Specific values typically assigned separately

• Class methods for preparation: methods section– Follows data declaration section

– Typically divided into two sub-sections• Methods declaration

• Methods definition (or implementation)

Page 22: 1Object-Oriented Program Development Using C++ Computer Science and Programming Languages Computers are ubiquitous Computer literacy is essential Computer

22Object-Oriented Program Development Using C++

Figure 1-17A Programming Plan for Displaying a Message

Page 23: 1Object-Oriented Program Development Using C++ Computer Science and Programming Languages Computers are ubiquitous Computer literacy is essential Computer

23Object-Oriented Program Development Using C++

Figure 1-18 Basic C++ Class Syntax

Page 24: 1Object-Oriented Program Development Using C++ Computer Science and Programming Languages Computers are ubiquitous Computer literacy is essential Computer

24Object-Oriented Program Development Using C++

A C++ Class Structure

• Class consists of a header and a body• Basic header: contains class keyword and name • Class body

– Follows declaration of header

– Enclosed by braces and ends with semicolon

– Contents• Comprised of declaration of data and methods

• May include definition of methods

Page 25: 1Object-Oriented Program Development Using C++ Computer Science and Programming Languages Computers are ubiquitous Computer literacy is essential Computer

25Object-Oriented Program Development Using C++

Figure 1-19The Structure of a C++ Class Named ShowFirstMessage

Page 26: 1Object-Oriented Program Development Using C++ Computer Science and Programming Languages Computers are ubiquitous Computer literacy is essential Computer

26Object-Oriented Program Development Using C++

Figure 1-20A Sample C++ Class

Page 27: 1Object-Oriented Program Development Using C++ Computer Science and Programming Languages Computers are ubiquitous Computer literacy is essential Computer

27Object-Oriented Program Development Using C++

A First Program in C++

• Classes and functions – Consist of identifiers: words and special symbols

• Reserved words• Standard identifiers• Programmer supplied words

– Building blocks for an executable C++ program• Standard template library (STL)

– Contains programmer-defined classes– Example: <cmath>

Page 28: 1Object-Oriented Program Development Using C++ Computer Science and Programming Languages Computers are ubiquitous Computer literacy is essential Computer

28Object-Oriented Program Development Using C++

Figure 1-22Creating a C++ Executable Program

Page 29: 1Object-Oriented Program Development Using C++ Computer Science and Programming Languages Computers are ubiquitous Computer literacy is essential Computer

29Object-Oriented Program Development Using C++

Reserved Words• Reserved word (keyword)

– Predefined for (and restricted to) specific use• Standard identifiers

– Predefined for (but not restricted to) specific use• Identifiers: combination of letters, digits, or

underscores (_) – First character must be a letter or underscore (_)– Only letters, digits, or underscores may follow

initial character– Blank spaces not allowed

Page 30: 1Object-Oriented Program Development Using C++ Computer Science and Programming Languages Computers are ubiquitous Computer literacy is essential Computer

30Object-Oriented Program Development Using C++

Table 1-2C++ Reserved Words

Page 31: 1Object-Oriented Program Development Using C++ Computer Science and Programming Languages Computers are ubiquitous Computer literacy is essential Computer

31Object-Oriented Program Development Using C++

Table 1-3Sample of Standard C++ Identifiers

Page 32: 1Object-Oriented Program Development Using C++ Computer Science and Programming Languages Computers are ubiquitous Computer literacy is essential Computer

32Object-Oriented Program Development Using C++

The main Function

• main ( ) function: program casing

• main ( ) header consists of three parts– Return type

– Name

– Parameter (or argument) list (optional)

• Body of main enclose by braces { } – Programming logic (linked to classes and functions)

– Return statement made up of keyword and value

– Each statement inside the function must end with semi-colon

Page 33: 1Object-Oriented Program Development Using C++ Computer Science and Programming Languages Computers are ubiquitous Computer literacy is essential Computer

33Object-Oriented Program Development Using C++

Figure 1-23The Structure of a main Function

Page 34: 1Object-Oriented Program Development Using C++ Computer Science and Programming Languages Computers are ubiquitous Computer literacy is essential Computer

34Object-Oriented Program Development Using C++

The cout Object

• cout object– cout derived from Console OUTput– Sends passed data to standard output display device

• Classic first program: Hello World– Comments identify programmer and contents– Includes header file called <iostream> from STL– Namespace “std” identifies location of files– chief instruction: cout << “Hello World!”;

• “<<“ stands for insertion (put) symbol

Page 35: 1Object-Oriented Program Development Using C++ Computer Science and Programming Languages Computers are ubiquitous Computer literacy is essential Computer

35Object-Oriented Program Development Using C++

Figure 1-25 The Output from Program 1-1

Page 36: 1Object-Oriented Program Development Using C++ Computer Science and Programming Languages Computers are ubiquitous Computer literacy is essential Computer

36Object-Oriented Program Development Using C++

Programming Style

• Programs begin executing with int main ( )• Only one main ( ) function per program• Recommend placement of syntactic elements

– Opening braces directly below first letter of int

– Closing brace follows return statement

– One statement per line

– Indentation represents program logic

Page 37: 1Object-Oriented Program Development Using C++ Computer Science and Programming Languages Computers are ubiquitous Computer literacy is essential Computer

37Object-Oriented Program Development Using C++

Comments

• Comments may be placed anywhere in program• Comments improve readability and maintainability• Comments are ignored by compiler• Two types of comments

– Line: denoted by pair of opening forward slashes (//)

– Block: opens with forward slash and asterisk and closes with asterisk and forward slash (/*…*/)