C++ Basics.ppt
《C++ Basics.ppt》由会员分享,可在线阅读,更多相关《C++ Basics.ppt(34页珍藏版)》请在麦多课文档分享上搜索。
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
- 1.请仔细阅读文档,确保文档完整性,对于不预览、不比对内容而直接下载带来的问题本站不予受理。
- 2.下载的文档,不会出现我们的网址水印。
- 3、该文档所得收入(下载+内容+预览)归上传者、原创作者;如果您是本文档原作者,请点此认领!既往收益都归您。
下载文档到电脑,查找使用更方便
2000 积分 0人已下载
下载 | 加入VIP,交流精品资源 |
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- CBASICSPPT
