45
A FIRST BOOK OF C++ CHAPTER 1 GETTING STARTED

A FIRST BOOK OF C++ CHAPTER 1 GETTING STARTED. OBJECTIVES In this chapter, you will learn about: Introduction to Programming Function and Class Names

Embed Size (px)

Citation preview

Page 1: A FIRST BOOK OF C++ CHAPTER 1 GETTING STARTED. OBJECTIVES In this chapter, you will learn about: Introduction to Programming Function and Class Names

A FIRST BOOK OF C++

CHAPTER 1

GETTING STARTED

Page 2: A FIRST BOOK OF C++ CHAPTER 1 GETTING STARTED. OBJECTIVES In this chapter, you will learn about: Introduction to Programming Function and Class Names

OBJECTIVES

In this chapter, you will learn about:

• Introduction to Programming• Function and Class Names• The cout Object• Programming Style• Common Programming Errors

A First Book of C++ 4th Edition 2

Page 3: A FIRST BOOK OF C++ CHAPTER 1 GETTING STARTED. OBJECTIVES In this chapter, you will learn about: Introduction to Programming Function and Class Names

INTRODUCTION TO PROGRAMMING

Computer program

• Data and instructions used to operate a computer

Programming

• Writing computer program in a language that the computer can respond to and that other programmers can understand

Programming language

• Set of instructions, data, and rules used to construct a program

• High-level languages (C++, VB)

• Low-level languages (Assembly)

A First Book of C++ 4th Edition 3

Page 4: A FIRST BOOK OF C++ CHAPTER 1 GETTING STARTED. OBJECTIVES In this chapter, you will learn about: Introduction to Programming Function and Class Names

INTRODUCTION TO PROGRAMMING

Procedural language

• Instructions are used to create self-contained units (procedures)

• Procedures accept data as input and transform data to produce a specific result as an output

• Initially, high-level programming languages were predominately procedural

A First Book of C++ 4th Edition 4

Page 5: A FIRST BOOK OF C++ CHAPTER 1 GETTING STARTED. OBJECTIVES In this chapter, you will learn about: Introduction to Programming Function and Class Names

INTRODUCTION TO PROGRAMMING

A First Book of C++ 4th Edition 5

Page 6: A FIRST BOOK OF C++ CHAPTER 1 GETTING STARTED. OBJECTIVES In this chapter, you will learn about: Introduction to Programming Function and Class Names

INTRODUCTION TO PROGRAMMING

Object-oriented languages

• Program must define objects that it is manipulating• Such definitions include:

• The general characteristics of objects

• Specific operations to manipulate objects

C++ is an object-oriented language

• Has procedures and objects• Supports code reuse• Reduce needs to revalidate and retest code.

A First Book of C++ 4th Edition 6

Page 7: A FIRST BOOK OF C++ CHAPTER 1 GETTING STARTED. OBJECTIVES In this chapter, you will learn about: Introduction to Programming Function and Class Names

INTRODUCTION TO PROGRAMMING

C++ began as extension to C

• C is a procedural language developed in the 1970s at AT&T Bell Laboratories

In early 1980s, Bjarne Stroustrup (also at AT&T) used his background in simulation languages to develop C++

Object-orientation and other procedural improvements were combined with existing C language features to form C++

A First Book of C++ 4th Edition 7

Page 8: A FIRST BOOK OF C++ CHAPTER 1 GETTING STARTED. OBJECTIVES In this chapter, you will learn about: Introduction to Programming Function and Class Names

ALGORITHMS AND PROCEDURES

Before writing a program, a programmer must clearly understand:

• What data is to be used• The desired result• The procedure needed to produce this result

The procedure is referred to as an algorithm

• Step-by-step sequence of instructions describing how to perform a computation

Humans solve problems heuristically

• Flat tire – change it, do not think about it.

A First Book of C++ 4th Edition 8

Page 9: A FIRST BOOK OF C++ CHAPTER 1 GETTING STARTED. OBJECTIVES In this chapter, you will learn about: Introduction to Programming Function and Class Names

ALGORITHMS AND PROCEDURES

Assume that a program must calculate the sum of all whole numbers from 1 through 100

A computer:

• Cannot respond to heuristic command: “Add the numbers from 1 - 100”

• Is an algorithm-responding machine and not a heuristic-responding machine

Several methods or algorithms can be used to find the required sum

A First Book of C++ 4th Edition 9

Page 10: A FIRST BOOK OF C++ CHAPTER 1 GETTING STARTED. OBJECTIVES In this chapter, you will learn about: Introduction to Programming Function and Class Names

A First Book of C++ 4th Edition 10

Page 11: A FIRST BOOK OF C++ CHAPTER 1 GETTING STARTED. OBJECTIVES In this chapter, you will learn about: Introduction to Programming Function and Class Names

