1 chapter 0 introduction to c++ sen zhang. 2 computer science computer science encompasses a variety...

51
1 Chapter 0 Introduction to C++ Sen Zhang

Post on 22-Dec-2015

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 Chapter 0 Introduction to C++ Sen Zhang. 2 Computer science Computer science encompasses a variety of topics and subjects that relate to computation

1

Chapter 0

Introduction to C++

Sen Zhang

Page 2: 1 Chapter 0 Introduction to C++ Sen Zhang. 2 Computer science Computer science encompasses a variety of topics and subjects that relate to computation

2

Computer science• Computer science encompasses a variety of topics and subjects that

relate to computation (or more generalized manipulation) of data (both software and hardware)– abstract analysis of algorithms – programming languages and formal grammars – program design – software engineering– computer hardware– Organization– architecture– Database– Graphics– network– Communication– Data mining and data warehouse– Data compression– …

Page 3: 1 Chapter 0 Introduction to C++ Sen Zhang. 2 Computer science Computer science encompasses a variety of topics and subjects that relate to computation

3

Why is programming language the first core course?

• From the previous list, you can tell that programming language is one of the most important topics that computer science should cover.

• At Oneonta, we have it as the first core course.

Page 4: 1 Chapter 0 Introduction to C++ Sen Zhang. 2 Computer science Computer science encompasses a variety of topics and subjects that relate to computation

What is a computer?

• A computer is a machine (device) that mimics human intelligence.

• A computer is a machine that can process information by executing a sequence of instructions.– Information is represented by data

– Instructions are organized in the form of programs.

• Programs are written in certain machine languages( low level or high level).

4

Page 5: 1 Chapter 0 Introduction to C++ Sen Zhang. 2 Computer science Computer science encompasses a variety of topics and subjects that relate to computation

• Language is a term most commonly used to refer to so-called "natural languages" — the written forms of communication ubiquitous among humankind.

• We ignore the spoken language, which relates to sounds, based on the use of the physical organ in speech. The ability to use speech originated in remote prehistoric times.

• Written aspect is more inspirational to this class than speech aspect.

5

Page 6: 1 Chapter 0 Introduction to C++ Sen Zhang. 2 Computer science Computer science encompasses a variety of topics and subjects that relate to computation

How to define a language?

• The systematic creation, maintenance and use of systems of symbols, which dynamically reference concepts and assemble according to structured patterns to form expressions and communicate meaning.

• A language is a system of signs (symbols, indices, icons) for encoding and decoding information.

6

Page 7: 1 Chapter 0 Introduction to C++ Sen Zhang. 2 Computer science Computer science encompasses a variety of topics and subjects that relate to computation

7

What a language is?

• A language is a system, used for communication, comprising a finite set of arbitrary symbols and a finite set of rules (or grammar) by which the combination of these symbols and creation of new identifiers are governed.

• These symbols and derived identifiers can be combined productively to convey new information.

• An interface between different parties!

Page 8: 1 Chapter 0 Introduction to C++ Sen Zhang. 2 Computer science Computer science encompasses a variety of topics and subjects that relate to computation

How to use a language?

• create• Speak (ignore)• Listen (ignore)• Write• read• Understand• agreement

8

Page 9: 1 Chapter 0 Introduction to C++ Sen Zhang. 2 Computer science Computer science encompasses a variety of topics and subjects that relate to computation

How to use a language?

• Create (not everybody can create a language)• Speak (ignore)• Listen (ignore)• Write (most people can write)• Read (most people can read)• Understand (?)• Agreement (specification)

9

Page 10: 1 Chapter 0 Introduction to C++ Sen Zhang. 2 Computer science Computer science encompasses a variety of topics and subjects that relate to computation

10

Languages

• Natural languages used by human beings– Evolved

– Many things unclear!

• Natural languages used by animals– Can you talk with a dog?

• (programming) Languages used by machines– Man-made!

– Well defined!

Page 11: 1 Chapter 0 Introduction to C++ Sen Zhang. 2 Computer science Computer science encompasses a variety of topics and subjects that relate to computation

A programming language is a language, therefore

• A programming language has some form of written specification of their syntax (form) and semantics (meaning).

• Some languages are defined by a specification document. – For example, the C++ programming language is

