Click here to load reader
Upload
graham
View
258
Download
0
Embed Size (px)
DESCRIPTION
C++ 面向对象程序设计 教师:余波 Email: [email protected] 13975892378. C++ 面向对象程序设计. 教材 : C++ 参考大全 学时 :40( 理论 )+24( 实践 ) 考试 :70%( 笔试 )+30%( 平时 ). 第一章 C++ 编程基础. 一、程序语言的发展. 1 、机器语言(用二进制代码表示) 2 、汇编语言(用符号表示) 3 、高级语言(类似自然语言). 第一章 C++ 编程基础. 二、 C++ 与 C 的联系. 1 、 C++ 是在 C 的基础上发展而来, - PowerPoint PPT Presentation
Citation preview
C++
Email: [email protected]
C++: C++:40()+24():70%()+30%()
C++123
C++C++C1C++C C2CC++
3 ,
C++
C++C++1+2 3+4
C++C++1CPP2OBJ3EXE4
C++IDE12 Borland C++,Visual C++
C++1
C++21 2: // C++ // C
C++312 3 45
C++4
C++1 2 3
C++45switch 6
C++12345
C++#include
1#2 #include 3
C++h
#include
C++1 {}
void main ( ) {cout
C++ 1 2 3 4{ }
C++2main main12 main3 main
C++4 main main
5 main void: int: 0 0
C++C++
{ }
C++112321.233 a
C++4 student 5
C++cout1 cout
2
C++4
cout
C++1 2 ;
C++3\n endl4
C++ 2.0 circum=12.56
C++ 1 int age;
C++2
int age; age=20;// int age=20;//
C++3
constconst float PI=3.14;
C++ 1 2* PI*radius // circum=2* PI*radius //
C++2 a+bc // + a+b-c //
C++3; circum=2* PI*radius;4C++12 /3
C++51+-/% % 22%7=1
C++ 345/10=34 345.0/10=34.5
C++2 = = circum=2 PIradius
C++cin cin >> cin >> >>
C++
1 int a,b,c; cin>>a; cin>>a>>b>>c;2cin>> cin>>100;() cin>>100+200;()cincout #include
C++ int)(float)(char)(double)(void)(*)(&)([ ])(struct)(union)(enum)(class)
1 1 8 0000 0000 1010 1010
2 16 32
3 -32768~32767
4 0000 0000 0000 1010 16
float radius; radius 4
2
long() short() signed() unsigned()
16
charchar1unsigned char1signed char1 char
16
intint2unsigned int2signed int2 int
16
short intshort int2shortunsigned short int2signed short int2
16
long intlong int4longunsigned long int4signed long int4
16
float4double8long double10
3
1void
2
12 0 012 OXox OX12
3
(float)(double)
3.14 31400 3.14E4 0.314 3.14E-1
4
ASCII char c=60; int i = &;
char c=1234;()
\0\t\n\v\f\?\r\\\b\\a\
\0 Hello
H e l l o\0
00
0
0
0\0
0
5
1 [] int grade[50]; grade50
[ ] n0~n-1 grade[0] grade[49]
2 enum {12n};:enumWEEKDAY{Sun,Mon,Tue,Wed,Thu,Fri,Sat};
,. : WEEKDAY w1,w2=sat;
{ }n WEEKDAYw1,w2Sun,Mon,Tue,Wed,Thu,Fri,Sat
012n
enum WEEKDAY{Sun,Mon,Tue,Wed,Thu, Fri,Sat}; : Sun,Mon,Tue,Wed,Thu,Fri,Sat0123456
enum SomeDigits{ONE=1,TWO,FIVE=5,SIX,SEVEN};:ONE=1,TWO=2,FIVE=5,SIX=6,SEVEN=7
3
struct { 1 2 n };
struct Person { char name[10]; int sex; int age; float pay; };Person p1;
p1.name= WangPin; p1.sex=1; p1.age=35; p1.pay=1000.0; Person p1={ WangPin,1,35,1000.
1 1 char a,b; 2 c=a+b; 3 4
1 2
if 1 if
2 if 1 else 21 2
4ifif
ifelse
if11 else if22 else if33 else ifnn else n+1
switch 1 switch {case 11 case 22 case nn default:n+1 }
2 switchcase default n+1n+1
31 case23case { }
4casecasebreak switchswitch5case
dayoftheweek4
ThursdayFridaySaturdayUnknown week day
dayoftheweek4Thursday
ifswitch switch break
while 1 while ,
2 3 YN
dowhile 1 do while;
23
YN
3 dowhilewhile while dowhile
for1for
2123 2
3YN
3forwhile1 for2 while 3forwhile
break(break;)break break
100 10 15 20 sum/num=15.0
continue(continue;) continue
11 0 10 -15 15 -30 20 sum/num=15.0
()goto goto 1210breakgoto
12
//{}1int max(int x,int y) {if (x>=y) return x; else return y; }
12 3void
4 return: return (); return ; return,return: return;
()123
1203
4
12 { }3
3 5 5
1 2 34
1main23
1234
1 2 34
1 2
main1 main2 mainargcargv void main (int argc,char *argv[ ]);
const constconst const 1const 2 constint strcmp(const char*str1, const char*str2);
const 1const 2const const int &min(int&,int&); min(a,b)=4;()
const1const 2 const void printmessage(char*msg) const;
c++
1
21 2
3 4for ANSIc++for
5goto goto
1 2 #include
4#include //cin,coutdouble factorial(int n) // factorial{ }void main( ) // main{} // cin,cout factorialmain
1
2
3
;
1 2 3 4
7int max(int x,int y) {if (x>=y) return x; else return y; } int max(int ,int )
1 2
1 2 3
1 inline 8inline int max(int a,int b) {return (a>b) ?a:b;}
2 9int maxinum=max(val1,val2);
int maxinum=((val1>val2)?val1:val2);
34
5
12
10 long add(long a,long b) {return a+b;} long add(double a,double b) {return a+b;} long add(float a,float b) {return a+b;}
31 , 2
1 11int f(int a,char b,char c= z,int d=100); : c, d
2 12 f(3,a , b ) f(3,a , b ,100) f(3,a ) f(3,a , z ,100)
31 2
3 4
4
13int fp(char c,int k=0,double d=100.0);ADAint fp( ); B void fp(char c);Cint fp(char,int); Dvoid fp(char,int,int)
1 n! long fact(int n) {if (n==0) return 1; return (n*fact(n-1));}
fact(4)n=44*fact(3)n=33*fact(2)n=22*fact(1)n=11*fact(0)126242
312 3
4 CPU
12template
int max(int x,int y) {if(x>y) return x; return y;} float max(float x, float y) {if(x>y) return x; return y;} long max(long x, long y) {if(x>y) return x; return y;} double max(double x, double y) {if(x>y) return x; return y;}
template Type max(Type d1,Type d2) {if (d1>d2) return d1; return d2; }Type
31 2
3
4
#include #include template Type max(Type d1,Type d2) {if (d1>d2) return d1; else return d2; } int max (int,int); char max(char,char); char*max(char*str1,char*str2);
4template T1 funcName(T1 para1,T2 para2) {if (d1>d2) return d1; else return d2; }
11 2 extern
//main.cppint x;int y;void main( ){}//file1.cppextern int x;int y;void func1( ){}
3:extern extern
21 2
//main.cppvoid print(char*)void main( ){print( Hello,friend\n);}
//file1.cpp#includevoid print(char*str){cout
31 static static int i;2 static : static int max(intx,int y){}3
4 ,
5 const,
1#include1 2#include //#include //
21h2
3
31 #if #else #elif #endif #ifdef #ifndef 2#define #define #ifdef #ifndef
112212312
41235 0
61static int addCount( ) { static int count; count++; return count; }
2 ;0
71
[]12int grade1[5]; int grade2[2*3];
1(1) (2)n 0~n-1
2[] int art[5];artart[0] art[1] art[2] art[3] art[4]
A[0]A[1]A[2]A[19]
B[0]B[1]B[2]B[19]
char str1[ ]=Wang Tao;char str2[ ]; str1str2 for(int i=0;str[i]!= \0;i++) str2[i]=str1[i]; str2[i]= \0;
WangTao \0
WangTao \0
n0~n-1
1 []={12n}int array[5]={10,20,30,40,50};array[0]=10, array[1]=20 ,array[2]=30 array[3]=40 ,array[4]=50
array
array[0] array[1] array[2] array[3] array[4]
1020304050
21 0 int array[3]={1,2}; array[0]=1, array[1]=2 ,array[2]=0
2 int array[ ]={6,5,4,3}; 4 4
3 char ca1[ ]={c , h , a , r };char ca2[ ]= char;1 \0char ca3[4]= char;
1 [1] [2]1 2int art[5][6];5630
21 m 0~m-1 n 0~n-12[][]art[2][3]art2 3
31 [][]={12m} int art[2][3]={{10,15,20},{25,30,35}};art6art[0][0]=10,art[0][1]=15,art[0][2]=20art[1][0]=25.art[1][0]=30,art[1][2]=35
art 0 1 2 0 1
101520253035
2 0int ib[3][3]={{1,2},{5}};ib[0][0]=1,ib[0][1]=2,ib[0][2]=0 ib[1][0]=5,ib[1][1]=0,ib[1][2]=0 ib[2][0]=0,ib[2][1]=0,ib[2][2]=0
int ia[ ][3]={{10,15,20},{25,30,35}}; ia2
char days[5][6]={one , two , three , four , five }>=+1
1
2
72
Struct {1 2 n };
struct student{char name[20]; char id[7]; float score;}; studentname idscore
student s1,s2;//student s3[3];//
s1 score s2 score s3[2] score
student s1={wang , 12345 ,93} student s2[3]={{ wang , 1234 ,76},{ zhang , 3333 ,83},{ li , 5678 ,90}};
student s1,s2;s1=s2;
12
struct B{char ch; int x,y; double z;};
struct E{int d;B b;};struct F{double data;F*next;};
struct Student{ struct Date{int year; int month; int day;}; char name[20]; Date birthday; float score; }; StudentDateStudentDate
81 =int i; intip=&i; float score; floatpf=&score;
ip ip5iip(ip==i)
83 int i; intip=&i; // ip 1NULL0 intip= NULL
84 const1const int i; const intip=&i;
2 int i; const intip=&i; ip=25
31 int ij; const intip=&i; ip=25() ip=&j; ()
2 int i; const intip=&i; ip=25 ( ) i=25; ()
3 const int ci=30; const int ip1=&ci;() int ip2=&ci;()
1const int i; int const ip=&i;
2 int i,j; int const ip=&i; ip=&j;() ip=32;()
1 const int i; const int const ip=&i;
2 int i,j; const int const ip=&i; ip=&j;() ip=32;()
85 int array[5]={10,20,30,40,50}; array=&array[0];
int array[100]; intpArray=array; pArrayi+1 array[i] pArray[i]
int array[100];array=&array[1];()1+1-1
int array[5]={10,20,30,40,50};int pa=array;
01234array pa pa+1 pa+2 pa+3 pa+4
1020304050
2 181182 182
1 183184
2i+1 int array[5]={10,20,30,40,50}; int pa=array; pa[i] array[i] pa+i) array+i)
3i+1 int array[5]={10,20,30,40,50}; int pa=array; pa[i] array[i] pa+i array+i
4 int array[10]; int pa=array pa+20
1\0 2 charstr= a string;
str
astring\0
86 newdelete11 new 2 NULL
3 new new new new []intip1=new int; intip2=new int(3); intstr=new int[10];
21delete 2 delete delete delete [ ]
intpInt=new int; floatpFloat=new float(34.5); intarr=new int[100]; delete pInt; delete pFloat; delete[ ]arr;
3newdeletenew new
new []
delete delete [ ]
deletenew int i; intip=&i; delete ip;() floatfp=new float(3.4); delete fp;() delete fp; ()
new NULL
3charweek[7]={Sunday , Monday , Tuesday , Wednesday , Thursday , Friday , Saturday };
1 .
:struct Student{ char name[20]; float score;};Student st={wanglin ,98};Studentps=&st; ps name==(ps).name==st.name== wanglin ps score==(ps) .score==st.score==98;
21
2
struct StudentNode{char name[20];float score;StudentNodenext;};
namescorenext
3(head) NULL
4
StudentNodeStudentNodepNew;pNew=new StudentNode;strcpy(pNewname, wangtao );pNewscore=67;pNewnext=NULL;
wangtao 67
pNewStudentNode*tail;tail=head;while(tail != NULL && tailnext != NULL)tail=tailnext;tailnext=pNew;
wusan 89 Lishi 88 wangtao 67head wusan 89 Lishi 88 wangtao 67head tailpNewtailpNew
5
12nheadpDel12npDelheadhead=headnext;delete pDel;
1i-1ini+1headpDel1i-1ii+1nheadpDelprenext= pDelnext;delete pDel;prepre
headif(head==pDel) head=headnext;else {pre=head; while(pre!=NULL && prenext!=pDel) pre=prenext; prenext=pDelnext;}delete pDel;
1234
87 1 C++
2 &int val; int &rval=val;// rvalval
3
4
&val=0X0065FDF4&rval=0X0065FDF4val=34 rval=34val=12 rval=12
11int & char & float & double & bool &
3int pInt=new int; int&rpInt= pInt; rpInt=45.6; delete rpInt;
21void void
2int arr[10]; int& rarr1=arr;() int& rarr2=arr[0];()
3int i; int& ri=i; int& pr=&ri;() int& int& int&intint& int
const1const2const int i; const int& ri=i; ri=45;() i=45; ()
3 constconst int ci=45; int& ri=ci;() const int& rci=ci;()
88 int i=10; intpi=&i;// pii int&ri=i; // rii
pi ri0x0065FDE40x0065FDE8iripi
100x0065FDE4
1 *pi=40; ri=40;
2int j; pi=&j;ri=j; /rijjrii /
89 swap
val1=10 val2= 20val1=20 val2= 1012
91
1
2 3
4
5
1
2Smalltalk Smalltalk1Smalltalk
23 Smalltalk
31 SmalltalkEiffelActorJAVA
2 Object CC++Object Pascal CLOS
92 1
2
3
//class Phone{private:char owner[10];char address[50];char phoneNumber[10];
public:Phone(charowner,charaddress,charphoneNumber);chargetPhoneNumber( );chargetOwner( );chargetAddress( );chardial(char*);};//Phone myphone( 62782622
1
2
3 myphone charmyphone.dial(82904456 );
1
2 instance) (template)
3
1
2
3
baseClassderivedBderivedAderivedCbaseClassBbaseClassA
4(base class)(derived class)
5
class A {}; class B:public A {}; class C:public B {};AB,CBA,CA
1
2
3
1
2 1 2C++
93 11
2 3
4 5
2
11
2
21 2
94 1
2
3
OOA1
21 2 3
4 5 6
1
2 3 4
95 12345
123
1 2
3
4 5
OOPL OOPL
96 OOPL
1OOPL 2OOPL 3OOPL
4OOPL 5 OOPL
6 OOPL
10 10.1 class {}class Rectangle{public: float width,height; float Area( ){return width*height;} float Perimeter( ){return 2*(width+height);}}
10 Rectangle rect; rectRectangle rect widthheight AreaPerimeter
10 10.2 pubilc: private: protected:
10 class Rectangle{private: float width,height; public: void SetWidth(float newWidth){width= newWidth;} void SetHeight (float newHeigh){heigh= newHeigh;} float Area( ){return width*height;} float Perimeter( ){return 2*(width+height);}}Rectangle rect;rect. SetWidth(45);rect. SetHeight (54.2);
10 123
10 10.3 1class Rectangle{private: float width,height; public: float Area( ){return width*height;} float Perimeter( ){return 2*(width+height);}}
10 2class Rectangle{private: float width,height; public: float Area( ) float Perimeter( )}float Rectangle Area( ){return width*height;}float Rectangle Perimeter( ){return 2*(width+height);}
10 1
10 21234 P234
10 3
10 1 P2352 1inline2inlineP2363 P237
10 const1const2const3constconst const const
10 10.4 P2403123
10 10.5 P245P246
10 10.6 thisthis thisP237P247-248
10 10.7 P250publicprivateP251
11 11.1
11 12 ~34 P268-270
11 11.2 class Name{public:Name( );Name(char*first);Name(char*first,char*last);};
Name name1;Name name2(Yun );Name name3(Yun , Zhao );
11 Name (char*first= default , char*last= default );1 2new
11 31
11 2Name (Name&nm){strcpy(firstname,nm.firstname); strcpy(lastname,nm.lastname);}Name name2(name1);name2name1
11 123: (){, ()}
11 4 class A{private:int mem;int& rmem=mem;()public:A( );};A( ):rmem(mem){mem=0;}
11 5 class Student{private:Name studentName;int studentID;public:Student(char*name,int id);};Student Student(char*name,int id):studentName(name), studentID(id){ }
11 class A{int mem;public:A(int a){mem=a;}};class B{A a;public:B( ):a(3) { };B(int a1):a(a1){ };};
11 11.3 P289-291123~45
12 12.1 staticclass File{ public: static int fileCount; };
12 1 privateprotectedpublic2 3
12 4 5 int File fileCount=0;
12 12.2 1ffriend f2ABABfriend class A;
12 P307-3091 2 3
12 1 friend 23 P311
13 13.1 11 operator2 operator12A operator++(A&x) {.} A operator+(A&x,A&y) {.}
13 21 operator operator++(x) ++x2 operator12 1 2 operator+(xy) x+y
13 13.2 thisP317P317-318
13 13.3 Matrix m; m.GetAt(i,j)=3 m(i,j)=3;12 3
13 P32612P32712P328
13 13.4 I/O12istream ostream
13 P340P34313.5 operator type( ) typeoperator int( ){return val;}
13 12si2=si1+(SmallInt)56; si2=si1+SmallInt(56);
14 BAAbase class)B(derived class)
14 14.1 class Derived:Base{ Derived}1 2
14 class Base{private: int i,j;public: void SetIJ(int,int); void Getij(int&,int&);};Derivedclass Derived:Base{private:int k;public:void SetK(int);int GetK( );};
14 1 2 P355
14 11class Derived : public Base{ Derived}2
14 21class Derived :protected Base{ Derived}2
14 31class Derived :private Base//class Derived : Base{ Derived}2
14 1 Derived Derived(int m1 ,int m2,int m3):Base(m1,m2){mem3=m3;}Derived Derived(int m1 ,int m2,int m3):Base(m1,m2)mem3m3{ }
14 2
14 14.2 class [ ]{ }class Derived:public Base1,private Base2 { }
14 1P3742 3
14 1 P376-3772 P377-3783 P378
14 1 2virtual
14 3 P380
14 14.3 MFCVisual C++)