A First Book of C++ 4th Edition 11

Page 12: A FIRST BOOK OF C++ CHAPTER 1 GETTING STARTED. OBJECTIVES In this chapter, you will learn about: Introduction to Programming Function and Class Names

A First Book of C++ 4th Edition 12

Page 13: A FIRST BOOK OF C++ CHAPTER 1 GETTING STARTED. OBJECTIVES In this chapter, you will learn about: Introduction to Programming Function and Class Names

CLASSES AND OBJECTS

Data object

• Set of values packaged as single unit

Class

• Set of objects with similar attributes

General concept of object-oriented programming is difference between an object and the larger set of which it is a member (class)

A red, Ford Taurus sedan is an instance, or object, of a general class of automobiles

A First Book of C++ 4th Edition 13

Page 14: A FIRST BOOK OF C++ CHAPTER 1 GETTING STARTED. OBJECTIVES In this chapter, you will learn about: Introduction to Programming Function and Class Names

CLASS EXAMPLE• Class = automobile

• Properties of automobile class= make, model, color, year

• Car.color = Red

• Object = Each individual auto is an object.

• Object is also an Instance of the automobile class.

• Methods = start, stop, speedup, slowdown

• Car.Start, Car.Stop

• Events of automobile class = Arrive, Crash

A First Book of C++ 4th Edition 14

Page 15: A FIRST BOOK OF C++ CHAPTER 1 GETTING STARTED. OBJECTIVES In this chapter, you will learn about: Introduction to Programming Function and Class Names

PROGRAM TRANSLATION

C++ source program

• Set of instructions written in C++ language

Machine language

• Internal computer language• Consists of a series of 1s and 0s

Source program cannot be executed until it is translated into machine language

• Interpreted language translates one statement at a time• Compiled language translates all statements together

A First Book of C++ 4th Edition 15

Page 16: A FIRST BOOK OF C++ CHAPTER 1 GETTING STARTED. OBJECTIVES In this chapter, you will learn about: Introduction to Programming Function and Class Names

PROGRAM TRANSLATION

A First Book of C++ 4th Edition 16

Page 17: A FIRST BOOK OF C++ CHAPTER 1 GETTING STARTED. OBJECTIVES In this chapter, you will learn about: Introduction to Programming Function and Class Names

FUNCTION AND CLASS NAMES

Modular programs

• Segments arranged in logical order to form an integrated unit

Module

• Segments of modular program

Function: Name of a C++ procedure

• Composed of sequence of C++ instructions• Function interface is its inputs and results• Method of converting input to results is encapsulated and

hidden within function

A First Book of C++ 4th Edition 17

Page 18: A FIRST BOOK OF C++ CHAPTER 1 GETTING STARTED. OBJECTIVES In this chapter, you will learn about: Introduction to Programming Function and Class Names

FUNCTION AND CLASS NAMES

A First Book of C++ 4th Edition 18

Page 19: A FIRST BOOK OF C++ CHAPTER 1 GETTING STARTED. OBJECTIVES In this chapter, you will learn about: Introduction to Programming Function and Class Names

FUNCTION AND CLASS NAMES

A First Book of C++ 4th Edition 19

Page 20: A FIRST BOOK OF C++ CHAPTER 1 GETTING STARTED. OBJECTIVES In this chapter, you will learn about: Introduction to Programming Function and Class Names

FUNCTION AND CLASS NAMES

Identifiers

• Names that convey an idea of the purpose of function or class

Identifier composition rules

• First character must be a letter or underscore• Only letter, digit, or underscore may follow• Blank spaces aren’t allowed• Identify component words with initial capitalization• Cannot be C++ keyword• Should be a mnemonic

A First Book of C++ 4th Edition 20

Page 21: A FIRST BOOK OF C++ CHAPTER 1 GETTING STARTED. OBJECTIVES In this chapter, you will learn about: Introduction to Programming Function and Class Names

FUNCTION AND CLASS NAMES

A First Book of C++ 4th Edition 21

Page 22: A FIRST BOOK OF C++ CHAPTER 1 GETTING STARTED. OBJECTIVES In this chapter, you will learn about: Introduction to Programming Function and Class Names

FUNCTION AND CLASS NAMES

Examples of valid identifiers:

grosspay taxCalc

addNums degToRad

multByTwo salesTax

netPay bessel

A First Book of C++ 4th Edition 22

Page 23: A FIRST BOOK OF C++ CHAPTER 1 GETTING STARTED. OBJECTIVES In this chapter, you will learn about: Introduction to Programming Function and Class Names

FUNCTION AND CLASS NAMES

Examples of invalid identifiers:

4ab3 (begins with a number)

e*6 (contains a special character)

while (is a keyword)