specified by an ISO Standard.

11

Page 12: 1 Chapter 0 Introduction to C++ Sen Zhang. 2 Computer science Computer science encompasses a variety of topics and subjects that relate to computation

A programming language?

• A programming language is an artificial language designed to express computations that can be performed by a machine, particularly a computer.

• Programming languages are languages created for programming purposes.

• Programs that control the behavior of a machine, to express algorithms precisely, or as a mode of human communication.

12

Page 13: 1 Chapter 0 Introduction to C++ Sen Zhang. 2 Computer science Computer science encompasses a variety of topics and subjects that relate to computation

What is writing?

• You will be surprised, there are many ways to define it, but not all of them are helpful in terms of learning programming.

• That is also the reason most people can not write very well even in her/his mother tongue.

• Now, let us define it!• Simply speaking, just put the basic written constructs

of the language ( symbols and letters, words and other constructs) together to express ideas, following the grammar rules of that language which everybody agree.

13

Page 14: 1 Chapter 0 Introduction to C++ Sen Zhang. 2 Computer science Computer science encompasses a variety of topics and subjects that relate to computation

Why do you write?

• For communication– Between you and others

– Between today’s you and you at a future time.

• So when you write, you have to follow the agreed rules, so that others can understand what you are trying to say.

• If you do not follow the grammar rules, it is not a good writing.

14

Page 15: 1 Chapter 0 Introduction to C++ Sen Zhang. 2 Computer science Computer science encompasses a variety of topics and subjects that relate to computation

15

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 use human language type

instructions– Low-level languages use instructions tied to a

computer type

Page 16: 1 Chapter 0 Introduction to C++ Sen Zhang. 2 Computer science Computer science encompasses a variety of topics and subjects that relate to computation

16

Coding <-> writing

• A writer writes a paper using a human being language.

• A programmer code a program using a programming language.

• You see, if you know how to use English to write, surely you do, then you must have the potential to learn how to code.

• However, if you really want to turn this potential into a real power, you have to learn and practice.

Page 17: 1 Chapter 0 Introduction to C++ Sen Zhang. 2 Computer science Computer science encompasses a variety of topics and subjects that relate to computation

Generations of programming languages

• Programming languages have been classified into several programming language generations. Historically, this classification was used to indicate increasing power of programming styles.

17

Page 18: 1 Chapter 0 Introduction to C++ Sen Zhang. 2 Computer science Computer science encompasses a variety of topics and subjects that relate to computation

• A first-generation programming language is a machine-level programming language.– Machine language

– Originally, no translator was used to compile or assemble the first-generation language.

– Programming in binary codes that are recognized by CPU

• The first-generation programming instructions were entered through the front panel switches of the computer system.

18

Page 19: 1 Chapter 0 Introduction to C++ Sen Zhang. 2 Computer science Computer science encompasses a variety of topics and subjects that relate to computation

Weaknesses of a machine language

• The main benefit of programming in a first-generation programming language is that the code a user writes can run very fast and efficiently, since it is directly executed by the CPU.

• However, machine language is a lot more difficult to learn than higher generational programming languages, and it is far more difficult to edit if errors occur. In addition, if instructions need to be added into memory at some location, then all the instructions after the insertion point need to be moved down to make room in memory to accommodate the new instructions. Doing so on a front panel with switches can be very difficult.

• Furthermore, portability is significantly reduced - in order to transfer code to a different computer it needs to be completely rewritten since the machine language for one computer could be significantly different from another computer. Architectural considerations make portability difficult too. For example, the number of registers on one CPU architecture could differ from those of another.

19

Page 20: 1 Chapter 0 Introduction to C++ Sen Zhang. 2 Computer science Computer science encompasses a variety of topics and subjects that relate to computation

• Second-generation programming language is a generational way to categorize assembly languages.

• Second-generation programming languages have the following properties:– The code can be read and written by a programmer.

• The second generation of programming languages also brought logical structure to software.

– To run on a computer it must be converted into a machine readable form, a process called assembly.

• An assembly language is specific designed for a particular CPU.

20

Page 21: 1 Chapter 0 Introduction to C++ Sen Zhang. 2 Computer science Computer science encompasses a variety of topics and subjects that relate to computation

