C++课件达内科技内部资料_第1页
C++课件达内科技内部资料_第2页
C++课件达内科技内部资料_第3页
C++课件达内科技内部资料_第4页
C++课件达内科技内部资料_第5页
已阅读5页,还剩500页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、The C+ Programming LanguageChapter 1,C+ Programming in UNIX,课程介绍 C+ 语法基础 面向对象程序设计的概念 大量的编程实践 目标 熟练掌握C+语法 具有面向对象程序设计的概念与能力 能熟练阅读复杂的C+程序源代码 能独立的设计与完成面向对象的C+程序,课程内容简介 1,C+语言基础 保留字 变量,常量 表达式 语句 函数 程序的结构 数据结构与算法 数组、指针、引用、结构、链表与栈,课程内容简介 2,C+面向对象编程 类 构造函数与析构函数 静态成员与友员 函数重载 继承与多态 I/O流 模板 异常,程序设计语言介绍1,What c

2、omputer understand? bits Assembler Language Limited structure Global scope Machine code Primitive High-Level Language Function decomposition Data separation High level structure,程序设计语言介绍2,Block Structured Language Encapsulation Flexible data scoping Modularization Object-Oriented Language Inheritanc

3、e Polymorphism Abstract data types,C+程序设计语言,1972,AT /main function int main() cout Hello world! endl; cout This is my first C+ program.n;,g的常用参数,c 编译成目标文件.o - o指定输出文件名,输出文件名跟在-o后面,用空格分隔。如果不使用这个选项,缺省的输出文件名为a.out。 - g产生有调试信息的可执行文件 - w不产生警告信息 - l 连接指定的库文件 - L指定库文件的路径 - i 要包含的头文件 - I 头文件的路径 - E 显示预处理后的程

4、序文件到屏幕上,可以用-o指定输出到文件 - S 产生汇编程序 如果没有c、E、S就会生成可执行文件,编译hello.cc, g+ -c hello.cc % ls,连接hello.o, g+ -o hello hello.o % ls % g+ hello.o % ls,运行hello 程序, hello % a.out,C+程序的基本结构1,*the first C+ program*/ #include using namespace std; /main function int main() cout Hello world! endl; cout This is my first

5、C+ program.n;,C+程序的基本结构2,include 与 #include Name space: 提供了一个全局标识符和全局变量所在的作用域。 int main() 注释 函数 函数的调用 cout语句,头文件,include语句 #include 与 #include,使用 #include using namespace std; 少用 #include,Main函数,main函数的作用 Standard C+ main( )格式: int main( ) return 0; /the default return value is 0;,注释,C+的注释,基本的输出语句,c

6、out,练习程序hi.cc,include using namespace std; int main( ) cout Hi Jian! endl; cout Have a nice day. endl; return 0;,练习程序myself.cc,编写一个程序,打印出自己的: 姓名 性别 年龄 家庭住址 电话号码 爱好 每一条信息输出为一行,在hi.cc中使用字符串,include using namespace std; int main() char name = John; cout Hi name ! endl; cout Have a nice day. endl; retur

7、n 0;,字符与字符串类型,字符,字符串/字符数组 char ch = A; char str120 = Hello world!; char str2 = Have a nice day,不同的main()格式,命令行参数 % ls l (or ls -al ) % vi hello.cc 在程序中使用命令行参数 int main(int argc, char *argv,命令行参数程序cmdline.cc,include using namespace std; int main (int argc, char* argv ) for(int i=0; iargc; i+) cout ar

8、gv i = argvi endl;,使用命令行参数的hi.cc,使用命令行参数,重新编写练习程序 cmdline.cc % hi John % hi Lisa % hi G. Bush,基本输入语句,cin语句 使用cin语句的hi.cc 重写hi.cc程序,不带命令行参数 程序自动提示用户输入字符串来获得姓名与年龄,练习程序age.cc,include using namespace std; int main() unsigned int age; char name 50; cout name; cout age; cout your name is: name endl; cout

9、You were age -2 years old two years ago. n;,条件语句,if语句 ifelse语句 不同if的等价与不等价形式 char ch; cin ch; if (ch = y) /note the difference: if ( ch = y) cout good endl; else cout try again. endl;,练习程序grade.cc,This is your assignment,Q int main( ) int i; i = 5; cout i = i endl; i = 8; cout i = i endl; return 0;,

10、另一个使用变量的例子程序,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,变量都有类型 变量在内存中的大小 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 s

11、ize of double is sizeof( double ) endl,程序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)

