1object-oriented program development using c++ computer science and programming languages computers...
TRANSCRIPT
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
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
3Object-Oriented Program Development Using C++
Figure 1-1Charles Babbage’s Analytical Engine
4Object-Oriented Program Development Using C++
Figure 1-2ENIAC (Courtesy IBM archives)
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
6Object-Oriented Program Development Using C++
Figure 1-4Basic Hardware Units of a Computer
7Object-Oriented Program Development Using C++
Figure 1-5A Pentium Microprocessor Chip
8Object-Oriented Program Development Using C++
Figure 1-6Internal Structure of a Hard Disk Drive
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
10Object-Oriented Program Development Using C++
Figure 1-9Assembly Programs Must Be Translated
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
12Object-Oriented Program Development Using C++
Figure 1-10Creating an Executable C++ Program
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
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
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
16Object-Oriented Program Development Using C++
Figure 1-11Basic Procedural Operations
17Object-Oriented Program Development Using C++
Figure 1-12Fortran Was Developed for Scientific and Engineering Applications
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)
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
20Object-Oriented Program Development Using C++
Figure 1-15Using Named Ingredients for Gary’s Sardine Spread
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)
22Object-Oriented Program Development Using C++
Figure 1-17A Programming Plan for Displaying a Message
23Object-Oriented Program Development Using C++
Figure 1-18 Basic C++ Class Syntax
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
25Object-Oriented Program Development Using C++
Figure 1-19The Structure of a C++ Class Named ShowFirstMessage
26Object-Oriented Program Development Using C++
Figure 1-20A Sample C++ Class
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>
28Object-Oriented Program Development Using C++
Figure 1-22Creating a C++ Executable Program
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
30Object-Oriented Program Development Using C++
Table 1-2C++ Reserved Words
31Object-Oriented Program Development Using C++
Table 1-3Sample of Standard C++ Identifiers
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
33Object-Oriented Program Development Using C++
Figure 1-23The Structure of a main Function
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
35Object-Oriented Program Development Using C++
Figure 1-25 The Output from Program 1-1
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
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 (/*…*/)