• A third-generation programming language (3GL) is a refinement of a second-generation programming language.

• The third generation brought refinements to make the languages more programmer-friendly.

• This includes features like good support for aggregate data types, and expressing concepts in a way that favors the programmer, not the computer (e.g. no longer needing to state the length of multi-character (string) literals in Fortran).

• A third generation language improves over a second generation language by having the computer take care of non-essential details, not the programmer.

• High level language is a synonym for third-generation programming language and higher generation ones.

• First introduced in the late 1950s, Fortran, and COBOL are early examples of this sort of language.

• Most popular general-purpose languages today, such as C, C++, C#, Java, and Python, are also third-generation languages.

21

Page 22: 1 Chapter 0 Introduction to C++ Sen Zhang. 2 Computer science Computer science encompasses a variety of topics and subjects that relate to computation

• A fourth-generation programming language (abbreviated 4GL) is usually a programming language or programming environment designed with a specific purpose in mind.– So, usually not a programming language for all purposes.

• The 4GL followed the 3GL in an upward trend toward higher abstraction and statement power.

• Examples– SQL– SAS– Matlab

22

Page 23: 1 Chapter 0 Introduction to C++ Sen Zhang. 2 Computer science Computer science encompasses a variety of topics and subjects that relate to computation

• A fifth-generation programming language (abbreviated 5GL) is a programming language based around solving problems using constraints given to the program, rather than using an algorithm written by a programmer.

• Most constraint-based and logic programming languages and some declarative languages are fifth-generation languages.

• Fifth-generation languages are designed to make the computer solve a given problem without the programmer.

• This way, the programmer only needs to worry about what problems need to be solved and what conditions need to be met, without worrying about how to implement a routine or algorithm to solve them.

• Fifth-generation languages are used mainly in artificial intelligence research. Prolog and List are examples of fifth-generation languages.

23

Page 24: 1 Chapter 0 Introduction to C++ Sen Zhang. 2 Computer science Computer science encompasses a variety of topics and subjects that relate to computation

C++

• C++ is a general-purpose programming language based on the C programming language as described in ISO/IEC 9899:1990.

• In addition to the facilities provided by C, C++ provides additional data types, classes, templates, exceptions, namespaces, inline functions, operator overloading, function-name overloading, references, free-store management operators, and additional library facilities.

24

Page 25: 1 Chapter 0 Introduction to C++ Sen Zhang. 2 Computer science Computer science encompasses a variety of topics and subjects that relate to computation

25

• How good you are at English can be measured at two levels ( I tend to think these two will get the idea across as far as your concern is how to survive CSCI 116)– Whether you know the language

– Can you apply the language

Page 26: 1 Chapter 0 Introduction to C++ Sen Zhang. 2 Computer science Computer science encompasses a variety of topics and subjects that relate to computation

26

Basketball

• Two different things, but related– You can understand basketball (memorization

and labor part, easy part)

– You can play basketball (practice also the creative part, difficult part)

Page 27: 1 Chapter 0 Introduction to C++ Sen Zhang. 2 Computer science Computer science encompasses a variety of topics and subjects that relate to computation

Now ask yourself?

• Do you really think you can write?• How well do you think you can write?• How many years have your studied English

before you reach whatever level you currently have?

• Have your found the answers to the question what do you need to learn in this course?

27

Page 28: 1 Chapter 0 Introduction to C++ Sen Zhang. 2 Computer science Computer science encompasses a variety of topics and subjects that relate to computation

28

Now, let us think about what a language ( say English ) is?

• mostly from the written language point of view– Just a set of rules, grammar, syntax,

• How to Sentence legal or not

– An alphabet = Letters + symbols

– Word

– Phrase

– Sentence

– Paragraphs

– chapters

– articles

– How to Punctuation

Page 29: 1 Chapter 0 Introduction to C++ Sen Zhang. 2 Computer science Computer science encompasses a variety of topics and subjects that relate to computation

What is the most important component a computer?

• CPU• A Central Processing Unit (CPU), or

sometimes just called processor, is a description of a class of logic machines that can execute programs written in its language.

29

Page 30: 1 Chapter 0 Introduction to C++ Sen Zhang. 2 Computer science Computer science encompasses a variety of topics and subjects that relate to computation