12、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.14,const限定符,限定一个常量或者函数 方便编译器来检测非法的修改操作,运算符,运算符 +,-,*,/,%, +,-, =, , =, 结

13、合性 优先级: 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; snu

14、m = 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,练习,为表示如下数据,应该使用什么类型的变量: 年龄 姓名 工资 电话号码 身份证号码 西三环到东三环的距离,练习程序

15、bin.cc,include using namespace std; int main( ) int a = 10; cout a; unsigned int r; int k; unsigned int j; char str33; memset(str, 0, 33); str32=0; r = a; int i = 32,练习程序bin.cc,do 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 colorRED, GREEN

16、, 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,

17、 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,include using namespace std; int main( ) int a, b,

18、 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语句 dowhile语句 while语句 break语句 continue语句,分支语句,switch语句 switch(ss) case 1

19、: break; case 2: break; default: break;,循环语句程序例子 chengFa.cc,编写一个程序,打印出乘法口诀表 分别使用for语句,dowhile语句,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

20、=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,This is your assignment. 判断输入的年份是否是闰年,Q void disp(char str ) cout This is your string: str endl; int main( ) char course1 = C+;

21、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,

22、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

23、 SexMALE, FEMALE; void disp(char *name, Sex gender=MALE); 如果在函数声明中指定了默认值,则在函数定义时不能再指定,内联函数,提高程序运行效率 内联函数的定义 inline int isnumber(char ch) return (ch=0 必须先定义,不支持函数原形(声明) 不支持结构控制语句,递归函数,一个函数调用它自己 如何正确的递归 必须有结束的条件 并且该条件一定能够满足,使用与不使用递归的例子程序,编写一个程序bigsum.cc ,使用一个函数来求n的值: #include using namespace std; int

24、bigsum(int a) if(a = 0) return 0; return a + bigsum(a - 1); int main() int n; cout n; int m = bigsum(n); cout The sum is: m; cout endl;,练习程序nbang.cc,使用递归函数,编写一个程序来求n!的值: This is your assignment,函数的重载,C+中的函数可以重载 什么是函数的重载: 对于在不同类型上作不同运算而又用同样的名字的情况,称为重载。 函数重载的注意事项: 重载函数至少在参数个数,参数类型, 或参数顺序上有所不同,函数重载的例子,

25、求两个数的平均值 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 code,include using namespace std; int Area(int width, int length = 1) return width * l

26、ength; 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形参的意义 #include using namespace std; void disp(const int I) cout I endl;

27、 I = 100; / think about this. int main( ) disp(50);,程序的结构,多文件结构 外部变量与内部变量 变量的作用域与可见性 头文件 静态全局变量 静态函数,多文件结构,按不同的功能模块将程序的源代码划分在多个文件中 不同源文件之间可以共享变量声明与类型定义 C+多文件的划分原则,外部变量与内部变量,存储类:auto, extern, register, static, volatile. 什么是外部变量 什么是内部变量,变量的作用域与可见性,局部变量的作用域 静态局部变量的作用域 全局变量的作用域 外部变量的作用域 常量的作用域,头文件,头文件的作

28、用 如何组织头文件 头文件的使用 编译选项,静态全局变量,何为静态全局变量 只在本源文件中可用,静态函数,定义静态函数 只在本源文件中可用,改写bigsum.cc与nbang.cc程序,将bigsum.cc与nbang.cc改写成多文件结构,每个文件只能有一个函数,改写bigsum.cc与nbang.cc程序,分别将前面的函数改为静态函数,再次编译并运行所有的程序 This is your assignment,练习程序hash.cc,include using namespace std; int main( ) char line100; cout line; int ch = 0; fo

29、r(int i=0; istrlen(line); i+) ch += linei; cout The hash of your string is : ch endl;,练习程序mywc.cc,输入一行文字,统计单词的个数。 为了输入的时候能包含空格,程序使用了getline函数。 #include using namespace std; int main( ) cout Please enter a line: endl; char line120; int cnt = 0; cin.getline(line, 120); int i = 0,练习程序mywc.cc,while (ist

30、rlen(line) if (linei != ) cnt +; while (linei != ),Q 下标是数组元素到开始的偏移量 数组下标从0开始 char buf4,数组2,数组在声明时,元素个数必须是常量或常量表达式 char buf10; int I; char bufI; /? int I = 10; char bufI; /? const int i = 10; char bufi; char bufi+1,数组3,如果数组的声明带有初始化,可以直接对整个数组赋值 访问数组元素,使用下标操作符 int iA10; iA0 = 0; iA1 = 1; int I = 0; I =

31、 iA0 + iA1,数组的初始化,在声明的时候就初始化 int iA5 = 0,1,2,3,4; int iB = 1,2,3; 使用赋值语句初始化数组 iA0 = 0; iA1 = 1; 数组的边界问题 int iC5; iC10 = 100; /run time ? Question: does C+ compiler have array bound checking,数组程序例子,编写一个程序,从键盘接受一个字符串,将该字符串颠倒顺序,然后打印出来,练习程序findmax.cc,include using namespace std; int main( ) int iA = 103

32、, 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(iAi iAj,if(iAi iAj) temp = iAi; iAi = iAj; iAj = temp; cout The bigNumber is: iA9 endl;,多维数组,二维数组与多维数组: int iA510; int iB23 = 1,2,3, 4,5,6; int iC246; 多维数组的初始化 仅有第一个维数可以省去 int iB 3 = 1,2,3, 4,5,6, 7

33、,8,9,数组练习程序mdim.cc,include using namespace std; int maximum(int 4, int, int); int main ( ) int sg34 = 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 = grade00; for (int i=0; i max) max = gradeij; return max;,结

34、构1,将不同类型的相关数据信息组织在一起 是用户自定义的类型 需要先声明类型的定义才能使用 结构与数组的区别 数组只是同一个数据类型的聚集 数组本身不是一个新的数据类型,结构2,struct ; ; ; (结构变量名); struct Person char name20; unsigned long id; float salary; char address200; p1, p2,结构的赋值1,通过取成员操作(.)来引用结构变量的元素 Person p1 = G.W Bush, 1000010, 1.5 , ZhongGuanChun, Beijing, China; strcpy(p1.

35、name, G.W Bush); p1.id = 1000010; p1.salary = 1.5; strcpy(p1.address, ZhongGuanChun, Beijing, China,结构的赋值2,结构赋值的例子 Person p1 = G. W Bush, 1000010, 1.5 , ZhongGuanChun, Beijing, China; Person p2 = p1,结构的存储模式1,每一个成员都有自己的存储空间 对每一个成员的操作都是独立的,各元素间不会相互影响,结构的存储模式2,include using namespace std; struct Person

36、 char name20; unsigned long id; float salary; ; int main( ) Person p1 = Zhang Weilong, 1000101, 32; cout ,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 memor

37、y from the system. Creating data structures such as linked lists,Pointer, address, variable,int theVariable =5; int * pPointer =,5,2000,2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010,address,theVariable,pPointer,指针1,编译器为变量与常量分配存储空间 任何存储空间都用地址来表示 任何变量/常量都有地址 一个变量的地址也是一个有意义的值 变量地址也可进行运算 这个值也可以

38、用另一个变量来存储 这另一个变量的类型就是指针类型,指针2,指针就是用来存储其他变量的地址的变量 指针变量自己也有地址 指针是有类型的 指针的类型要跟它所指向的变量的类型一致 整数指针,浮点数指针,理解指针的例子程序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 iVal; i

39、Val = 10; 整数指针性变量 int *iPtr; iPtr = Any data type in c+ can be a pointer type,指针的使用,指针在使用前必须初始化 指针的类型决定了指针所参与的运算 指针只能进行加减法运算,理解指针的例子程序 ptracc.cc,include using namespace std; int main( ) int var1 = 11; int var2 = 22; int * ptr; ptr =,理解指针的例子程序 ptracc.cc,Var1 (11,Var2 (22,Var1 (11,Var2 (22,ptr is 11,p

40、tr is 22,ptr,ptr,指针与数组,数组的本质就是数组里所有变量的首地址 数组的下标操作就是针对地址,指针与数组的例子程序 ptr.cc,分别用数组的方式与指针的方式来操作 int iv5 分别给他们赋初值并输出他们的值与地址: #include using namespace std; int j; int iv5 = 1,2,3,4,5; int main() int a = 800; int i; int b = 900; int *ptr =,指针与数组的例子程序 ptr.cc,cout ,结构指针,结构是一种数据类型 结构类型也有指针 结构变量vs结构指针变量 结构指针变量

41、的操作 结构变量用( . )操作来存取成员 结构的指针用( - )操作来存取成员,结构数组,数组的元素是某一个结构类型 struct Person char name20; unsigned long id; float salary; char address200; ; Person allPeople100,结构中的数组元素,结构中的某个元素是数组类型 struct Person char name20; unsigned long id; float salary; char address200; ; Person Jack = Jack, 2L, 8000.00, Toronto,

42、指针作形参,利用指针在函数中传递参数,程序swap1.cc,include using namespace std; void swap(int, int); int main( ) int a = 3, b = 8; cout a= a , b = b endl; swap(a,b); cout after swapping n; cout a = a , b = b endl; void swap(int x, int y) int temp = x; x = y; y = temp;,程序swap2.cc,include using namespace std; void swap(in

43、t *, int * ); int main( ) int a = 3, b = 8; cout a= a , b = b endl; swap(,练习程序date.cc,This is your assignment 要求用结构、指针,Q ip = new int; float *fp; fp = new float; int *ipA = new int5; int * pA5; for(int j=0; j5; j+) pAj = new int10,Delete操作,delete ip; delete fp; delete ipA; for(int j=0; j5; j+) delet

44、e pAj,含指针的结构变量的赋值,如果结构中含有指针成员,使用了动态空间,通过指针传递参数,函数的形参类型可以是指针 通过指针来传递参数,练习程序ptrsort.cc,include using namespace std; int main( ) void bsort(int*, int); const int N =10; int dataN= 37,84,62,91,11,65,57,28,19,49; int *arr = new intN; for( int i=0; iN; i+ ) arri = datai; bsort(arr, N); for(int j=0; jN; j+

45、) cout arrj ; cout endl; delete arr;,练习程序ptrsort.cc,void bsort(int* ptr, int n) void order(int *, int *); int j,k; for(int j=0; j * numb2) int temp = * numb1; *numb1 = * numb2; *numb2 = temp;,危险的指针用法,使用NULL指针的内容 使用没有初始化的指针变量 使用已经被delete 的指针内容 函数返回局部变量的地址 由函数申请的堆空间,而由调用者来释放,函数指针,调用一个函数 函数都有地址,存放在内存的代

46、码区(code). 函数指针 函数指针指向代码区中的某个函数。 函数指针的声明 int (*func) (char a, char b); int * func (char a, char b); /how about this one,函数指针,func is a pointer that points to a function. Example: int fn1 (char a, char b); int* fn2(char x, char y); int fn3(int a); int (* fp1) (char a, char b); int ( * fp2) (int s); fp

47、1 = fn1; /ok fp1 = fn2; /error fp2 = fn3; / ok fp2 = fp1; /error fp1 = fn2(a, b); /error. fp1(a, b); /ok,函数指针 funcptr.cc,include using namespace std; int swap(int *a, int *b) int t; t = *a; *a = *b; *b = t; int main() int i = 100; int j = 200; int m = 300; int n = 400; cout i= i endl,函数指针 funcptr.cc

48、,cout j= j endl; swap( /end of funcptr.cc,指针的指针,可以存在多层的指针 int iNum; int *ptr; int *pptr,void指针,void * ptr,const与指针,普通指针:指向的对象的值可以改变,指针的值也可改变 int *p; 常量指针:指针可以指向不同的对象,但其指向的对象的值不能变 const int *p; 指针常量:指针本身是常量,只能指向固定的对象,但其指向的对象的值可改变 int * const p; 必须初始化 常量指针指向常量 const int * const p; 必须初始化,引用,引用是一个变量的别名

49、引用的声明 int iVal; int 引用的使用 声明的时候就必须初始化 引用不能重新赋值(不能重新绑定为另一个新对象的别名,引用的例子程序 myRef.cc,include using namespace std; int main() int iVal = 100; cout iVal= iVal endl; cout ,引用的重新赋值 myRef2.cc,为引用重新赋值 int iA; int,通过引用来传递参数,函数的形参可以是引用 通过引用来传递参数,引用作为函数的参数 ref1.cc,include using namespace std; void disp(int,使用引用的

50、swap.cc程序,include using namespace std; void swap(int,函数返回一个类型的引用,函数可以返回任何类型的引用 如果返回的引用对应于一个局部变量,函数返回引用 ref3.cc,指出下列程序的输出结果分别是什么,include using namespace std; int,堆中指针变量的引用 testDelete.cc,看看下列程序有无问题 #include using namespace std; int,如何释放堆中的空间,int* p =,引用的使用,尽量使用引用来传递参数 尽量使用const来保护引用 尽量使用引用来代替指针 危险的引用方

51、法 返回一个局部变量的引用,Q ,结构的定义,struct Person char name30; unsigned int age; char address100; float salary;,结构的操作1,void setName(Person *pP, char* str) strcpy(pP-name, str); void setAge(Person *pP, unsigned int yr) pP-age = yr; void setSalary(Person *pP, float s) pP-salary = s; void setAddress(Person *pP, cha

52、r* addr) strcpy(pP-address, addr);,结构的操作2,void disp(Person *pP ) cout name age address salary endl;,使用结构变量的person.cc,include int main( ) Person Jack = Jack, 30, Beijing China, 8000.0; disp(,封装好的结构1,struct Person char name30; unsigned int age; char address100; float salary; void setName(char*); void

53、setAge(unsigned int); void setAddress(char*); void setSalary(float); void disp( );,封装好的结构2,void Person:setName(char* str) strcpy(name, str); void Person:setAge(unsigned int yr) age = yr; void Person:setSalary(float s) salary = s; void Person:setAddress(char* addr) strcpy(address, addr);,封装好的结构3,void

54、 Person:disp( ) cout Name: name endl; cout Age: age endl; cout Address: address endl; cout Salary: salary endl;,使用封装后的结构变量的person.cc,include int main( ) Person Jack = Jack, 30, Beijing China, 8000.0; Jack.disp( ); Person somebody; somebody.setName( Lisa ); somebody.setAge( 20 ); somebody.setAddress(

55、 Toronto, Canada ); somebody.setSalary(6000.0); somebody.disp( );,仍然存在的问题,include int main( ) Person Jack = Jack, 30, Beijing China, 8000.0; Jack.disp( ); Jack.salary = 1000.00; Jack.disp( );,如何实现数据隐藏,引入class类型 对数据成员进行保护 增加存取范围 私有成员private 保护成员protected 公共成员public,定义类来实现数据隐藏,class Person private: ch

56、ar name30; unsigned int age; char address100; float salary; public: void setName(char* str); void setAge(unsigned int yr); void setAddress(char *str); void setSalary(float yuan); void disp( );,类成员的作用域属性,在C+中,class与struct的区别 struct的缺省作用域为public class的缺省作用域为private private的数据成员 只能被类的成员函数所使用,使用对象的perso

57、n.cc,include int main( ) Person somebody; somebody.setName( Lisa ); somebody.setAge( 20 ); somebody.setAddress( Toronto, Canada ); somebody.setSalary(6000.0); somebody.disp( );,类的声明与类的定义,类的声明 一般在头文件中 声明类的所有成员 声明类的成员的属性 声明所有接口(公共函数成员)的签名(格式) 类的定义 一般放在程序文件( .cc文件)中 定义类的所有成员函数的具体实现方法,对象,对象是类的一个实例 Perso

58、n是一个class,它是一个数据类型 somebody是一个对象,它是一个具体的变量 类是一类对象的描述 定义结构,可以声明许多变量 定义类,可以声明许多对象,增加对象初始化自己的能力,新person.cc程序的问题 Person Jack = Jack, 30, Beijing China, 8000.0; Jack.disp( ); 构造函数 一种特殊的函数 没有返回类型 只能作为是公共成员 只能用一个固定的名字,函数名就是类的名字,Person类的构造函数,class Person public: Person(char*, unsigned int, char*, float);,构造函数的特点,构造函数只在建立对象的时候自动被调用一次 构造函数必须是公共的,否则无法生成对象 构造函数只负责为自己的类构造对象,在构造函数中初始化变量,Person:Person( ) : name(Ja

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论