<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    c++ 速成

    ??? 用了幾年java 了,突然想學習c++ ,昨天用了一天的時間瘋狂的學習了一天c++ 基礎知識,發現感覺還不錯,不過精驗告訴我,學編程語言一定要實踐,在這里指記錄了一些學習中的點滴。

    ? 1 const 與volatile 的用法
    ? 1 const
    ?? #include<conio.h>
    ?? #include<iostream.h>
    ?? //行參數指向const 類型變量的指針
    ?? void display_c(cons int * pi)
    ?? {
    ???? cout<<"display_c:"<<*pi<<endl;
    ?? }
    ?? //行參為普通類型變量的指針
    ?? void display(int *pi)
    ?? {
    ???? cout<<"display_c:"<<*pi<<endl;
    ?? }
    ?? //const 類型變量
    ?? const int i1=1;
    ?? //error? i1=3;
    ?? //const 類型變量的指針
    ?? int i2=2;
    ?? const int * pi2=&i2;
    ?? //error *pi2=3
    ?? // const 指針
    ?? int * const pi3=&i3;
    ?? *pi3=5;
    ?? // error *pi3=&i4? 可以賦予不同的值,但是不可一指向不同的變量
    ?? //指向const 內容的const 指針
    ?? const int * cosnt pi5=&i5;
    2 sizeof 返回某個便賴寧嘎或數據類型的長度
    3 引用
    ?1 引用變量
    ? int i=1;
    ? int & r_i=i;
    5 名空間
    ? 1 namespace
    ?? namespace car
    ? {
    ??? int model;
    ??? int length;
    ??? int width;
    ? }
    ? namespace plane
    ? {
    ??? int model;
    ??? namespace size
    ??? {
    ????? int lenth;
    ????? int width;
    ??? }
    ? }
    ? namespace car //添加名空間的成員
    ? {
    ??? char * name;
    ? }
    ? namespqce c=car; //定義別名
    ? int Time //外不變量屬于全局名空間
    ?
    ? car::length=3;
    ? plane::size::length=70;
    ?
    ? int Time=1996;
    ? ::Time=1997;

    ?2 using
    ? void main()
    ?{
    ?? using namespace car;
    ?? length;
    ?? using namespace phane;
    ?? model;

    ?}
    6 new 與delete 運算符
    ?double * pd;?? // define pointer variable
    ?pd=new double; // allocate memory
    ?if(pd!=null)
    ?{
    ?? ...
    ? delete pd;? // free memory
    ?}

    ?double1=null
    ?* pds=new double[100];
    ?if(pds)
    ?{
    ?? ....
    ? delete[] pds;

    ?}

    ?如果是使用new 進行內存空間分配沒有成功,則返回空指針null
    ?釋放一個數組分配的內存是,常常采用帶[]的形式
    7 void 指針 它指向一個地址值,但是不說名數據類型,可以使用void 指針創建一個通用的函數,在使用
    ?的時候將指針類型轉化成相應的具體類型。
    ?void ShowHex(void *pv,int siae)
    {
    ?....
    ?((unsigned char *)pv)[i]
    }
    void main()
    {
    ? void *pv=null;
    ? unsigned char c1=0x12;
    ? pv=&c1;
    ? ShowHex(pv,sizeof(c1));
    ?}
    9 typeid 運算符 用來求得變量或隊形愛女嘎的數據類型,返回一個type_info 類型的對象,通過該對象
    ? 可以獲取數據類型的名稱

    ? include<typeinfo.h>
    ? int i;
    ? const type_info &t0=typeid(i);
    ? t0.name();
    10 函數
    ?? 1 模板函數
    ? template<class T> T min(R &x,Tv&y)
    ?{
    ?? return x<y?x:y;
    ?}
    ? 2 指向函數的指針
    ?? int max(int x,int y)
    ? {
    ??? ...
    ? }
    ? int min(int x,int y)
    ? {
    ???? ...
    ? }
    ? int (* m_pfunction)(int,int);
    ? void main()
    ? {
    ??? m_pfunction=max;
    ??? (*m_pfunction)(2,3);
    ???? m_pfunction=min;
    ??? (*m_pfunction)(2,3);
    ? }
    11 類與對象
    ?1 構在函數和析構函數
    ?#include <iostream.h>
    #include <string.h>
    #include <conio.h>
    class Book
    {
    private:
    ? char * pBookName;
    public:
    ? int PageNum;
    public:
    ? Book(char * pBN=NULL);
    ? ~ B o o k ( v o i d ) ;
    ? void SetBookName(char * pBN);
    ? int GetBookName(char * pBN,unsigned int MaxSize);
    } ;

    Book:Book(char *PBN)
    {
    ? cout<<"構造函數"<<endl;
    ? pBookName=null;
    ? if(oBN!=null)
    ? {
    ???? pBookName=new char[strlen(pBN)+1];
    ???? if(pBookName!=null)
    ???????? strcyp(pBookName,pBN);
    ? }
    }

    Book::~Book()
    {
    ? delete[] pBookName;
    }

    void Book::SetBookName(char * pBN)
    {
    ? if(pBookName!=null)
    ??? delete[] pBookName;
    ?? pBookName=new char[strlen(pBN)+1];
    ???? if(pBookName!=null)
    ???????? strcyp(pBookName,pBN);
    }

    int Book::GetBookName(char * pBN,unsigned intmaxSize)
    {
    ?if((pBookName!=null))&&(MaxSize>strlen(pBookName))
    {
    ? strcpy(pBN,pBookName);
    ? retrun strlen(strlen(pBookName));
    ?}
    }

    // 使用
    Book b1;
    b1.SetBookName("test");
    Book b2(test1);
    ?2 對象的引用參數傳遞
    ? void Add(Book b)
    ? void AddRef(Book & b);
    ?3 靜態成員變量 是一個公共變量
    ?在初始化 的時候利用作用運算符:: 對私有類型的靜態成員變量可以向公有類型的靜態成變量一樣賦值
    ?但不能直接引用
    ?3 const 類型成員函數與mutable
    ?class CDate
    ?{
    ?? public:
    ??? int year;
    ??? mutable int month;
    ??? CDate(int y=2000,int m=1)
    ??? {
    ?????? year=y;
    ?????? month=m;
    ??? };
    ??? int BetMonth() const ;//read only
    ??? void SetMonth(int m);// write only
    ?}
    ? void CDate::SetMonth(int m)
    ?{
    ?? month=m;
    ?}
    ?void main()
    ?{
    ?? CDate d1;
    ??
    ?}

    ?在const 類型的成員函數定義中,不可一直接或簡介的修改普通類型的成員變量
    ?如果象修改const 類型對象的成員函數,可以使用關鍵字mutable 對該成員變量進行修改
    ?5 對象的初始化與初始化行
    ?將參數類表中的數值賦予成員變量時,不僅可以在構造函數的函數體中進行,以阿寬衣在初始化行中進行
    ?在初始化處驚醒初始化的情況有:
    ? 1 分層類的構在函數可以調用它的任何一個子對象的構造函數
    ? 2 對常量const 類型的成員變量的初始化必須在初始化行上
    ? 3 對于引用類型的成員變量的初始化必須在初始化行上
    ?
    ?class CPoint
    ?{
    ?? public:
    ??? int x,y;
    ??? CPoint(int ax=0,int ay=0)
    ??? {
    ????? x=ax;
    ????? y=ay;
    ??? }
    ?};

    ?class CRect
    ?{
    ?? private:
    ??? CPoint low_right;
    ??? CPoint up_left;
    ?? public:
    ??? int & CenterX;
    ??? const int CenterY;
    ??? CRect(int x1,int y1,int x2,int y2,int &x3,int y3)
    ???? :up_left(x1,y1),low_right(x2,y2),CenterX(x3),CenterY(y3)
    ?? {
    ????
    ?? }
    ?};
    void main()
    {
    ? int cx=5;
    ? int cy=6;
    ? CRect r1(1,2,3,4,cx,cy);
    }
    ?6 拷貝構造函數
    ? 拷貝構造函數與普通構造函數的差別在與棋參數類表,參數列表中有一個對象,該對象的數據類型是
    ?本類的一個引用,而且一般情況下,該對象還應該是一個const 類型的對象。
    ? 如果在類的定義是不是顯示的定義一個拷貝函數,則編譯器會自動的定義一個拷貝構造函數
    ?
    ?class CPoint
    ?{
    ?? public:
    ???? int x,y;
    ???? CPoint(int m_x=0,ubt m_y=0);?? // default constructor?
    ???? cPoint(const CPoint &c);????? //copy consrucotr
    ?};
    ?CPoint::CPoint(int m_x,int m_y)
    ?{
    ??
    ?}
    ?
    ?CPoint::CPoint(const CPoint &c)
    ?{
    ?? x=c.y;
    ?? y=c.x;
    ?}

    ?void main()
    ?{
    ?? CPoint c1(1,2);? //invoke default constructor
    ?? CPoint c2-c1;??? // invoke copy constructor
    ?}
    7 template class
    ?template<class t,int Size>class Array // template class
    ?{
    ?? private:
    ???? T arr[Size];
    ???? int CurSize;
    ?? public:
    ??? Array(T * date,int n)
    ??? {
    ????? CurSize=n<Size?n;Size;
    ????? for(int i=0;i<CurSize;i++)
    ????? {
    ??????? Arr[i]=data[i];?
    ????? }
    ??? }
    }

    void main()
    {
    ? int i1[10]={1,2,3,4,5,6,7,8,9};
    ? Array<int,6>array_i1(i1,i0);
    }

    1 友員類和友員函數
    ? #include <iostream.h>
    ? #include<string.h>
    ? #include<conio.h>

    class SoftWareEngineer; //先對SoftwareEngineer 類進行顯示說明一下

    class Computer // 定義Computer 類
    {
    ?? private:
    ???? int Price;
    ?? public:
    ???? Computer(int p){Price=p};
    ??? friend class SoftwareEngineer; //將友員類載這里聲明
    ??? frined void Judge(Computer &c,SoftwareEngineer? & s) //友員函數
    ???
    };

    class SoftwareEngineer
    {
    ?? int Salary;
    ?? char Name[20];
    ? public:
    ??? SoftwareEngineer(int s,char *n //構造函數)
    ? {
    ???? Salary=s;
    ???? strcpy(Name,n);
    ? }
    int GetComputerPrice(Computer &c){return c.Price} //訪問Computer 的思友變量
    friend void Judge(Computer &c,SoftwareEngineer & s) //友員函數
    };
    //判斷一個軟件工程師是否可以用一個月的薪水買的器一臺電腦
    void Judge(Computer &c,SoftwareEngineer &s) //橋友員函數
    {
    ?? if(c.Price>s.Salary)
    ???? cout<<"軟件工程師"<<s.Name<<"的一個月薪水買不起一臺電腦"<<endl;
    ?? else
    ???? cout<<"軟件工程師"<<s.Name<<"的一月薪水買的起一臺電腦"<<endl;
    }
    void main()
    {
    ? Computer c1(100);
    SoftwareEngineer s1(120,"user1");
    Judge(c1,s1);

    SiftwareEngineer s2(80,"user2")
    Judge(c1,s2);
    getch();
    }
    2運算符重載
    #include<iostream.h>
    #include<conio.h>
    #include<iomanip.h>

    class TValue{
    ?private:
    ???? int value;
    ?public:
    ?? TValue(int i){value=i}
    ?//成員函數重載運算符
    ? TValue operator!();
    ? TValue operator+(TValue & rv);
    ?// 友員函數重載運算符
    ? friend ostream & operator<<{ostream & os,TValue & rv};
    }

    TValue Tvalue::operator!()
    {
    ? return TValue(!value);
    }

    TValue TValue:operator+(TValue& rv)
    {
    ? return TValue(value+rv.value);
    }

    ostream & operator<<(ostream & os,TValue rv)
    {
    ?? os<<setw(5)<<rv.value;
    ? return os;
    }

    void main()
    {
    ?? TValue v1(3),v2(5);
    ?? cout<<
    }
    3 類的派生和繼承
    1class Box{
    ?? public:
    ???? int width,height;
    ???? void SetWidth(int w){width=w};
    ???? void SetWidth(int h){height=h;};
    ?};
    ?class TColorBox::public Box{
    ? public:
    ???? int color;
    ???? void SetColor(int c){color=c;};
    ?};

    void main(){
    ??? TColorBox cb;
    ??? cb.SetColor(255); //聲明非繼承類的對象
    ??? cb.SetWidth(100);//聲明繼承類的對象
    ??? cb.SetHeight(100); //聲明繼承類的對象
    ??
    ? }
    ?2 不能被繼承的成員
    ? 構造函數,析構函數,用戶定義的新操作符,用戶定義的賦值操作,友員關系
    ?3 構造函數,析構函數的調用順序
    class A{
    ?int a,b,c;
    ?public:
    ? A(int x,int y,int z)
    {
    ?? a=x;
    ?? b=y;
    ? c=z;
    }

    };
    class B{
    ? int d;
    ? public :
    ?? B(int xx):A(xx,xx+1,xx+2)(d=xx);//內聯構造函數
    ?? B(int x,int y,int z,int xx);//非內聯構造函數
    ? B:B(int x,int y,int z,int xx):A(x,y,z)
    {
    ?? d=xx;
    }
    }
    ?
    實例
    class Currency
    {
    ? poublic:
    ??? double par_value;
    ??? Currency(){per_value=0.0;}
    ??? Currency(double d){per_value=d;}
    ??? Currency(Currency &rc){par_value=rc.par_value;}
    ??
    ?? Currency & operator={Currency & c}
    ?? {
    ????? par_valuc=c.par_value;
    ????? return * this;
    ?? }
    ?? Currency & operator+(Currency & c)
    ? {
    ???? par_value+=c.par_value;
    ??? return *this;
    ? }
    }

    //人民幣
    ?class RMB:public Currency
    ?{
    ?? public:
    ?? RMB():Currency(){};//調用派生類的構造函數前,需要調用器基類的工造函數
    ?? RMB(double d):Currency(d){};
    ?? RMB(Currency& c):Currency(c){};
    ?? RMB(RMB& rmb):Currency(rnb){};
    ?? friend ostream& operator<<{ostream& os,RMB& rnb};//派生類的附加功能
    ?};
    ?ostream& operator<<{ostream& os, RMB& rmb} //output 運算符不能是一個類的成員函數
    {
    ? os<<"¥"<<setiosflags(ios::shwopoint|ios:fixed)<<setprecision(2)rmb.par_value;
    ?return os;
    }

    void main()
    {
    ?? RMB r_income(5000);
    ?? RMB r_balance;
    ?? r_balance=r_income=r_expense;
    ? cout<<"income"<<r_income<<endl;
    }
    4 將iostream 運算符重載
    ?1)ostream & operator<< (ostream &os,const Triangular & ths)
    {
    ?? os<<"("<<rhs.beg_pos()<<","<<rhs.length()<<")";
    ? rhs.display(rhs.length(),rhs.beg_pos(),os);
    }
    ouutput 運算符不能夠設計成member function
    ?2)istream& operator>>(istream &is,Triangular &rhs)
    ?{
    ??? char ch1,ch2;
    ??? int bp,len;
    ?? //輸入 ch1='(',bp=3,ch3=',' len=6
    ?? is>>ch1>>bp>>ch2>>len;
    ?? rhs.beg_pos(bp);
    ?? rhs.length(len);
    ?? rhs.next_reset();
    ?? return is;
    ?}
    Triangular tris;
    cin>>tri2
    4 虛基類
    載繼承關系中,同一基類被繼承多次,不僅會引器歧異,而起可能浪費空間
    ?class A
    {
    ?? public:
    ???? int value;
    };
    class B:public virtual A(); //虛基類 編譯器只產生一個基類版本。如果不定義為virtual 編譯器不知到調用那個value 了,當然
    class C:public virtual A();//? 也可以return B::value;
    class D:public b.public c
    {
    ? public
    ?? int GetValue(){return value;}
    };

    void main()
    {
    ? D dd;
    ? dd.GetValue();
    }

    5 多態形,和虛函數
    class TFirst
    {
    ?? public virtual void Display();
    };

    void TFirst::Display()
    {
    ?? cout<<"first "
    }

    class TSecond:public TFirst
    {
    ?? public:
    ???? virtual void Display();
    };

    void TSecond::Display()
    {
    ?? cout<<"second"
    }

    void Display(TRist * pFirst)
    {
    ?? pFisrt=>Display();
    }

    void main()
    {
    ?? TFirst * pFirst=new TFirst;
    ?? TSecond * pSecond=new TSecond;
    ?? pFirst->Display();
    ?? pSecond->Display();
    ?? Display(pFirst);
    ?? Display(pSecond);
    ??? delet pfirst ;
    ??? delet pSecond;
    ??
    ??? getch();
    }

    c++ builder 中的集合的
    ? 1 集合的概念基本
    ?? Set<type,minval,maxval>
    ?? Set<char,'A','C'> s1
    ??
    ?? tydefef Set(char,1,255) UPPERCASet;
    ?? UPPERCASESet s1;
    ?? s1<<'A'<<'B'<<'C';
    ?? sysset.h 直接包含載vcl.h 中
    ? 2 集合的操作
    ?? #include<iostream>
    ?? #include<system.hpp>
    ?? #include<conio.h>
    ??
    ?? using namespace std;
    ? typedef Set<char,'B','Z'> UpperSet;
    ? typedef Set<char,'a','z'> LoverSet;
    ? typeder Set<char,'a','j'> HalfLowerSet;

    ? void set_example()
    ? {
    ????? LowerSet ae,ae2,ac,de;
    ????? UpperSet AE,AE2,AC,DE;
    ???? HalfLowerSet aj;
    ? }

    異常處理
    1 c++ 的異常處理
    ?#include <iostream.h>
    ?#include <conio.h>

    ?class Crange
    ?{
    ??? public:
    ????? int index;
    ????? CRange(int i){index=i;}
    }
    ?? class CString
    ?? {
    ??????? char a[100];
    ??????? int len;
    ??????? public:
    ???????? CString(char * s)
    ??????? {
    ???????????? Len=strlen(s);
    ???????????? strcpy(a,s);
    ??????? }?
    ?????? char & operator[](int i)
    ????? {
    ?????????? if(i>0 && i<len) return a[i];
    ?????????? else throw CRange(i);
    ????? }
    ????
    ????? void DisplayNoCatch(CString & s)
    ???? {
    ???????????? int j;
    ???????????? for(j=0lj<20;j++)
    ?????????????? cout<<s[j]<<""endl;;
    ???? }
    ???
    ???? int DisplayHasCatch(CString & s)
    ??? {
    ??????? try{
    ?????????????? DisplayNoCatch(s);
    ??????? }catch(CRange r)
    ??????? {
    ??????????????? cerr<<r.index<<endl;
    ??????? }
    ???????? return 99;
    ??? }
    ?}
    ?2 多路捕捉
    ? #include<iostream.h>
    ? #include<conio.h>
    ? void MultiException()
    ? {
    ????? int i;
    ????? double d;
    ????
    ????? int no;
    ????? cout<<"(>0 and <5)";
    ????? cin>>no;
    ????? tyr
    ????? {
    ???????? switch(no)
    ??????? {
    ??????????? case 1;
    ?????????????? throw 123;
    ?????????????? break;
    ???????????? case 2:
    ?????????????? throw i;
    ?????????????? break;
    ???????????? case 3:
    ?????????????? throw d;
    ?????????????? break;
    ???????????? case 4:
    ?????????????? throw "Error";
    ??????????????? break;
    ????????????? default:
    ??????????????? cout<<"error";
    ??????? }
    ????? }
    ????? catch(int ie)
    ???? {
    ????????? cout<<"int"<<endl;
    ???? }
    ???? catch(double de)
    ???? {
    ???????? cout<<"double"<<endl;
    ???? }
    ???? catch(char* se)
    ??? {
    ????????? cout<<"char"<<endl;
    ??? }
    ? }

    ?3 bcb中的異常類
    ?? 1)卻省vcl 異常處理 無try catch 自動處理
    ???? int i=4,j=0;k;
    ???? k=i/k;
    ???? //下一句永遠不會執行
    ??? ShowMessage(k);
    ?? 2) 引發vcl 異常類
    ??? try{
    ??? }
    ??? catch(Exception &e)
    ??? {
    ????????? ShowMessage(e.,Message);
    ??? }
    ?? 不能引發如int double 等簡單類型的異常
    ?? 3) c++ 異常處理
    ??? try
    ??? {
    ???????? throw 2222;
    ??? }
    ??? catch(int ErrorCode)
    ?? {
    ???????? ShowMessage(ErrorCode);
    ??? }
    ?}
    ?4 單一捕捉異常
    ?? try{
    ????? k=10/0;
    ?? }catch(EDivByZero & E)
    ?? {
    ????? MessageDlg()?
    ?? }
    ? 5 捕捉所有異常
    ?? try
    ?? {
    ?? }catch(...) or(EExternalException &E)

    posted on 2006-08-25 15:28 康文 閱讀(2560) 評論(0)  編輯  收藏 所屬分類: c\c++

    <2006年8月>
    303112345
    6789101112
    13141516171819
    20212223242526
    272829303112
    3456789

    導航

    統計

    常用鏈接

    留言簿(1)

    隨筆分類

    隨筆檔案

    文章檔案

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 亚洲高清中文字幕| 亚洲国产精品乱码一区二区| 亚洲伊人久久大香线蕉影院| 日韩免费的视频在线观看香蕉| 亚洲精品午夜无码电影网| 国产大片免费网站不卡美女| 亚洲国产人成在线观看69网站| 久久国产乱子免费精品| 亚洲一区二区在线视频| 久草视频免费在线| 亚洲入口无毒网址你懂的| 免费黄色app网站| 手机永久免费的AV在线电影网| 亚洲一区无码精品色| 免费黄色app网站| 久久亚洲免费视频| 天天摸夜夜摸成人免费视频| 人成午夜免费视频在线观看| 九九热久久免费视频| 久久久久亚洲AV综合波多野结衣 | 91老湿机福利免费体验| 亚洲精品视频在线免费| 麻豆一区二区免费播放网站| 青青青亚洲精品国产| 亚洲精品乱码久久久久久蜜桃不卡 | 大桥未久亚洲无av码在线| 亚洲国产91精品无码专区| 香蕉免费一级视频在线观看| 亚洲一区二区影院| 国产精品美女自在线观看免费| 青青久久精品国产免费看| 亚洲av女电影网| 青草草在线视频永久免费| 久久www免费人成看国产片| 亚洲人成网站在线观看播放动漫| 午夜dj免费在线观看| 中文字幕在线免费看| 中文字幕精品三区无码亚洲| 久久国产免费一区| 老司机亚洲精品影院在线观看 | 亚洲成a人片在线播放|