CPU mimics brain in a very rudimentary way, but it works

• CPU is composed of several blocks of transistors called units. – Calculating units : Aritmetic Logical Unit, Floating Point Unit and Multimedia unit.

– Control units : They are in charge of instructions decoding, the access to the RAM, ...

– Cache memory units : Cache level 1, Cache level 2, Cache level 3.

• Physical limitations and their solutions.– Burning technologies

– Higher the frequency of CPU is, more it heats. To fix this problem, we can use a finer burning technology.

– Electric conductorAfter 15 years of research, copper offers many advantages compared to aluminium, including a better electric conductivity, a lower energy consumption, and a better resistance in change in temperature.

30

Page 31: 1 Chapter 0 Introduction to C++ Sen Zhang. 2 Computer science Computer science encompasses a variety of topics and subjects that relate to computation

31

1. CPU organization

registers

ALU inputregisters

ALU

A + B

A B

ALU output register

Page 32: 1 Chapter 0 Introduction to C++ Sen Zhang. 2 Computer science Computer science encompasses a variety of topics and subjects that relate to computation

32

What is the language used by machine?

• Binary language– Alphabet {0,1} (or symbols)

– Instructions or words,• The combination of 0, 1 is unlimited• Each combination potentially can be a coding for

an unique instruction

– Sentences• statements

Page 33: 1 Chapter 0 Introduction to C++ Sen Zhang. 2 Computer science Computer science encompasses a variety of topics and subjects that relate to computation

33

Binary language vs. English

• CPU (machine) knows Binary language, but not English. A CPU won’t learn your language

• Human being knows English, but not 001111.• So we need to establish the link between

English and 0s,1s– How to bridge the gap between machines and

human beings

Page 34: 1 Chapter 0 Introduction to C++ Sen Zhang. 2 Computer science Computer science encompasses a variety of topics and subjects that relate to computation

34

High-level language layer

Assembly language layer

Machine language layer

Micro-instruction layer

directly executed

interpreted

assembled

compiled

Hardware layer

Hardware

Application layer

Operating System

Conceptual Layers of a Modern Computer

Page 35: 1 Chapter 0 Introduction to C++ Sen Zhang. 2 Computer science Computer science encompasses a variety of topics and subjects that relate to computation

35

One of the bridges is C++ language

• A middle man!

• C++ is similar to English, can be learned easily by most people. – Yes, people need to learn it like a foreign language!

• Machine knows only 0,1, neither English nor no C++! – so we need a translator, so called compiler, to translate

everything from C++ to machine instructions.

Page 36: 1 Chapter 0 Introduction to C++ Sen Zhang. 2 Computer science Computer science encompasses a variety of topics and subjects that relate to computation

36

Why not a complier translate English to machine code!

• Natural languages are too complicated, too flexible, too creative, too … – full of imagination and rhetoric!

– NLU is a big field!• Before we can fully understand natural languages,

we better retreat to a well defined man made language like C++!

Page 37: 1 Chapter 0 Introduction to C++ Sen Zhang. 2 Computer science Computer science encompasses a variety of topics and subjects that relate to computation

37

Steps in writing C++ code

• Know how to solve it on paper.• Map the solution into the source code that

conforms to C++ grammer• Compile• debug• Execute• Improve,

– tighten, robust, bullet-proof

Page 38: 1 Chapter 0 Introduction to C++ Sen Zhang. 2 Computer science Computer science encompasses a variety of topics and subjects that relate to computation

Programming

• Programming=represent information + process information

38

Page 39: 1 Chapter 0 Introduction to C++ Sen Zhang. 2 Computer science Computer science encompasses a variety of topics and subjects that relate to computation

Information

• Data classified as different types– Numeric data

– Logic data

– Characters and sentences

– …

39

Page 40: 1 Chapter 0 Introduction to C++ Sen Zhang. 2 Computer science Computer science encompasses a variety of topics and subjects that relate to computation

Information representation

• Factorize information– Some are constants, some are variables.

• Information decomposition• Information type identification

40

Page 41: 1 Chapter 0 Introduction to C++ Sen Zhang. 2 Computer science Computer science encompasses a variety of topics and subjects that relate to computation

