ImageVerifierCode 换一换
格式:PPT , 页数:34 ,大小:772.50KB ,
资源ID:372889      下载积分:2000 积分
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝扫码支付 微信扫码支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【http://www.mydoc123.com/d-372889.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(C++ Basics.ppt)为本站会员(李朗)主动上传,麦多课文库仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知麦多课文库(发送邮件至master@mydoc123.com或直接QQ联系客服),我们立即给予删除!

C++ Basics.ppt

1、C+ Basics,Programming,Introduction to C+,C is a programming language developed in the 1970s with the UNIX operating system C programs are efficient and portable across different hardware platforms C+ is an extension of the C language C programs should run in C+ C+ supports “object-oriented” programm

2、ing,General form of a C+ program,/Program description is first #include directives go next using namespace std; int main() constant declarations go here variable declarations go hereassignment statements go here return 0; ,General form of a C+ program,/simple program #include using namespace std; in

3、t main()/ constant declarationconst double Pi = 3.14159;/ variable declarationsdouble radius;double area;/ assignment statementscout radius;area = Pi * radius * radius;cout “Area : “ area endl;return 0; ,Syntax of the C+ Language,Reserved words (appear in blue in Visual C+) Reserved words have a spe

4、cial meaning in C+. The list of reserved words: asm, auto, bool, break, case, catch, char, class, const, continue, default, delete, do, double, else, enum, extern, float, for, friend, goto, if, include, inline, int, long, namespace, new, operator, private, protected, public, register, return, short,

5、 signed, sizeof, static, struct, switch, template, this, throw, try, typedef, union, unsigned, using, virtual, void, volatile, while,Syntax of the C+ Language,Identifiers (appear in black in Visual C+) An identifier is a name for variables, constants, functions, etc. It consists of a letter followed

6、 by any sequence of letters, digits or underscores Names are case-sensitive. The following are unique identifiers: Hello, hello, whoami, whoAMI, WhoAmI Names cannot have special characters in theme.g., X=Y, J-20, #007, etc. are invalid identifiers. C+ reserved words cannot be used as identifiers. Ch

7、oose identifiers that are meaningful and easy to remember.,Syntax of the C+ Language,Comments (appear in green in Visual C+) Comments are explanatory notes not compiled (i.e. no executable code is generated for comments) Comments are done in two ways: / A double slash starts a single line comment/*

8、A slash followed by an asterisk marks the start of a multiple line comment.It ends with an asterisk followed by a slash */,Syntax of the C+ Language,Compiler Directive: #include It refers to a header file of library functions or variables (e.g. input and output). When you compile your C+ program, th

9、e compiler reads in the contents of the file before compiling the rest of your program. The included file is compiled together with the program. There are two forms of #include: #include / for pre-defined files #include “my_lib.h“ / for user-defined files,Libraries,#include loads the code from the s

10、tandard libraries #include / new I/O library #include / old I/O library #include / standard functions #include / math functions #include / contains random funct #include / time functionusing namespace std; indicates that the new C+ libraries should be used. If this line is left out, then the old ios

11、tream library is loaded: #include ,Constant Declarations,Constants represent permanent values. Their values can only be set in the declaration:const double pi = 3.14159; They can make a program more readable and maintainableConstant declaration syntax: const = ;Examples: const double US2HK = 7.8; co

12、nst double HK2Yuan = 1.07; const double US2Yuan = US2HK* HK2Yuan;,Variable Declarations,A variable is best thought of as a container for a value with an address: Variable declaration syntax:; Examples:int nickel;int penny;A variable must be declared before it can be used.int main()x = 5; / illegal:

13、x was not declared,5342.23,1024,Variable Declarations,A variable can be initialized in a declaration:int x = 3; Several variables of the same type can be declared in the same declaration:double total_USD, area; A variable must have only one type. For example, a variable of the type int can only hold

14、 integer values.,Types of Variable Declarations,Simple C+ variable types: int integer (32-bit integer on the PC) (example: 1) short 16-bit integer (allows 32,767) long 32-bit integer (allows 2,147,483,647) float floating point number (allows about 7 digits of precision: 0.1234567) double double prec

15、ision float (allows about 15 digits of precision: 0.12345678901234) char a single character (example: y),BINARY DIGIT,Simple Number System,Decimal What is the maximum value a 3 decimal digit number can hold? 999,Binary What is the maximum value a 16-binary digit number can hold?one bit for sign 0: p

16、ositive (or zero) 1: negative +32767,Decimal base,# digits,# digits,Binary base,Memory Depiction,float y = 12.5; short Temperature = 32; char Letter = c; short Number;,Memory location,Assignment Statements,Assignment syntax:= ;Examples:int n, m, k; / declarationn = 5;m = 6 + (4 * 6);k = (n * 2) + m;

