C++詳細復習
35032 ワード
1. :
bool 1
char 1
short 2
int 4
unsigned int 4
long 4
float 4
long long 8
double 8
2.const
3.“,” , , , 2.
a=3*5,a*4 60
4. ! && ||
5.
&
|
^
~
<< >>
int a=3;
<span style="white-space:pre"> </span>int b=5;
<span style="white-space:pre"> </span>int c=a|b;
<span style="white-space:pre"> </span>cout<<c<<endl;// 7
<span style="white-space:pre"> </span>a&b 1
6.IO
dec
hex
oct
ws
setw()
7.
( (year%4==0&&year%100!=0)||(year%400==0) )
8. typedef
9.enum
10.
, 。
11.
12. : 。
13.
: 。
C++ 。 。
14.
:
: , 。
15 break
for (int i=0;i<5;i++)
<span style="white-space:pre"> </span>for (int j=0;j<5;j++)
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>if(3==j)
<span style="white-space:pre"> </span>break;
<span style="white-space:pre"> </span>cout<<"i="<<i<<"j="<<j<<endl;
<span style="white-space:pre"> </span>}
, 。
16
enum weekday{mon,tues};
<span style="white-space:pre"> </span> weekday ws=mon;
<span style="white-space:pre"> </span> cout<<ws<<endl;
17 <span style="white-space:pre"> </span> static
#include <iostream>
using namespace std;
void fun()
{
<span style="white-space:pre"> </span>static int i=0;
<span style="white-space:pre"> </span>i++;
<span style="white-space:pre"> </span>cout<<"i="<<i<<endl;
}
int main()
{
<span style="white-space:pre"> </span>fun();
<span style="white-space:pre"> </span>fun();
<span style="white-space:pre"> </span>return 0;
}
/*
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>i=1
<span style="white-space:pre"> </span>i=2
*/
18 UML
19
static
::
: , . . const, !!!
:
。 。
#include <iostream>
using namespace std;
class test
{
public:
static int num;
};
int test::num = 0;
void main()
{
cout<<test::num <<endl;
test::num = 20;
cout<<test::num <<endl;
}
,
,
20
friend , private protected
:
B A ,B A , A B 、 。
21
,
, ( )
const
22 : 。
const &
, , ,
22
–#include< >
, C++ include
–#include" "
, ,
#define
– , const 。
– , 。
#undef
– #define ,
#if
// “ ”
1
#else
// “ ”
2
#endif
#ifdef
1
#else
2
#endif
“ ” #defined , undef , 1, 2。
#ifndef
1
#else
2
#endif
“ ” , 1, 2。
//head.h
#ifndef HEAD_H
#define HEAD_H
…
class Point
{
…
}…
#endif
23
:
, ,
extern
:
24 static
25
namespace {
int n;
void fun()
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>}
}
26
, : , ,
,
void fun( int arr[][5]) //
{
<span style="white-space:pre"> </span>//
}
27
n n
#include <iostream>
#include <iomanip>
using namespace std;
class A
{
public:
<span style="white-space:pre"> </span>int num;
<span style="white-space:pre"> </span>A()
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>cout<<" "<<endl;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>A(int n):num(n)
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>cout<<" "<<endl;
<span style="white-space:pre"> </span>}
};
int main( )
{
<span style="white-space:pre"> </span>A arr[5]={1,2,3,4,5};
<span style="white-space:pre"> </span>A* poinArr[3];
<span style="white-space:pre"> </span>return 0;
}
28 &var var
0,
, , unsigned long int
void vobject;// , void
void *pv;// , void
//void int :
pint = (int *)pv;
29
const char *name1="John"; //
char s[]="abc";
name1=s; // ,name1
*name1='1'; //
char *const name2="John";
name2="abc";// ,
const int a; int const a; , a int 。
const int *a;
int const *a;
const( , const)
int *const a; const( , const)
const int *a; a , int int , int 。 int const* a; 。
int * const a; a , int , 。
int const * a const; , int const * const a; a , int int , , int 。 const int* const a; 。
:y=*px++ y=*(px++) (* ++ , )
– 。 :p==0 p!=0
30
for(i=0; i<10; i++)
cout<<a[i];
for(i=0; i<10; i++)
cout<<*(a+i);
for(p=a; p<(a+10); p++)
cout<<*p;
Point *pa[2]; // pa[0],pa[1]
cout<<*(*(array2+i)+j)<<" ";
// cout<<array2[i][j]<<" ";
31
:
(* ) ( );
void print_stuff(float data_to_print);
void (*function_pointer)(float);
print_stuff(pi);
function_pointer= print_stuff;
Point A(5,10);
Piont *ptr;
ptr=&A;
->
ptr->getx() (*ptr).getx();
32 this
。
。
, this , , , this 。
33
34
new T( )
: , T , 。
: :T , 。 :0(NULL)
delete P
: P 。P new 。
new delete
new int
new int() // , 0
int *p=new int [5]();// () 0
delete []
int *p=new int [5]();
delete[] p;
delete []
char (*fp)[3];
fp = new char[2][3];
new T[ ][ ]..
,
T T , .
float (*fp)[25][10];
fp=new float [10][25][10];
vector
vector< > ( )
:
//vector , 0, .vector
vector< > { , }
vector<int> arr(10);
vector<int> arr(10);
<span style="white-space:pre"> </span>for (int i=0;i<10;i++)
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>cout<<arr[i]<<endl;
<span style="white-space:pre"> </span>}
malloc
void *malloc( size );
size:
: , void 。 ,
free
void free( void *memblock );
memblock: , 。
:
:<cstdlib> <cmalloc>
35
C c1,c2;
c2=c1;
, , .
36
strcat( ),strcpy( ),strcmp( ),strlen( ),strlwr( ),strupr( )
<cstring>
cin>>
getline(cin,s2) , , .
getline(cin,s2,',') ','
37
–
–
–
public protected , private 。
public protected , private 。
// public
protected
, private 。
, public 。
, , 。
class A
{
<span style="white-space:pre"> </span>protected:
<span style="white-space:pre"> </span>int x;
}
int main()
{
A a;
a.x=5; //
}
class A {
<span style="white-space:pre"> </span>protected:
<span style="white-space:pre"> </span>int x;
}
class B: public A
{
<span style="white-space:pre"> </span>public:
<span style="white-space:pre"> </span>void Function();
};
void B::Function()
{
<span style="white-space:pre"> </span>x=5; //
}
: ( ), .
:
38
, , ,
:: ( 1 , 2 ,... n , ): 1( ), 2( ), ... n( )
{
;
};
//
:: ( 1 , 2 ,... n , ): 1( ), 2( ), ... n( ),
{
;
};
//
class Derived: public Base2, public Base1, public Base3 {
<span style="white-space:pre"> </span>// Derived,
public:<span style="white-space:pre"> </span>//
<span style="white-space:pre"> </span>Derived(int a, int b, int c, int d): Base1(a), member2(d), member1(c), Base2(b) { }
<span style="white-space:pre"> </span>// ,
private:<span style="white-space:pre"> </span>//
<span style="white-space:pre"> </span>Base1 member1;
<span style="white-space:pre"> </span>Base2 member2;
<span style="white-space:pre"> </span>Base3 member3;
};
//
1. , ( )。
2. , 。
3. 。
39
, 。
, 。 :
C::C(C &c1):B(c1)
{…}
,
( ) 。
, 。
。
40
:
, 。
, , 。
, 。
41
//@@@
, , , —— 。
: c1.A::f() c1.B::f()
: C f(), f() A::f() B::f()
–
– virtual :class B1:virtual public B
– .
– ,
:
–
class B{ private: int b;};
class B1 : virtualpublic B { private: int b1;};
class B2 :virtualpublic B { private: int b2;};
class C : public B1, public B2{ private: float d;}
:
C cobj;
cobj.b;
( ) ,
,
//7_8.cpp
#include <iostream>
using namespace std;
// :
class Base0 {<span style="white-space:pre"> </span>// Base0
public:
Base0(int var):var0(var){}
<span style="white-space:pre"> </span>int var0;
<span style="white-space:pre"> </span>void fun0() { cout << "Member of Base0" << endl; }
};
class Base1: virtual public Base0 {<span style="white-space:pre"> </span>// Base1
public:<span style="white-space:pre"> </span>//
<span style="white-space:pre"> </span>Base1(int var):Base0(var){}
<span style="white-space:pre"> </span>int var1;
};
class Base2: virtual public Base0 {<span style="white-space:pre"> </span>// Base2
public:<span style="white-space:pre"> </span>//
<span style="white-space:pre"> </span>Base2(int var):Base0(var){}
<span style="white-space:pre"> </span>int var2;
};
class Derived: public Base1, public Base2 {<span style="white-space:pre"> </span>// Derived
public:<span style="white-space:pre"> </span>//
<span style="white-space:pre"> </span>Derived(int var):Base0(var),Base1(var),Base2(var){}
<span style="white-space:pre"> </span>int var;
<span style="white-space:pre"> </span>void fun() { cout << "Member of Derived" << endl; }
};
int main() {<span style="white-space:pre"> </span>//
<span style="white-space:pre"> </span>Derived d;<span style="white-space:pre"> </span>// Derived d
<span style="white-space:pre"> </span>d.var0 = 2;<span style="white-space:pre"> </span>//
<span style="white-space:pre"> </span>d.fun0();<span style="white-space:pre"> </span>//
<span style="white-space:pre"> </span>return 0;
}
42
。
:
:
:
: ,
:
( ):
( ): ( )
:
–
–
–
:( )
– , 。
– , 。
C++ :. .* :: ?:
C++ , 。
。
。
, 。
//
。
( )。
operator ( )
{
......
}
= -1( ++、-- )
= , 。
B
– B , oprd1 B oprd2, oprd1 A , B A , oprd2 。
– , oprd1 B oprd2 oprd1.operator B(oprd2)
// U
– U , U oprd, oprd A , U A , 。
– , U oprd oprd.operator U()
ock& operator ++ ();<span style="white-space:pre"> </span>//
<span style="white-space:pre"> </span>
// ++ --
– ++ -- , oprd++ oprd--, oprd A , ++ -- A , int 。
– , oprd++ oprd.operator ++(0)
Clock operator ++ (int);<span style="white-space:pre"> </span>//
B , oprd1 B oprd2 operator B(oprd1,oprd2 )
B , B oprd operator B(oprd )
++ -- , oprd B operator B(oprd,0 )
//8_3.cpp
#include <iostream>
using namespace std;
class Complex {<span style="white-space:pre"> </span>//
public:<span style="white-space:pre"> </span>//
<span style="white-space:pre"> </span>Complex(double r = 0.0, double i = 0.0) : real(r), imag(i) { }<span style="white-space:pre"> </span>//
<span style="white-space:pre"> </span>friend Complex operator + (const Complex &c1, const Complex &c2);<span style="white-space:pre"> </span>// +
<span style="white-space:pre"> </span>friend Complex operator - (const Complex &c1, const Complex &c2);<span style="white-space:pre"> </span>// -
<span style="white-space:pre"> </span>friend ostream & operator << (ostream &out, const Complex &c);<span style="white-space:pre"> </span>// <<
private:<span style="white-space:pre"> </span>//
<span style="white-space:pre"> </span>double real;<span style="white-space:pre"> </span>//
<span style="white-space:pre"> </span>double imag;<span style="white-space:pre"> </span>//
};<span style="white-space:pre"> </span>
Complex operator + (const Complex &c1, const Complex &c2) {<span style="white-space:pre"> </span>//
<span style="white-space:pre"> </span>return Complex(c1.real + c2.real, c1.imag + c2.imag);
}
Complex operator - (const Complex &c1, const Complex &c2) {<span style="white-space:pre"> </span>//
<span style="white-space:pre"> </span>return Complex(c1.real - c2.real, c1.imag - c2.imag);
}
ostream & operator << (ostream &out, const Complex &c) {<span style="white-space:pre"> </span>//
<span style="white-space:pre"> </span>out << "(" << c.real << ", " << c.imag << ")";
<span style="white-space:pre"> </span>return out;
}
int main() {<span style="white-space:pre"> </span>//
<span style="white-space:pre"> </span>Complex c1(5, 4), c2(2, 10), c3;<span style="white-space:pre"> </span>//
<span style="white-space:pre"> </span>cout << "c1 = " << c1 << endl;
<span style="white-space:pre"> </span>cout << "c2 = " << c2 << endl;
<span style="white-space:pre"> </span>c3 = c1 - c2;<span style="white-space:pre"> </span>//
<span style="white-space:pre"> </span>cout << "c3 = c1 - c2 = " << c3 << endl;
<span style="white-space:pre"> </span>c3 = c1 + c2;<span style="white-space:pre"> </span>//
<span style="white-space:pre"> </span>cout << "c3 = c1 + c2 = " << c3 << endl;
<span style="white-space:pre"> </span>return 0;
}
43
。
。
, .
, virtual。
virtual , 。
, , , 。
: 。
// : , ,
:
1.
2.
3. ,
// .
//8_4.cpp
#include <iostream>
using namespace std;
class Base1 { // Base1
public:
<span style="white-space:pre"> </span>virtual void display() const;<span style="white-space:pre"> </span>//
};
void Base1::display() const {
<span style="white-space:pre"> </span>cout << "Base1::display()" << endl;
}
class Base2: public Base1 { // Base2
public:
<span style="white-space:pre"> </span>void display() const;<span style="white-space:pre"> </span>//
};
void Base2::display() const {
<span style="white-space:pre"> </span>cout << "Base2::display()" << endl;
}
class Derived: public Base2 { // Derived
public:
<span style="white-space:pre"> </span>void display() const;<span style="white-space:pre"> </span>//
};
void Derived::display() const {
<span style="white-space:pre"> </span>cout << "Derived::display()" << endl;
}
void fun(Base1 *ptr) { //
<span style="white-space:pre"> </span>ptr->display();<span style="white-space:pre"> </span>//" -> "
}
int main() {<span style="white-space:pre"> </span>//
<span style="white-space:pre"> </span>Base1 base1;<span style="white-space:pre"> </span>// Base1
<span style="white-space:pre"> </span>Base2 base2;<span style="white-space:pre"> </span>// Base2
<span style="white-space:pre"> </span>Derived derived;<span style="white-space:pre"> </span>// Derived
<span style="white-space:pre"> </span>fun(&base1);<span style="white-space:pre"> </span>// Base1 fun , Base1
<span style="white-space:pre"> </span>fun(&base2);<span style="white-space:pre"> </span>// Base2 fun , Base2
<span style="white-space:pre"> </span>fun(&derived);<span style="white-space:pre"> </span>// Derived fun , derived
<span style="white-space:pre"> </span>return 0;
}
C++
, .
//8_5.cpp
#include <iostream>
using namespace std;
class Base {
public:
<span style="white-space:pre"> </span>virtual ~Base();
};
Base::~Base() {
<span style="white-space:pre"> </span>cout<< "Base destructor" << endl;
}
class Derived: public Base {
public:
<span style="white-space:pre"> </span>Derived();
<span style="white-space:pre"> </span>~Derived();
private:
<span style="white-space:pre"> </span>int *p;
};
Derived::Derived() {
<span style="white-space:pre"> </span>p = new int(0);
}
Derived::~Derived() {
<span style="white-space:pre"> </span>cout << "Derived destructor" << endl;
<span style="white-space:pre"> </span>delete p;
}
void fun(Base* b) {
<span style="white-space:pre"> </span>delete b;
}
int main() {
<span style="white-space:pre"> </span>Base *b = new Derived();
<span style="white-space:pre"> </span>fun(b);
<span style="white-space:pre"> </span>return 0;
}
44
, , ,
:
virtual ( )=0;//
– , , 。
– , , 。
– 。
– 。
– , 。
45
, , 。
:
template <typename >
//
#include<iostream>
using namespace std;
template<typename T>
T abs(T x)
{
<span style="white-space:pre"> </span>return x<0?-x:x;
}
int main()
{
<span style="white-space:pre"> </span>int n=-5;
<span style="white-space:pre"> </span>double d=-5.5;
<span style="white-space:pre"> </span>cout<<abs(n)<<endl;
<span style="white-space:pre"> </span>cout<<abs(d)<<endl;
}
, 、 、 , ( )。
:
template < >
class
{ }
,
template < >
<T>:: ( )
//
//9_2.cpp
#include <iostream>
#include <cstdlib>
using namespace std;
struct Student {<span style="white-space:pre"> </span>// Student
<span style="white-space:pre"> </span>int id;<span style="white-space:pre"> </span>//
<span style="white-space:pre"> </span>float gpa;<span style="white-space:pre"> </span>//
};
template <class T> // :
class Store {
private:
<span style="white-space:pre"> </span>T item;<span style="white-space:pre"> </span>// item
<span style="white-space:pre"> </span>bool haveValue;<span style="white-space:pre"> </span>// haveValue item
public:
<span style="white-space:pre"> </span>Store();<span style="white-space:pre"> </span>// ( )
<span style="white-space:pre"> </span>T &getElem();<span style="white-space:pre"> </span>//
<span style="white-space:pre"> </span>void putElem(const T &x); //
};
// 。
template <class T><span style="white-space:pre"> </span>//
Store<T>::Store(): haveValue(false) { }
template <class T> //
T &Store<T>::getElem() {
<span style="white-space:pre"> </span>if (!haveValue) {<span style="white-space:pre"> </span>// ,
<span style="white-space:pre"> </span>cout << "No item present!" << endl;
<span style="white-space:pre"> </span>exit(1);<span style="white-space:pre"> </span>// , 。
<span style="white-space:pre"> </span>// ,
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>return item; // item
}
template <class T><span style="white-space:pre"> </span>//
void Store<T>::putElem(const T &x) {
<span style="white-space:pre"> </span>haveValue = true;<span style="white-space:pre"> </span>// haveValue true, item
<span style="white-space:pre"> </span>item = x;<span style="white-space:pre"> </span>// x item
}
int main() {
<span style="white-space:pre"> </span>Store<int> s1, s2;<span style="white-space:pre"> </span>// Store<int> , item int
<span style="white-space:pre"> </span>s1.putElem(3);<span style="white-space:pre"> </span>// S1 ( S1)
<span style="white-space:pre"> </span>s2.putElem(-7);<span style="white-space:pre"> </span>// S2 ( S2)
<span style="white-space:pre"> </span>cout << s1.getElem() << " " << s2.getElem() << endl;<span style="white-space:pre"> </span>// S1 S2
<span style="white-space:pre"> </span>Student g = { 1000, 23 };<span style="white-space:pre"> </span>// Student
<span style="white-space:pre"> </span>Store<Student> s3;<span style="white-space:pre"> </span>// Store<Student> s3, item Student
<span style="white-space:pre"> </span>s3.putElem(g); // D ( D)
<span style="white-space:pre"> </span>cout << "The student id is " << s3.getElem().id << endl;<span style="white-space:pre"> </span>// s3
<span style="white-space:pre"> </span>Store<double> d;<span style="white-space:pre"> </span>// Store<double> s4, item double
<span style="white-space:pre"> </span>cout << "Retrieving object d... ";
<span style="white-space:pre"> </span>cout << d.getElem() << endl; // D
<span style="white-space:pre"> </span>// d , D.getElement()
<span style="white-space:pre"> </span>return 0;
}
46 STL
– (container)
– (iterator)
– (algorithms)
– (function object)
(namespace)
(named scope)
–
– , NS:
namspace NS {
class File;
void Fun ();
}
,
NS:: File obj;
NS:: Fun ();
using
– , :using NS::File; File
–using namespace std; std
47
#include<iostream.h>
int Div(int x,int y);
int main()
{
<span style="white-space:pre"> </span>try
<span style="white-space:pre"> </span>{ cout<<"5/2="<<Div(5,2)<<endl;
<span style="white-space:pre"> </span>cout<<"8/0="<<Div(8,0)<<endl;
<span style="white-space:pre"> </span>cout<<"7/1="<<Div(7,1)<<endl;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>catch(int)
<span style="white-space:pre"> </span>{ cout<<"except of deviding zero.
"; }
<span style="white-space:pre"> </span>cout<<"that is ok.
";
<span style="white-space:pre"> </span>}
int Div(int x,int y)
{
if(y==0) throw y;
return x/y;
}