上海交通大学交大密西根 ·联合学院一 181 UM-SJTU Joint Institute ■ University of Michigan Shanghal Jiao Tong University Vg101:Introduction to Computer and Programming Data Types and Expressions The McGraw-Hill Companies,Inc.,2000
Vg101:Introduction to Vg101:Introduction to Computer and Programming Computer and Programming Data Types and Expressions © The McGraw-Hill Companies, Inc., 2000
Objectives To write C++programs to perform simple calculations (2.2). To read input from the keyboard using the cin object($2.3). To simplify programming by omitting the std::prefix (2.4). To use identifiers to name variables,constants,functions,and classes (2.5). To use variables to store data(§§2.6-2.7). To program with assignment statements and assignment expressions (2.7) To use constants to store permanent data($2.8). To declare variables using numeric data types($2.9). To use operators to write numeric expressions (2.9). To convert numbers to a different type using casting (2.10). To represent character using the char type (2.11). To become familiar with C++documentation,programming style,and naming conventions(§2.l3). To distinguish syntax errors,runtime errors,and logic errors($2.14) 。 To debug logic errors (2.15)
Objectives Objectives • To write C++ programs to perform simple calculations (§2.2). • To read input from the keyboard using the cin object (§2.3). • To simplify programming by omitting the std:: prefix (§2.4). • To use identifiers to name variables, constants, functions, and classes (§2.5). • To use variables to store data (§§2.6-2.7). • To program with assignment statements and assignment expressions (§2.7). • To use constants to store permanent data (§2.8). • To declare variables using numeric data types (§2.9). • To use operators to write numeric expressions (§2.9). • To convert numbers to a different type using casting (§2.10). • To represent character using the char type (§2.11). • To become familiar with C++ documentation, programming style, and naming conventions (§2.13). • To distinguish syntax errors, runtime errors, and logic errors (§2.14). • To debug logic errors (§2.15)
上海交通大学交大密西根 联合学院·一 ◆ 18 UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University Parts of a Program Comments Preprocessor Commands Words:Keywords and other common concepts Declarations (classes objects,constants, variables,and functions) Statements (simple assignments,compound, loop,conditional,and branching statements)
Parts of a Program Parts of a Program • Comments • Preprocessor Commands • Words: Keywords and other common concepts • Declarations (classes & objects, constants, variables, and functions) • Statements (simple assignments, compound, loop, conditional, and branching statements)
The "Hello World"Program One of the important 1.1 Getting Started influences on the design of The only way to learn a new C++was the C,which was programming language is to write programs in it.The first program developed at Bell Labs in the to write is the same for all early 1970s.The primary languages: reference manual for C was Print the words written by Brian Kernighan hello,world and Dennis Ritchie. This is the big hurdle;to leap over it you have to be able to create the On the first page of their program text somewhere,compile book,the authors suggest that it,load it,run it,and find out where your output went.With the first step in learning any these mechanical details language is to write a simple mastered,everything else is program that prints the comparatively easy.In C,the program to print "hello,world"is message“hello,.world”on the #include display.That advice remains main(){ sound today. printf("hello,world");
The “Hello World Hello World ” Program Program • One of the important influences on the design of C++ was the C, which was developed at Bell Labs in the early 1970s. The primary reference manual for C was written by Brian Kernighan and Dennis Ritchie. • On the first page of their book, the authors suggest that the first step in learning any language is to write a simple program that prints the message “hello, world” on the display. That advice remains sound today. 1.1 Getting Started The only way to learn a new programming language is to write programs in it. The first program to write is the same for all languages: Print the words hello, world This is the big hurdle; to leap over it you have to be able to create the program text somewhere, compile it, load it, run it, and find out where your output went. With these mechanical details mastered, everything else is comparatively easy. In C, the program to print “hello, world” is #include main() { printf("hello, world"); }
上海交通大学交大密西根 联合学院·一 ◆ 81 UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University Our First Program Our first #include C++ #include "MsgConsoleT.h" using namespace std; program will This is the first C++program It will display a welcome message for you displays a message int main (void) “Welcome to 9 the Vg101class' MsgConsoleT consolej on the console.printLine (Welsome to the Vg101 class!n) console. return 0;
Our First Program Our First Program • Our first C++ program will displays a message “Welcome to the Vg101class ” on the console
上海交通大学交大密西根 联合学院·一 ◆ 181 UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University Composition of a Program Preprocessor those being included will be inserted into your program before #include compilation for Vg101class.lib specially #include“MsgConsoleT.h” designed for this course using namespace std; is defined for this prog. /display welcome msg * ● using namespace std /necessary int main (void) Enclosed in /*..*are comments Main function:every console program will have one and only one main function Start of the function body Body of the main function )end of the function body
Composition of a Program Composition of a Program #include #include “MsgConsoleT.h” using namespace std; /* display welcome msg */ int main (void) { …. } • Preprocessor : those being included will be inserted into your program before compilation • for Vg101class.lib specially designed for this course • is defined for this prog. • using namespace std // necessary • Enclosed in /* … */ are comments • Main function: every console program will have one and only one main function • ‘{‘ Start of the function body • Body of the main function • ‘}’ end of the function body
上海交通大学交大密西根 联合学院·一 ◆ ■ 181 UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University Composition of a Function int main (void) main function declaration ‘'Function body start MsgConsoleT console; Variable declaration;here an object called console is declared to be of class MsgConsoleT console.printLine Method function printLine of ("welcome to the Vg101 class ConsoleT is called. class"); return 0; Return statement End of the function body
Composition of a Function Composition of a Function int main (void) { MsgConsoleT console; console.printLine ("welcome to the Vg101 class“); return 0; } • main function declaration • ‘{’ Function body start • Variable declaration; here an object called console is declared to be of class MsgConsoleT • Method function printLine () of class ConsoleT is called . • Return statement • ‘}’ End of the function body
上海交通大学交大密西根 联合学院 ■ UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University A few More Details To use the member date and function defined in another class,you could declare that class to be the base class of your class using syntax like class :public In your later homework,your might need to define a particular class for the task you are required to accomplish.For the purpose of automatic program grading,you will be given some function like disp (so that you can focuses on algorithm design instead of the output format
A few More Details A few More Details • To use the member date and function defined in another class, you could declare that class to be the base class of your class using syntax like class : public • In your later homework, your might need to define a particular class for the task you are required to accomplish. For the purpose of automatic program grading, you will be given some function like disp () so that you can focuses on algorithm design instead of the output format
上海交通大学交大密西根 联合学院· 81 UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University About ConsoleT If your class will be run in a console environment (text based window),your class should have a base class called ConsoleT defined by the class. There are a few usefule functions defined in ConsoleT like printLine O and readInt O as you will see in the later examples.You can use it to print multiple items as the“cout”used in your textbook. There is a count part of the printLine called readLine,which is equivalent to“cin
About ConsoleT ConsoleT • If your class will be run in a console environment (text based window), your class should have a base class called ConsoleT defined by the class. • There are a few usefule functions defined in ConsoleT like printLine () and readInt () as you will see in the later examples. You can use it to print multiple items as the “cout” used in your textbook. • There is a count part of the printLine () called readLine (), which is equivalent to “cin
functions defined in ConsoleT int readInt (string msg); double readDouble (string msg); string readString (string msg); void runTimeError (string msg); void printLine(.…); void readLine (...) ● To use method functions defined in ConsoleT,you first define an object which is an instance of ConsoleT
functions defined in functions defined in ConsoleT ConsoleT int readInt (string msg); double readDouble (string msg); string readString (string msg); void runTimeError (string msg); void printLine (…); void readLine (…); • To use method functions defined in ConsoleT, you first define an object which is an instance of ConsoleT ConsoleT console ;