17、k = k / 2;,Assignment Statements,A variable must be assigned a value before it can be used. Variables are not automatically initialized in VC+. int x, y; / x declared, but not initialized/ x and y have random valuesy = x; / the random value in x assigned to yOnce a value has been placed in a variabl

18、e, it stays there until the program changes it.,Assignment Statements,int NewStudents = 6; int OldStudents = 21; int TotalStudents;TotalStudents = NewStudents + OldStudents ;,Assignment Statements,TotalStudents = NewStudents + OldStudents;OldStudents = TotalStudents;,Value1 = 10; Value2 = 20;int Hol

19、d = Value1;Value1 = Value2;Value2 = Hold;,Arithmetic Operators,Common Addition + Subtraction - Multiplication * Division / Mod % Note No exponentiation operator,Integer Division,Integer division produces an integer result It rounds down the result Examples 3 / 2 evaluates to 1 4 / 6 evaluates to 0 1

20、0 / 3 evaluates to 3,Mod,Produces the remainder of the division Examples5 % 2 evaluates to 112 % 4 evaluates to 04 % 5 evaluates to 4,Converting inches to feet and inches,63 inch = 5 foot 3 inch 5 = 63 / 12 3 = 63 % 12,Operators and Precedence,Which of the following is it equivalent to mx + b ? (m *

21、 x) + b m * (x + b) Operator precedence tells how to evaluate expressions Standard precedence order ( ) Evaluated first, if nested innermost done first * / % Evaluated second. If there are several, then evaluate from left-to-right + - Evaluate third. If there are several, then evaluate from left-to-

22、right,Operator Precedence,Examples1 + 2 * 3 / 4 - 5 = 1 + 1 - 5 = -32 * 4 / 5 + 3 * 5 % 4 = 6/5 + 15%4 = 1+3 = 43.0 * 3 / 4 = 9.0 / 4 = 2.25(1 + 3) * (2 + 4 * 6) * 3) / 2 + 2 = 4 * (26*3)/2 + 2 = 158,Standard Input/Output,cin - the standard input streamInput operator “” extracts data from input “str

23、eam” (the keyboard by default) skips over white spaces extracts only characters of the right form and performs automatic conversion to the type specified,Standard Input/Output,cout - the standard output stream Output operator “ id score; cout “Student ID: “ id “ score: “ score endl;,/ Convert inches

24、 to feet and inches / Input: inches / Output: feet and inches #include using namespace std; int main() / inches to feet conversion factor const int in2feet = 12; int inches; / number of inches int feet; / number of feet cout inches; / Convert inches to feet and inches feet = inches / in2feet; inches

25、 = inches % in2feet; cout feet “ feet “ inches “ inches “ endl;return 0; ,Assignment Conversions,A floating-point expression assigned to an integer object is always rounded down (truncated) An integer expression assigned to a floating-point object is converted to a floating-point value Example 1:flo

26、at y = 2.7;int i = 15;int j = 10;i = y; / i is now 2cout i endl;y = j; / y is now 10.0cout y endl;,Assignment Conversions,Example 2: int m, n; double x, y; m = 3; n = 2.5; / 2.5 converted to 2 and assigned to n x = m/n; / 3/2=1 converted to 1.0 and assigned to x n = x+m/2;/ m/2=1 : integer division/

27、 x+m/2 : double addition because x is double/ convert result of m/2 to double (i.e. 1.0)/ x+m/2=2.0/ convert result of x+m/2 to int (i.e. 2)/ because n is int,White Spaces,Extra blanks or tabs (white spaces) are ignored x=3; x = 3 ; Blank lines and comments are treated as white spaces Code can be in

28、dented in any way More than one statement can be on one line int x, y; x=3; y = 10; int z = x+y; A single statement can be continued over several lines cout feet “ feet and “ inches “ inches“ endl;,Good Programming Style,Place each statement on a line by itself (except long cout statements)x = m/n;

29、Use blank lines to separate sections of codeUse the same indentation for all statements in the same block of code . (“Format selection”, ALT-F8, will automatically fix indentation)int main()int x;x = 5; return 0;,Good Programming Style,Use meaningful identifier namesdouble area, sum, radius;Document

30、 each variable when it is declareddouble area; / area of the circledouble distance; / distance from top to bottomDocument each segment of code / Convert inches to feet and inches feet = inches / in2feet; inches = inches % in2feet;Document the beginning of the program with a header that tells the purpose of the program/ Convert inches to feet and inches,

copyright@ 2008-2019 麦多课文库(www.mydoc123.com)网站版权所有
备案/许可证编号:苏ICP备17064731号-1