Sie sind auf Seite 1von 9

C++ Programs to Accompany

Programming Logic and Design

Jo Ann Smith

AN INTRODUCTION TO C++
AND
THE C++ PROGRAMMING ENVIRONMENT

Algoritme dan Struktur Data 1


Yohanes Suyanto

C++ Programs . . . Algoritme dan Struktur Data I


Objectives

After completing this chapter you will be able to:


Discuss the C++ programming language and its history
Explain introductory concepts and terminology used in
object-oriented programming
Recognize the structure of a C++ program
Complete the C++ development cycle, which includes
creating a source code file, compiling the source code,
and executing a C++ program

C++ Programs . . . Algoritme dan Struktur Data I


THE C++ PROGRAMMING LANGUAGE

The C programming language was written in the early


1970s by Dennis Ritchie at AT&T Bell Labs.
Its a high level language and low level language
The C++ programming language was developed by
Bjarne Stroustrup at AT&T Bell Labs in 1979
The C++ is an OOP language

C++ Programs . . . Algoritme dan Struktur Data I


AN INTRODUCTION TO OBJECT-ORIENTED
TERMINOLOGY

object
encapsulation
behaviour
attribute
method
class
instance

C++ Programs . . . Algoritme dan Struktur Data I


THE STRUCTURE OF A C++ PROGRAM

#i n c l u d e <i o s t r e a m >
u s i n g namespace s t d ;
i n t main ( )
{
c o u t << ” H e l l o World ” << e n d l ;
return 0;
}

C++ Programs . . . Algoritme dan Struktur Data I


THE C++ DEVELOPMENT CYCLE

Step 1 Source code Step 2


Write C++ file Compile source
source code (MyProgram.cpp) code

C++ Object Code


MyProgram.o
and
C++ Executable
MyProgram

Step 3
Execute program

Output

C++ Programs . . . Algoritme dan Struktur Data I


Declaring and initializing C++ variables
Invalid variable names
Name of Explanation
variable
3wrong Invalid because it begins with a digit
don’t Invalid because it contains a single quotation mark
int Invalid because it is a C++ keyword
first name Invalid because it contains a space

Valid variable names and declarations

double salary ;
double cost = 12.95;
string firstName ;
string homeAddress = ” 123 Main S t r e e t ” ;

C++ Programs . . . Algoritme dan Struktur Data I


ARITHMETIC OPERATORS
Operator Name Symbol Example Comment
Addition + num1 + num2
Subtraction - num1 - num2
Multiplication * num1 * num2
Division / 15/2 Integer division; result is 7; frac-
tion is truncated.
15.0 / 2.0 Floating point division; result is
7.5.
15.0 / 2 Floating point division because
one of the operands is a floating
point number; result is 7.5.
Modulus % hours % 24 Performs division and finds the
remainder; result is 1 if the value
of hours is 25.
Unary plus + +num1 Maintains the value of the ex-
pression; if the value of num1 is
3, then +num1 is 3.
Unary minus - -(num1 - num2) If value of (num1 - num2) is 10,
then -(num1 - num2) is -10.

C++ Programs . . . Algoritme dan Struktur Data I


ASSIGNMENT OPERATORS AND THE ASSIGNMENT STATEMENT

Operator Name Symbol Example Comment


Assignment = count = 5; Places the value on the right
side into the memory location
named on the left side.
Initialization = int count = 5; Places the value on the right
side into the memory location
named on the left side when
the variable is declared.
Assignment += num += 20; Equivalent to num = num +
20;
-= num -= 20; Equivalent to num = num - 20;
*= num *= 20; Equivalent to num = num * 20;
/= num /= 20; Equivalent to num = num / 20;
%= num %= 20; Equivalent to num = num %
20;

C++ Programs . . . Algoritme dan Struktur Data I

Das könnte Ihnen auch gefallen