资源描述
,单击此处编辑母版标题样式,单击此处编辑母版文本样式,第二级,第三级,第四级,第五级,C+程序设计 讲师:鲍钰,11/28/2026,1,C+程序设计(Adv1),11/28/2026,C+程序设计 讲师:鲍钰,2,C+,基础知识,Review,.h file,class declaration and function headers,.cpp or.c file,class or function implementation,11/28/2026,C+程序设计 讲师:鲍钰,3,C+,基础知识,Review,#include,Used to include header files,#include for library files,#include,“,file.h,”,for local files and library files,11/28/2026,C+程序设计 讲师:鲍钰,4,C+,基础知识,Review,#define,Also to declare constants,Common convention to use all capitals,#define ARRAYLEN 20,#define TRUE 1,No semicolon after preprocessor directives,11/28/2026,C+程序设计 讲师:鲍钰,5,C+,基础知识,Review,Pointers,A pointer is a variable that holds an address,Special operators for pointers,&-takes an object and returns the object,s address,*-takes a pointer object and returns the object that it points to,11/28/2026,C+程序设计 讲师:鲍钰,6,指针声明,C+uses an asterisk*to denote a pointer.If Item is a type,then a pointer to such an Item object has the type Item*.For example,Item*item_ptr,;,declares item_ptr as a pointer variable to an Item object.,11/28/2026,C+程序设计 讲师:鲍钰,7,指针,EXAMPLE,int*p1;,int a=5;,p1=,*p1=6;/a=6;,11/28/2026,C+程序设计 讲师:鲍钰,8,Creating dynamic objects,Item_ptr=,new,Item,;,creates a new dynamic object of type Item and assigns its location to the pointer variable item_ptr.,11/28/2026,C+程序设计 讲师:鲍钰,9,指针,EXAMPLE,char*p1;,p1=new char;,*p1=,a,;,delete p1;,11/28/2026,C+程序设计 讲师:鲍钰,10,Deleting dynamic objects,delete,item_ptr,;,disposes of the dynamic object to which item_ptr points and returns the space it occupies to the free store so it can be used again.,11/28/2026,C+程序设计 讲师:鲍钰,11,Following pointers,*item_ptr,denotes the object to which item_ptr points.,11/28/2026,C+程序设计 讲师:鲍钰,12,NULL pointers,If a pointer variable item_ptr has no dynamic object to which it currently refers,then it should be given the special value.,Item_ptr=NULL,;,Uninitialized or random pointer objects should always be reset to NULL.,After deletion,a pointer object should be reset to NULL.,#include,11/28/2026,C+程序设计 讲师:鲍钰,13,Dynamically Allocated Arrays,Item_array=,new,Itemarray_size,;,creates a dynamic array of Item objects,indexed from 0 up to array_size 1.,11/28/2026,C+程序设计 讲师:鲍钰,14,Dynamically Allocated Arrays,for example:,int,size,*dynamic_array,i,;,cout size,;,dynamic_array=,new int,size,;,for,(i=0,;,i as a shorthand,so we can replace the expression(*p),.,the_data by the equivalent,but more convenient,expression p-the_data.,11/28/2026,C+程序设计 讲师:鲍钰,22,指针,EXAMPLE,struct Rec,int id;,string name;,;,Rec stu1;,Rec*p1,*p2;,p1=,p1-id=1;/(*p1).id=1;,p2=new Rec;,p2-id=2;,11/28/2026,C+程序设计 讲师:鲍钰,23,指针,EXAMPLE,char ch1=,b,;,char ch2=,e,;,char*p1=,char*p2=,*p1=,m,;,*p2=,*p1=,n,/what is stored in ch1,ch2?,11/28/2026,C+程序设计 讲师:鲍钰,24,C+,基础知识,Review,11/28/2026,C+程序设计 讲师:鲍钰,25,Pointer Example Result,11/28/2026,C+程序设计 讲师:鲍钰,26,C+,基础知识,Review,Declarations,static int n;/,静态分配,堆内存,int n;/,临时分配,栈内存,int*nPtr=new int;/,动态分配,void instructions();,bool user_says_yes();,11/28/2026,C+程序设计 讲师:鲍钰,27,C+,基础知识,Review,Arrays and Strings,int x5;,int*x=new int5;,char s6;,char s=,“,CS225,”,;/,with a null character at the end,0,11/28/2026,C+程序设计 讲师:鲍钰,28,Parameter Passing,11/28/2026,C+程序设计 讲师:鲍钰,29,Pointer Example Result,11/28/2026,C+程序设计 讲师:鲍钰,30,Parameter Passing,11/28/2026,C+程序设计 讲师:鲍钰,31,Reference Example Result,11/28/2026,C+程序设计 讲师:鲍钰,32,The Basics of Linked Structures,A linked structure is made up of nodes,each containing both the information that is to be stored as an entry of the structure and a pointer telling where to find the next node in the structure.,11/28/2026,C+程序设计 讲师:鲍钰,33,单链表的存储映像,11/28/2026,C+程序设计 讲师:鲍钰,34,The Basics of Linked Structures,#include,typedef char Node_entry;,struct,Node,/data members,Node_entry entry;,Node*next;,/constructors,Node();,Node(Node_entry item,Node*add_on=NULL);,;,11/28/2026,C+程序设计 讲师:鲍钰,35,The Basics of Linked Structures,Node:Node(),next=NULL;,Node:Node(Node_entry item,Node*add_on),entry=item;,next=add_on;,11/28/2026,C+程序设计 讲师:鲍钰,36,The Basics of Linked Structures,void main(),Node first_node(a);/Node first_node stores data a.,Node*p0=/p0 points to first Node.,Node*p1=new Node(b);/A second node storing b is created.,p0-next=p1;/The secondNode is linked after first node.,Node*p2=new Node(c,p0);/A thirdNode storing c is created.,/The thirdNode links back to the first node,*p0.,p1-next=p2;/The thirdNode is linked after the secondNode,coutentryentrynext-entrya1=a1;,this-a2=a2;,/void Myclass:operator=(const Myclass&original),/a1=original.a1+100;,/a2=original.a2+100;,/,void Myclass:print(),couta1 a2;,11/28/2026,C+程序设计 讲师:鲍钰,51,C+,基础知识,void main(),Myclass m1;,Myclass m2;,coutendlHere is m1:endl;,m1.set(3,4);,m1.print();,m2=m1;,coutendlHere is m2:endl;,m2.print();,coutendl;,11/28/2026,C+程序设计 讲师:鲍钰,52,Result,Here is m1:,3 4,Here is m2:,3 4,11/28/2026,C+程序设计 讲师:鲍钰,53,C+,基础知识,目录,Equals,下例程,11/28/2026,C+程序设计 讲师:鲍钰,54,C+,基础知识,使用了,new,或指针的相同类或结构体的对象间是不可以用,=,进行赋值操作的,必须对,=,进行了操作符重载后,才能赋值。否则两个类的指针指向相同的内存地址。,11/28/2026,C+程序设计 讲师:鲍钰,55,C+,基础知识,目录,Equals2,下例程,11/28/2026,C+程序设计 讲师:鲍钰,56,C+,基础知识,引用,C+,中使用引用建立变量或对象的别名。,引用分为:引用参数、返回引用以及独立引用。,11/28/2026,C+程序设计 讲师:鲍钰,57,C+,基础知识,引用,目录,AboutClass4,下例程,#include,void swap(int&x,int&y),int tmp;,tmp=x;,x=y;,y=tmp;,void main(),int a=1,b=2;,coutendlold a:a old b:b;,swap(a,b);,coutendlnew a:a new b:b;,coutendl;,引用参数,11/28/2026,C+程序设计 讲师:鲍钰,58,C+,基础知识,引用,目录,AboutClass4,下例程,#include,char s=Hello world;,char&replace(int i),return si;,void main(),replace(5)=X;,coutsendl;,返回引用,11/28/2026,C+程序设计 讲师:鲍钰,59,C+,基础知识,引用,目录,AboutClass4,下例程,#include,void main(),int a=1,b=2;,int,ref=10;,coutnew a:aendl;,独立引用,11/28/2026,C+程序设计 讲师:鲍钰,60,C+,基础知识,引用,说明引用时,独立引用必须初始化。,一旦初始化,引用就不能重新赋值。,11/28/2026,C+程序设计 讲师:鲍钰,61,C+,基础知识,引用,不能引用数组,char s=Hello world;,/void setArray(char&s2),/s20=h;,/,void setArray(char s2),s20=h;,void main(),setArray(s);,coutsendl;,11/28/2026,C+程序设计 讲师:鲍钰,62,C+,基础知识,引用,不能建立引用指针,但可以建立指针的引用。,void main(),int a=1;,int,int,/int,int *p=,int*,cout,
展开阅读全文