A First Book of C++ 4th Edition 23

Page 24: A FIRST BOOK OF C++ CHAPTER 1 GETTING STARTED. OBJECTIVES In this chapter, you will learn about: Introduction to Programming Function and Class Names

THE MAIN() FUNCTION

Each C+ program must have one and only one function named main

Called a driver function because it drives the other modules

A First Book of C++ 4th Edition 24

Page 25: A FIRST BOOK OF C++ CHAPTER 1 GETTING STARTED. OBJECTIVES In this chapter, you will learn about: Introduction to Programming Function and Class Names

THE MAIN() FUNCTION

A First Book of C++ 4th Edition 25

Page 26: A FIRST BOOK OF C++ CHAPTER 1 GETTING STARTED. OBJECTIVES In this chapter, you will learn about: Introduction to Programming Function and Class Names

THE MAIN() FUNCTION

First line of function is called header line

• What type of data, if any, is returned from function• The name of function• What type of data, if any, is sent into function

Data transmitted into function at runtime are referred to as arguments of function

A First Book of C++ 4th Edition 26

Page 27: A FIRST BOOK OF C++ CHAPTER 1 GETTING STARTED. OBJECTIVES In this chapter, you will learn about: Introduction to Programming Function and Class Names

THE MAIN() FUNCTION

A First Book of C++ 4th Edition 27

Page 28: A FIRST BOOK OF C++ CHAPTER 1 GETTING STARTED. OBJECTIVES In this chapter, you will learn about: Introduction to Programming Function and Class Names

QUIZ #1

A First Book of C++ 4th Edition 28

• True or False: Low-Level languages are easier to use than high-level languages.

• A(n) ______ is a step-by-step sequence of instructions that describes how a computation is to be performed.

• True or False: In a problem-solving mode, people tend to think heuristically.

Page 29: A FIRST BOOK OF C++ CHAPTER 1 GETTING STARTED. OBJECTIVES In this chapter, you will learn about: Introduction to Programming Function and Class Names

THE COUT OBJECT

The cout object sends data to the standard output display device

• The display device is usually a video screen• Name derived from Console OUTput and pronounced “see

out”

Data is passed to cout by the insertion symbol

• cout << “Hello there, World!”;

A First Book of C++ 4th Edition 29

Page 30: A FIRST BOOK OF C++ CHAPTER 1 GETTING STARTED. OBJECTIVES In this chapter, you will learn about: Introduction to Programming Function and Class Names

THE COUT OBJECT

A First Book of C++ 4th Edition 30

Page 31: A FIRST BOOK OF C++ CHAPTER 1 GETTING STARTED. OBJECTIVES In this chapter, you will learn about: Introduction to Programming Function and Class Names

THE COUT OBJECT

Preprocessor command

• Performs an action before the compiler translates source code to machine code

• Example: #include <iostream>• Causes the iostream file to be inserted wherever the #include command appears

iostream is part of the C++ standard library

• Included in iostream are two important classes:

• istream: Declarations and methods for data input

• ostream: Declarations and methods for data output

A First Book of C++ 4th Edition 31

Page 32: A FIRST BOOK OF C++ CHAPTER 1 GETTING STARTED. OBJECTIVES In this chapter, you will learn about: Introduction to Programming Function and Class Names

THE COUT OBJECT

Namespace

• File accessed by compiler when looking for prewritten classes or functions

Sample namespace statement:

• using namespace std;• iostream contained in a namespace called std• Compiler uses iostream’s cout object from std whenever cout is referenced

• Without using• std::cout

A First Book of C++ 4th Edition 32

Page 33: A FIRST BOOK OF C++ CHAPTER 1 GETTING STARTED. OBJECTIVES In this chapter, you will learn about: Introduction to Programming Function and Class Names

THE COUT OBJECT

A First Book of C++ 4th Edition 33

Page 34: A FIRST BOOK OF C++ CHAPTER 1 GETTING STARTED. OBJECTIVES In this chapter, you will learn about: Introduction to Programming Function and Class Names

THE COUT OBJECT

Newline escape sequence

• Instructs the display device to move to a new line• Caused when the characters backslash \ and n are used

together• Backslash provides an “escape” from the normal

interpretation of the character that follows

Newline escape sequences can be placed anywhere within a message to cout

A First Book of C++ 4th Edition 34

Page 35: A FIRST BOOK OF C++ CHAPTER 1 GETTING STARTED. OBJECTIVES In this chapter, you will learn about: Introduction to Programming Function and Class Names

THE COUT OBJECT

A First Book of C++ 4th Edition 35

Page 36: A FIRST BOOK OF C++ CHAPTER 1 GETTING STARTED. OBJECTIVES In this chapter, you will learn about: Introduction to Programming Function and Class Names

PROGRAMMING STYLE

