为了正常的体验网站,请在浏览器设置里面开启Javascript功能!

达内C++

2013-05-25 50页 ppt 2MB 57阅读

用户头像

is_925695

暂无简介

举报
达内C++nullThe C++ Programming Language Chapter 1The C++ Programming Language Chapter 1C++ Programming in UNIXC++ Programming in UNIX课程介绍 C++ 语法基础 面向对象程序设计的概念 大量的编程实践 目标 熟练掌握C++语法 具有面向对象程序设计的概念与能力 能熟练阅读复杂的C++程序源代码 能独立的设计与完成面向对象的C++程序课程内容简介 1课程内容简介 1C++语言基础 保留字 变量,常量 表达...
达内C++
nullThe C++ Programming Language Chapter 1The C++ Programming Language Chapter 1C++ Programming in UNIXC++ Programming in UNIX课程介绍 C++ 语法基础 面向对象程序设计的概念 大量的编程实践 目标 熟练掌握C++语法 具有面向对象程序设计的概念与能力 能熟练阅读复杂的C++程序源代码 能独立的设计与完成面向对象的C++程序课程内容简介 1课程内容简介 1C++语言基础 保留字 变量,常量 达式 语句 函数 程序的结构 数据结构与算法 数组、指针、引用、结构、链表与栈课程内容简介 2课程内容简介 2C++面向对象编程 类 构造函数与析构函数 静态成员与友员 函数重载 继承与多态 I/O流 模板 异常程序设计语言介绍1程序设计语言介绍1What computer understand? bits Assembler Language Limited structure Global scope Machine code Primitive High-Level Language Function decomposition Data separation High level structure程序设计语言介绍2程序设计语言介绍2Block Structured Language Encapsulation Flexible data scoping Modularization Object-Oriented Language Inheritance Polymorphism Abstract data typesC++程序设计语言C++程序设计语言1972,AT&T, Bell Lab. Dennis Ritche, C language 1980, Bell Lab. Bjarne Stroustrup, C extension, 1983, C++ named 1997, ANSI (American National Standards Institute) C++ (standard C++)What C++ will we learn in this course ?What C++ will we learn in this course ?Standard C++: ANSI C++ is more scalable to different platform such as Unix, Microsoft windows, Mac… The standard C++ library got supported by most of industry providers. You’d better don’t know C. ........??? We will try to avoid C library and C syntax. A C++ file can be with .cc, .cp, .cpp extensions. 为什么选标准 C++为什么选标准 C++ANSI 规范了C++的标准, 使之具有高度的可移植性。 C++ 程序能够运行得很快,可直接操作系统资源,保持机器层次的实现细节。 C++ 不图形环境, 对系统要求相对较低。 易于解决与旧工程的接口以及在数据库,存储和性能方面的技术限制。 C++ 是一种面向对象的多范型语言,可以将面向对象的模型映射成为C++ 的结构。它为开发者设计和编写一个解决提供了一定的选择范围。C++的优点C++的优点Supports data abstraction and object-oriented programming Contains all existing features of C, making the migration from C to C++ relatively easy Is as portable and efficient as C Can be linked to existing C libraries and functions Provides strong static-type checking Is a general-purpose language程序员应该具备的计算机知识1程序员应该具备的计算机知识1操作系统与应用程序 运行环境与运行机制 系统与命令 运行环境与命令行参数 进程 栈 堆Binary and HexadecimalBinary and HexadecimalBinary: 0101 1000 Decimal: 88 Hexadecimal: 0x58 1 byte = 8 bits.程序员应该具备的计算机知识2程序员应该具备的计算机知识2编辑器 编译器 编译器的功能 解释执行与编译执行的差别 熟悉自己常用的编译器,查错能力 连接器 库与库函数 系统调用软件开发周期软件开发周期Software LifecycleSoftware Lifecycle开发 测试 维护 更新熟悉你的环境熟悉你的环境可用的UNIX服务器 www.openlab.com.cn 192.168.0.21 192.168.0.23 192.168.0.26 Telnet命令介绍: telnet 192.168.0.21 Login: use your registered user account. Password: type in your pass word. 创建自己的学习帐号创建自己的学习帐号telnet www.openlab.com.cnlogin: tarena Password: tarena 欢迎使用达内科技(中国)公司开放实验室的服务! Welcome to the OpenLab of Tarena Technologies Inc. Cananda. 请按照以下提示创建您的用户帐号. Please follow the steps to create your own account. (请输入您要注册的帐号)Please enter your new account name:XXXXXXXX (请输入您的E-Mail地址)Please enter your email address: XXX@YYY.ZZZ 用自己的帐号登录UNIX服务器用自己的帐号登录UNIX服务器Escape character is '^]'. SunOS 5.8 login: XXXXXXX Choose a new password. New password: *******程序员经常用到的UNIX命令1程序员经常用到的UNIX命令1简单的文件维护与管理 ls, cd, mkdir, rm, cp, mv, cat, more 源程序的编写 vi, ed 编译与连接 gcc, g++, ld 运行与调试 adb,gdb程序员经常用到的UNIX命令2程序员经常用到的UNIX命令2查看运行状态 % ps –ef % grep aaa a.txt (aaa is the chars in the file name a.txt) % prstat (ctrl D to exit) % kill pid (pid is a process id)第一个UNIX上的C++程序第一个UNIX上的C++程序用vi编辑器来编写hello.cc源程序 % vi hello.cc/*the first C++ program*/ #include using namespace std; //main function int main() { cout << "Hello world!" << endl; cout << "This is my first C++ program.\n"; }g++的常用参数g++的常用参数- c 编译成目标文件.o - o指定输出文件名,输出文件名跟在-o后面,用空格分隔。如果不使用这个选项,缺省的输出文件名为a.out。 - g产生有调试信息的可执行文件 - w不产生警告信息 - l 连接指定的库文件 - L指定库文件的路径 - i 要包含的头文件 - I 头文件的路径 - E 显示预处理后的程序文件到屏幕上,可以用-o指定输出到文件 - S 产生汇编程序 如果没有c、E、S就会生成可执行文件编译hello.cc编译hello.cc% g++ -c hello.cc % ls连接hello.o连接hello.o% g++ -o hello hello.o % ls % g++ hello.o % ls运行hello 程序运行hello 程序% hello % a.out C++程序的基本结构1C++程序的基本结构1/*the first C++ program*/ #include using namespace std; //main function int main() { cout << "Hello world!" << endl; cout << "This is my first C++ program.\n"; }C++程序的基本结构2C++程序的基本结构2#include< > 与 #include" " Name space: 提供了一个全局标识符和全局变量所在的作用域。 int main() 注释 函数 函数的调用 cout语句头文件头文件#include语句 #include < > 与 #include " "使用 #include using namespace std; 少用 #include Main函数Main函数main函数的作用 Standard C++ main( )格式: int main( ) { … return 0; //the default return value is 0; } 注释注释C++的注释基本的输出语句基本的输出语句cout练习程序hi.cc练习程序hi.cc#include using namespace std; int main( ) { cout << "Hi Jian!" << endl; cout << "Have a nice day." << endl; return 0; }练习程序myself.cc练习程序myself.cc编写一个程序,打印出自己的: 姓名 性别 年龄 家庭住址 电话号码 爱好 每一条信息输出为一行在hi.cc中使用字符串在hi.cc中使用字符串#include using namespace std; int main() { char name[ ] = "John"; cout << "Hi " << name << "!" << endl; cout << "Have a nice day." << endl; return 0; }字符与字符串类型字符与字符串类型字符,字符串/字符数组 char ch = ‘A’; char str1[20] = "Hello world!"; char str2[ ] = "Have a nice day!";不同的main()格式不同的main()格式命令行参数 % ls –l (or ls -al ) % vi hello.cc 在程序中使用命令行参数 int main(int argc, char *argv[ ])命令行参数程序cmdline.cc命令行参数程序cmdline.cc#include using namespace std; int main (int argc, char* argv[ ]) { for(int i=0; i using namespace std; int main() { unsigned int age; char name [50]; cout << "please enter your name:" << endl; cin >> name; cout << "please enter your age:" << endl; cin >> age; cout << "your name is:" << name << endl; cout << "You were " << age -2 << " years old two years ago. \n"; }条件语句条件语句if语句 if…else语句 不同if的等价与不等价形式 … char ch; cin >> ch; if (ch == ‘y’) //note the difference: if ( ch = ‘y’) cout << "good" << endl; else cout << "try again." << endl; …练习程序grade.cc练习程序grade.ccThis is your assignment. Q & AQ & AThe C++ Programming Language Chapter 2The C++ Programming Language Chapter 2字符集字符集abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 0 1 2 3 4 5 6 7 8 9 _ + - * / % = . , : ? ’ \ " ~ | ! # & ( ) [ ] { } ^ < > 空白C++的保留字C++的保留字auto, bool, break, case, catch, char, class, const, const_cast , continue, default, delete, do, double, else, enum, extern, false, float, for, friend, goto, if, inline, int, long, new, operator, private, protected, public, return, short, signed, sizeof, static, struct, switch, template, this, throw, true, try, typedef, union, unsigned, virtual, void, while, …常量与变量常量与变量内存 程序的内存使用 常量 变量 动态内存 变量的类型C++变量名C++变量名变量名(identifier) 第一个字符必须是字母或下划线 只能使用字母,数字,或下划线 中间不能有空格 不能是保留字,不能与全局函数和数据类型同名 C++严格区分大小写(UNIX中……) 使用易懂的变量名(一般是相关的或者缩写) 长度一般不要超过32个字符 不单是变量名,所有需要自己起名的地方都需要遵守这样的规则。C++变量C++变量C++是强类型语言 每一个变量都有确定的类型,且保持不变 基本数据类型 整型,int, 字符型, char, 实型, float, double, 逻辑型, bool ( standard c++ new feature ! )基本数据类型1基本数据类型1char, unsigned char, signed char, int, unsigned int, signed int, short int, unsigned short int, signed short int, long int, signed long int, unsigned long int, float, double, long double, bool void :enum,struct,union,array,pointer,class基本数据类型2基本数据类型2实型数据 (float, double…) 无unsigned.The standard C++ new featuresThe standard C++ new featuresbool: 其值必为关键字true 或false 四个转型运算子: static_cast: compiling time to check data type(primitive). const_cast: only for constant data type converting. dynamic_cast: usually for top-down data type cast. reinterpret_cast: more general cast for all data types. (到多态的时候再详细讲)变量与数据类型变量与数据类型C++是强类型语言 先声明,后使用 C++编译器对变量声明的处理一个使用变量的程序例子一个使用变量的程序例子#include using namespace std; int main( ) { int i; i = 5; cout << "i = " << i << endl; i = 8; cout << "i = " << i << endl; return 0; }另一个使用变量的例子程序另一个使用变量的例子程序#include using namespace std; int main( ) { i = 5; //see what happens… cout << "i = " << i << endl; i = 8; cout << "i = " << i << endl; return 0; int i; }变量与变量的size变量与变量的size变量都有类型 变量在内存中的大小 int i; double d; cout << "size of i is " << sizeof( i ) << endl; cout << "size of int is " << sizeof( int ) << endl; cout << "size of d is " << sizeof( d ) << endl; cout << "size of double is " << sizeof( double ) << endl;程序size.cc程序size.cc编写一个程序,打印出所有C++基本类型的大小 #include using namespace std; int main() { cout << "size of char is: " << sizeof(char) << endl; cout << "size of unsigned char is: " << sizeof(unsigned char) << endl; cout << "size of signed char is: " << sizeof(signed char) << endl; cout << "size of int is: " << sizeof(int) << endl; cout << "size of unsigned int is: " << sizeof(unsigned int) << endl; cout << "size of signed int is: " << sizeof(signed int) << endl; cout << "size of short int is: " << sizeof(short int) << endl; …… }变量的取值范围变量的取值范围变量的类型与值的范围 常用类型的取值范围常量常量常量与常量的数据类型 const double pi=3.14const限定符const限定符限定一个常量或者函数 方便编译器来检测非法的修改操作运算符运算符运算符 +,-,*,/,%,… ++,--, ==, >, <, >=, <=, != !, &&, || & | ^ ~ << >> 结合性 优先级: see table 3-1 in page 35 in the recommended book 1.运算符的使用运算符的使用if ( demo = 2 ) 与 if (demo == 2 ) if ( 2== demo ) //左值与右值。 if ( demo != 2 ) 与 if ( demo =! 2 )运算符的优先级运算符的优先级int a=8,b=4,c=5; cout << (a%b ? b : c); cout << a%b ? b:c; 变量的赋值变量的赋值赋值表达式 变量的初始化 一次声明多个变量 声明并初始化变量无符号类型的值无符号类型的值无符号整数类型的回绕 unsigned short int snum; snum = 65535; cout << "snum=" << snum << endl; snum = snum + 1; cout << "snum=" << snum << endl;有符号类型的值有符号类型的值有符号整数类型的回绕 int inum = 2147483647; cout << "inum = " << inum << endl; inum = inum + 1; cout << "inum = " << inum << endl;常用类型的取值范围常用类型的取值范围常用类型的取值范围 int i = 65535; int j = 65535; cout << i*j/9 << endl;练习练习为表示如下数据,应该使用什么类型的变量: 年龄 姓名 工资 电话号码 身份证号码 西三环到东三环的距离练习程序bin.cc练习程序bin.cc#include using namespace std; int main( ) { int a = 10; cout << "Please enter a number:" << endl; cin >> a; unsigned int r; int k; unsigned int j; char str[33]; memset(str, '0', 33); str[32]='\0'; r = a; int i = 32;练习程序bin.cc练习程序bin.ccdo { j = r; r = r/2; k = j - r * 2; if (k) str[--i] = '1'; else str[--i] = '0'; } while(r != 0); cout << str << endl; }枚举类型枚举类型enum color{RED, GREEN, BLUE, WHITE, BLACK}; color tvColor = GREEN; Enum constant variable does not need memory allocation. The default value in Enum is: 0, 1, 2, 3, 4…. Specify the element values: enum color {RED=100, GREEN=200, BLUE, WHITE=300, BLACK=400} 表达式表达式左值与右值 float a; a = 5/2; ++a; 求值顺序 特殊表达式: ( ? : ) 逗号表达式: int a, b, c; a = 1, b=a=2, c=b+3; 表达式的求值顺序表达式的求值顺序求值顺序 副作用表达式的左值与右值表达式的左值与右值int a, b, c,d; int e = (a = 1, b = a, c = a+b, d = c + 5); (a = 1, b = a, c = a+b, d = c + 5) = 8; e = (a = 0, b = a + 5, b+2); (a = 0, b = a + 5, b+2) = 9; //ohps …, you may get problem... 练习程序comma.cc练习程序comma.cc#include using namespace std; int main( ) { int a, b, c,d; int e = (a = 1, b = a, c = a+b, d = c + 5); (a = 1, b = a, c = a+b, d = c + 5) = 8; cout <<"d=" << d << endl; }程序语句程序语句控制语句 表达式语句 空语句 语句块控制语句控制语句条件判断语句 if ( ) { … } if ( ) {…} else {…} 循环控制语句 while( ) { … } do { … } while( ); for ( ) { … } 多路选择语句 switch( ) { case: }循环语句循环语句for语句 do…while语句 while语句 break语句 continue语句分支语句分支语句switch语句 switch(ss) { case 1: … break; case 2: … break; default: break; };循环语句程序例子 chengFa.cc循环语句程序例子 chengFa.cc编写一个程序,打印出乘法口诀表 分别使用for语句,do…while语句,while语句来实现The output is: 1x1=1 1x2=2, 2x2=4 1x3=3, 2x3=6, 3x3=9 1x4=4, 2x4=8, 3x4=12, 4x4=16 1x5=5, 2x5=10, 3x5=15, 4x5=20, 5x5=25 1x6=6, 2x6=12, 3x6=18, 4x6=24, 5x6=30, 6x6=36 1x7=7, 2x7=14, 3x7=21, 4x7=28, 5x7=35, 6x7=42, 7x7=49 1x8=8, 2x8=16, 3x8=24, 4x8=32, 5x8=40, 6x8=48, 7x8=56, 8x8=64 1x9=9, 2x9=18, 3x9=27, 4x9=36, 5x9=45, 6x9=54, 7x9=63, 8x9=72, 9x9=81练习程序year.cc练习程序year.ccThis is your assignment. 判断输入的年份是否是闰年。Q & AQ & AThe C++ Programming Language Chapter 3The C++ Programming Language Chapter 3函数函数什么是函数 函数的基本要素 参数 返回值 函数的声明与定义 形参与值参 函数的调用定义函数定义函数<返回类型> 函数名( <参数表> …) { … return … } <参数表> <参数类型> <参数名称>函数定义的例子函数定义的例子#include using namespace std; void disp(char str[ ] ) { cout << "This is your string: " << str << endl; } int main( ) { char course1[ ] = "C++"; char course2[ ] = "Java"; char course3[ ] = "Oracle"; char course4[ ] = "UNIX"; disp( course1 ); disp( course2 ); }函数声明函数声明<返回类型> 函数名( <参数表> …); void disp( char* ); float average(int, int); float average(int a, int b); 为什么需要函数声明?调用函数调用函数函数的形参 函数的调用过程 填入值参 获得返回值栈的技术简介栈的技术简介栈的工作原理 函数的调用与栈栈的原理栈的原理i = f1( ); j = f2( ); cout << i << endl; cout << j << endl;cout << f1( ) << endl; cout << f2( ) << endl; #include using namespace std; int f1(); int f2(); int main( ) { int i, j; i = f1( ); j = f2( ); cout << i << endl; cout << j << endl } int f1( ) { int n = 5000; return n; } int f2( ) { int n; return n; } 变量的作用域变量的作用域局部变量与全局变量默认参数默认参数函数的形参可以指定默认值 必须从右到左指定参数的默认值 函数在调用时,是按从左到右的顺序在匹配参数使用默认参数的函数例子使用默认参数的函数例子enum Sex{MALE, FEMALE}; void disp(char *name, Sex gender=MALE); 如果在函数声明中指定了默认值,则在函数定义时不能再指定内联函数内联函数提高程序运行效率 内联函数的定义 inline int isnumber(char ch) { return ((ch>=‘0’ && ch<=‘9’) ? 1 : 0); } 必须先定义,不支持函数原形(声明) 不支持结构控制语句递归函数递归函数一个函数调用它自己 如何正确的递归 必须有结束的条件 并且该条件一定能够满足使用与不使用递归的例子程序使用与不使用递归的例子程序编写一个程序bigsum.cc ,使用一个函数来求n的值: #include using namespace std; int bigsum(int a) { if(a == 0) return 0; return a + bigsum(a - 1); } int main() { int n; cout << "Please input a number: "; cin >> n; int m = bigsum(n); cout << "The sum is: " << m; cout << endl; } 练习程序nbang.cc练习程序nbang.cc使用递归函数,编写一个程序来求n!的值: This is your assignment…函数的重载函数的重载C++中的函数可以重载 什么是函数的重载: 对于在不同类型上作不同运算而又用同样的名字的情况,称为重载。 函数重载的注意事项: 重载函数至少在参数个数,参数类型, 或参数顺序上有所不同。 函数重载的例子函数重载的例子求两个数的平均值 double Average(int, int); double Average(float, float); double Average(double, double); double Average(long, long);思考思考题 在一个程序中定义了这两个函数会怎样? int Area(int width, int length=1); int Area(int size);Area.cc source codeArea.cc source code#include using namespace std; int Area(int width, int length = 1) { return width * length; } int Area(int size) // int Area(int size, int leng) is not allowed { return size * size; } int main( ) { cout << Area(3, 5) << endl; //set length = 5 instead. cout << Area(10) << endl; //is this ok? }函数参数的const限定函数参数的const限定可以使用const限定词来修饰形参以保护实参不被修改。 const形参的意义 #include using namespace std; void disp(const int I) { cout << I << endl; I = 100; // think about this. } int main( ) { disp(50); }程序的结构程序的结构多文件结构 外部变量与内部变量 变量的作用域与可见性 头文件 静态全局变量 静态函数多文件结构多文件结构按不同的功能模块将程序的源代码划分在多个文件中 不同源文件之间可以共享变量声明与类型定义 C++多文件的划分原则外部变量与内部变量外部变量与内部变量存储类:auto, extern, register, static, volatile. 什么是外部变量 什么是内部变量 变量的作用域与可见性变量的作用域与可见性局部变量的作用域 静态局部变量的作用域 全局变量的作用域 外部变量的作用域 常量的作用域头文件头文件头文件的作用 如何组织头文件 头文件的使用 编译选项静态全局变量静态全局变量何为静态全局变量 只在本源文件中可用静态函数静态函数定义静态函数 只在本源文件中可用改写bigsum.cc与nbang.cc程序改写bigsum.cc与nbang.cc程序将bigsum.cc与nbang.cc改写成多文件结构,每个文件只能有一个函数改写bigsum.cc与nbang.cc程序改写bigsum.cc与nbang.cc程序分别将前面的函数改为静态函数,再次编译并运行所有的程序 This is your assignment…练习程序hash.cc练习程序hash.cc#include using namespace std; int main( ) { char line[100]; cout << "Please enter a string:" << endl; cin >> line; int ch = 0; for(int i=0; i using namespace std; int main( ) { cout << "Please enter a line:" << endl; char line[120]; int cnt = 0; cin.getline(line, 120); int i = 0; 练习程序mywc.cc练习程序mywc.ccwhile (i 数组名[元素个数]; 下标是数组元素到开始的偏移量 数组下标从0开始 char buf[4];数组2数组2数组在声明时,元素个数必须是常量或常量表达式 char buf[10]; int I; char buf[I]; //??? int I = 10; char buf[I]; //??? const int i = 10; char buf[i]; char buf[i+1];数组3数组3如果数组的声明带有初始化,可以直接对整个数组赋值 访问数组元素,使用下标操作符[ ] int iA[10]; iA[0] = 0; iA[1] = 1; int I = 0; I = iA[0] + iA[1]; 数组的初始化数组的初始化在声明的时候就初始化 int iA[5] = {0,1,2,3,4}; int iB[ ] = {1,2,3}; 使用赋值语句初始化数组 iA[0] = 0; iA[1] = 1; 数组的边界问题 int iC[5]; iC[10] = 100; //run time ??? Question: does C++ compiler have array bound checking?数组程序例子数组程序例子编写一个程序,从键盘接受一个字符串,将该字符串颠倒顺序,然后打印出来…练习程序findmax.cc练习程序findmax.cc#include using namespace std; int main( ) { int iA[ ] = {103, 5, 68, 115, 32, 23, 66, 599, 38, 444}; for(int i = 0; i < 10; i++) { for(int j = 0; j < i; j++) { int temp; if(iA[i] < iA[j]) //if(iA[i] < iA[j]) { temp = iA[i]; iA[i] = iA[j]; iA[j] = temp; } } } cout << "The bigNumber is: " << iA[9] << endl; }多维数组多维数组二维数组与多维数组: int iA[5][10]; int iB[2][3] = { {1,2,3}, {4,5,6}}; int iC[2][4][6]; 多维数组的初始化 仅有第一个维数可以省去 int iB[ ][3] = { {1,2,3}, {4,5,6}, {7,8,9}};数组练习程序mdim.cc数组练习程序mdim.cc#include using namespace std; int maximum(int [][4], int, int); int main ( ) { int sg[3][4] = { {68,77,73,86}, {87,96,78,89}, {90, 70, 81, 86}}; cout << "the max grade is " << maximum(sg, 3,4) << endl; }int maximum(int grade[ ][4], int row, int col) { int max = grade[0][0]; for (int i=0; i < row; i ++) for (int j=0; j< col; j++) if(grade[i][j] > max) max = grade[i][j]; return max; } 结构1结构1将不同类型的相关数据信息组织在一起 是用户自定义的类型 需要先声明类型的定义才能使用 结构与数组的区别 数组只是同一个数据类型的聚集 数组本身不是一个新的数据类型结构2结构2struct <结构名> { <成员类型> <成员变量名>; <成员类型> <成员变量名>; <成员类型> <成员变量名>; … } (结构变量名); struct Person { char name[20]; unsigned long id; float salary; char address[200]; } p1, p2;结构的赋值1结构的赋值1通过取成员操作(.)来引用结构变量的元素 Person p1 = { "G.W Bush", 1000010, 1.5 , "ZhongGuanChun, Beijing, China"}; strcpy(p1.name, "G.W Bush"); p1.id = 1000010; p1.salary = 1.5; strcpy(p1.address, "ZhongGuanChun, Beijing, China");结构的赋值2结构的赋值2结构赋值的例子 Person p1 = { "G. W Bush", 1000010, 1.5 , "ZhongGuanChun, Beijing, China"}; Person p2 = p1;结构的存储模式1结构的存储模式1每一个成员都有自己的存储空间 对每一个成员的操作都是独立的,各元素间不会相互影响结构的存储模式2结构的存储模式2#include using namespace std; struct Person { char name[20]; unsigned long id; float salary; }; int main( ) { Person p1 = {"Zhang Weilong", 1000101, 32}; cout << "&p1=" << &p1 << endl; cout << "&p1.name=" << &p1.name << endl; cout << "&p1.id=" << &p1.id << endl; cout << "&p1.salary=" << &p1.salary << endl; }What are pointers for ?What are pointers for ?Accessing array elements. Passing arguments to a function when the function needs to modify the original argument. Passing arrays and strings to functions. Obtaining memory from the system. Creating data structures such as linked lists.Pointer, address, variablePointer, address, variableint theVariable =5; int * pPointer = & theVariable;5200020002001200220032004200520062007200820092010addresstheVariablepPointer指针1指针1编译器为变量与常量分配存储空间 任何存储空间都用地址来表示 任何变量/常量都有地址 一个变量的地址也是一个有意义的值 变量地址也可进行运算 这个值也可以用另一个变量来存储 这"另一个"变量的类型就是指针类型指针2指针2指针就是用来存储其他变量的地址的变量 指针变量自己也有地址 指针是有类型的 指针的类型要跟它所指向的变量的类型一致 整数指针,浮点数指针理解指针的例子程序addr.cc理解指针的例子程序addr.cc定义下面的变量,分别输出它们的值与他们的存储地址 int iVal1 = 1; int iVal2 = 2; double dVal1 = 1.1; double dVal2 = 2.2;指针的定义指针的定义<数据类型> *<指针变量名>; int* ip; const int * icp; char * str; NULL指针指针的操作指针的操作取地址操作符 & int * ip; int i = 89; ip = & i; 引用指针指向的变量 * 获得该指针所指向的变量 int j = 0; j =
/
本文档为【达内C++】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。 本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。 网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。

历史搜索

    清空历史搜索