Information processing

• Operations– simple statements/expression using supported

operators

– Compound expressions by combining supported operations

– Compound statements by combining/grouping multiple statements

– Built in functions

– User defined new functions

41

Page 42: 1 Chapter 0 Introduction to C++ Sen Zhang. 2 Computer science Computer science encompasses a variety of topics and subjects that relate to computation

Information process

• Output, how to display information on screen• Input, how to get information into the program.• Manipulation

Page 43: 1 Chapter 0 Introduction to C++ Sen Zhang. 2 Computer science Computer science encompasses a variety of topics and subjects that relate to computation

43

What we should do?

• Learn rules (put time in, I know you can do it)• Write C++ code by following the rules (need

thinking hardly and be creative, I know not everybody can do that, but I hope I can make as many of as possible to get it if you follow me tightly!)

• Work hard! Time consuming. If you agree writing is creative and time consuming process, you need to allocate enough time. Do not miss any class!

Page 44: 1 Chapter 0 Introduction to C++ Sen Zhang. 2 Computer science Computer science encompasses a variety of topics and subjects that relate to computation

• C++ not our native language, neither machine’s native language, so a foreign language to both of us and machine. We need to learn it and machine needs a translator.

44

Page 45: 1 Chapter 0 Introduction to C++ Sen Zhang. 2 Computer science Computer science encompasses a variety of topics and subjects that relate to computation

How to study this course efficiently!

• Listen to lecture.• Read textbook• Do homework• Ask questions• Participate

45

Page 46: 1 Chapter 0 Introduction to C++ Sen Zhang. 2 Computer science Computer science encompasses a variety of topics and subjects that relate to computation

questions

• Important questions in class, your personal puzzles after class

• Sometimes, need patience, try to find out answer to your questions first. Or just read book.– For some trivial facts, which can be easily obtained from

reading notes and book, all of you can get it easily. Do not need to worry others can get it or not.

– My concern is how to get you understand what you can get out of from reading.

• Your questions might be answered shortly as our class move on. I know most of them will.

• Class time is precious, we need to move on.

46

Page 47: 1 Chapter 0 Introduction to C++ Sen Zhang. 2 Computer science Computer science encompasses a variety of topics and subjects that relate to computation

Many ways

• There are many ways to solve the same problem.• the way I will show you is the best way that emphasizes

the understanding of basic concepts, not necessary the shortest way, which is not my concern. My interest is to give you a solid foundation.

• Walk well before run!!! • “Cool way” or “shortcut” will come naturally once you

understand the basic. And for a real programmer, shortcuts are really trivial, sometimes need to be avoided.

• •

47

Page 48: 1 Chapter 0 Introduction to C++ Sen Zhang. 2 Computer science Computer science encompasses a variety of topics and subjects that relate to computation

I will show you the sequence!

• Not just a final program! For you can find many already from the textbook and the Internet.

• I will do the coding in front of you.• It also means, I will show you all the common

mistakes that could (will) happen to any programmer.

• I will show you how to debug.

48

Page 49: 1 Chapter 0 Introduction to C++ Sen Zhang. 2 Computer science Computer science encompasses a variety of topics and subjects that relate to computation

The best way to learn!

• To see how others do it.• To follow it.• Not just see the final result, which shows no clue

about the order, the sequence, the steps from which the final result has been constructed.

• Not just see the final result, which shows no clue about how it is coded!!

• I will show you how it is coded.

49

Page 50: 1 Chapter 0 Introduction to C++ Sen Zhang. 2 Computer science Computer science encompasses a variety of topics and subjects that relate to computation

I show you the errors

Three types,– Sometimes, they are on purpose.

– Sometimes, I will make typos by accident, but from programming point of view, that is NOT wrong, just part of the programming.

– Sometimes, an improving process. That is part of programming. We need to refine.

In some sense, debugging is programming.

Just like writing is rewriting or refining.

Be patient. Step by step, do not jump!

50

Page 51: 1 Chapter 0 Introduction to C++ Sen Zhang. 2 Computer science Computer science encompasses a variety of topics and subjects that relate to computation

• In order to make examples as simple as possible, some code may not conform to best practices for coding, which will be learned usually in upper level courses.

51