Every C++ program must contain one and only one main() function

• Statements included within braces { }

C++ allows flexibility in format for the word main, the

parentheses ( ), and braces { }

• More than one statement can be put on line• One statement can be written across lines

Use formatting for clarity and ease of program reading

A First Book of C++ 4th Edition 36

Page 37: A FIRST BOOK OF C++ CHAPTER 1 GETTING STARTED. OBJECTIVES In this chapter, you will learn about: Introduction to Programming Function and Class Names

PROGRAMMING STYLE

Function name starts in column 1

• Name and parentheses on their own line

Opening brace of function body on next line

• Aligned with first letter of function name

Closing brace is last line of function

• Aligned with opening brace

Standard form highlights the function as a unit

A First Book of C++ 4th Edition 37

Page 38: A FIRST BOOK OF C++ CHAPTER 1 GETTING STARTED. OBJECTIVES In this chapter, you will learn about: Introduction to Programming Function and Class Names

PROGRAMMING STYLE

Within function, indent statements 2-3 spaces

• Creates uniform look for similar statement groups• Good programming practice

Final program form should be consistent

• Proper format improves program readability and understandability

A First Book of C++ 4th Edition 38

Page 39: A FIRST BOOK OF C++ CHAPTER 1 GETTING STARTED. OBJECTIVES In this chapter, you will learn about: Introduction to Programming Function and Class Names

COMMENTS

Explanatory remarks written within program

• Clarify purpose of the program• Describe objective of a group of statements• Explain function of a single line of code

Computer ignores all comments

• Comments exist only for convenience of reader

A well-constructed program should be readable and understandable

• Comments help explain unclear components• Always begin with set of initial comments with author, date

modified, description

A First Book of C++ 4th Edition 39

Page 40: A FIRST BOOK OF C++ CHAPTER 1 GETTING STARTED. OBJECTIVES In this chapter, you will learn about: Introduction to Programming Function and Class Names

COMMENTSLine comment

• Begins with two slashes(//) and continues to the end of the line• Can be written on line by itself or at the end of line that contains

program code// this is a line comment

Block comment

• Multiple-line comment begins with the symbols /* and ends with the symbols */

/* This is a block comment that

spans

three lines */

A First Book of C++ 4th Edition 40

Page 41: A FIRST BOOK OF C++ CHAPTER 1 GETTING STARTED. OBJECTIVES In this chapter, you will learn about: Introduction to Programming Function and Class Names

COMMON PROGRAMMING ERRORS

Omitting parentheses after main()

Omitting or incorrectly typing the opening brace {

• Opening brace signifies start of function body

Omitting or incorrectly typing the closing brace }

• Closing brace signifies end of function

Omitting the semicolon at the end of each statement

Adding a semicolon after the #include <iostream> preprocessor command

A First Book of C++ 4th Edition 41

Page 42: A FIRST BOOK OF C++ CHAPTER 1 GETTING STARTED. OBJECTIVES In this chapter, you will learn about: Introduction to Programming Function and Class Names

COMMON PROGRAMMING ERRORS

Misspelling the name of an object or function

• Example: Typing cot instead of cout

Forgetting to close a string sent to cout with a double-quote symbol

Forgetting \n to indicate a new line

A First Book of C++ 4th Edition 42

Page 43: A FIRST BOOK OF C++ CHAPTER 1 GETTING STARTED. OBJECTIVES In this chapter, you will learn about: Introduction to Programming Function and Class Names

SUMMARY

A C++ program consists of one or more modules

• One module must be the function main()• main() is starting point of C++ program

The simplest C++ program has the form:

#include <iostream>

using namespaces std;

int main()

{

program statements;

return 0;

}

A First Book of C++ 4th Edition 43

Page 44: A FIRST BOOK OF C++ CHAPTER 1 GETTING STARTED. OBJECTIVES In this chapter, you will learn about: Introduction to Programming Function and Class Names

SUMMARY

C++ statements are terminated by a semicolon

Standard library contains many functions and classes

• Standard Library provided with C++ compiler• Includes <iostream> for input and output

cout object displays text or numeric results

• Stream of characters is sent to cout by:• Enclosing characters in double quotes

• Using the insertion (“put to”) operator, <<

A First Book of C++ 4th Edition 44

Page 45: A FIRST BOOK OF C++ CHAPTER 1 GETTING STARTED. OBJECTIVES In this chapter, you will learn about: Introduction to Programming Function and Class Names

QUIZ #2

A First Book of C++ 4th Edition 45

• True or False: The cout object displays on the monitor whatever data is passed to it.

• A keyword immediately before the function name defines the _____ of the value returned by the function when it has completed its operation.

• Comments that span across two or more lines are usually written as _____ comments.

• True or False: The cout object is created from the istream class.