物件導向 C++學習之我之過錯,物件導向學習過錯

來源:互聯網
上載者:User

物件導向 C++學習之我之過錯,物件導向學習過錯

////標註的那行為關鍵行,是易錯的地方

1.類內的函式宣告:

注意這裡是和C語言函式宣告不一樣的,不需要把形參名聲明,只用形參類型,但在定義的時候就需要把形參名聲明。

#include <iostream>#include <cmath>using namespace std;double sum;class Location{  public :     Location(double ,double );     double getx(){return x;}     double gety() {return y;} ////  double distance(Location &); //注意這裡是和C語言函式宣告不一樣的,不需要把形參名聲明,只用形參類型     friend double distance(Location &,Location &);  private :    double  x;    double  y;};Location ::Location(double a,double b){    x=a;    y=b;}double Location ::distance(Location &p){    double dx=x-p.x;    double dy=y-p.y;    return (double)sqrt(dx*dx+dy*dy);}double distance(Location &p1,Location &p2){    double dx=p1.x-p2.x;    double dy=p1.y-p2.y;    return sqrt(dx*dx+dy*dy);}int  main(){   Location a(-10,-20),b(-40,60);   cout<<a.getx()<<" "<<a.gety()<<endl;   cout<<b.getx()<<" "<<b.gety()<<endl;   cout<<a.distance(b)<<endl;   //distance(a,b);   return 0;}

2虛函數與重載函數

詳情看代碼

#include <iostream>using namespace std;//bp->func1執行的是衍生類別中的成員函數,func1是虛函數//bp->func2執行的是基類中的成員函數,失去了虛特性//func3是錯誤的 既不是虛函數也不為重載函數//bp->func4執行的是基類中的成員函數,普通的重載class Base{  public :     virtual void func1();     virtual void func2();     virtual void func3();     void func4();};class Derived:public Base{  public :      virtual void func1();      void func2(int x);      //char func3()       void func4();};void Base::func1(){    cout<<"B1"<<endl;}void Base::func2(){    cout<<"B2"<<endl;}void Base::func3(){    cout<<"B3"<<endl;}void Base::func4(){    cout<<"B4"<<endl;}void Derived::func1(){   cout<<"D1"<<endl;}void Derived::func2(int x){    cout<<"D2"<<endl;}void Derived::func4(){    cout<<"D4"<<endl;}int main(){     Base d1,*bp;     Derived d2;     bp=&d2;     bp->func1();     bp->func2();     bp->func4();     return 0;}

3.轉換構造與類型轉換

#include <iostream>using namespace std;//輸出結果的過程,1先尋找2個將類對象相加的運算子函數,程式中未有//2 然後尋找到可以將類對象轉換的轉換函式,進行調用//3 尋找將2個int型函數相加的運算子//4 由於a3=10,隱式調用得到real=imag=5class Complex{  public :   Complex() {};   Complex(int r,int i)   {   real=r;       imag=i;   }   Complex(int i)   {       real=imag=i/2;   }   operator int ()   {       return real+imag;   }   void print()   {       cout<<"real:"<<real<<"\t"<<"imag:"<<imag<<endl;   }   private :       int real;       int imag;};int main(){    Complex a1(1,2),a2(3,4);    Complex a3;    a3=a1+a2;    a3.print();    return 0;}

4.多重繼承與虛函數

#include <iostream>using namespace std;//B1的派生路徑:B1中的是虛函數,所以當指標指向obj3時,fun呈現虛特性,調用D::fun//B2的派生路徑:B2中的只是一般成員函數,所以此時是一個普通的重載函數,fun體現重載特性,調用B2class Base1{   public :       virtual void fun()       {           cout<<"B1"<<endl;       }};class Base2{   public :      void fun()      {          cout<<"B2"<<endl;      }};class Derived:public Base1,public Base2{    public :       void fun()       {           cout<<"D"<<endl;       }};int main(){    Base1 *ptr1;    Base2 *ptr2;    Derived obj3;    ptr1=&obj3;    ptr1->fun();    ptr2=&obj3;    ptr2->fun();    return 0;}

5.運算子多載

友元函數重載運算子在聲明的時候參數需要帶取地址

成員函數重載運算子的時候則不需要

class Three{  public :      Three (int I1,int I2,int I3);      friend Three operator ++(Three &);  //友元首碼++             Three operator ++();         //成員首碼++      friend Three operator ++(Three &,int)  //友元尾碼++             Three operator ++(int);         //成員尾碼++};     

6.衍生類別的建構函式

格式 :衍生類別名(參數總表):基類名(參數表)

class Student{  public :      Student (int number1,string name1,float score1)    {  number=number1;      name=name1;      score=score1;    }  protected :    int number;    int name;    int score;};class UStudent :public Student{    UStudent(int number1,string name1,float score1,string major1)    :Student(number1,name1,score1)     //定義衍生類別建構函式時,綴上要調用的基類的建構函式和參數    {        major=major1;    }   private :    string major;};

7.解構函式

指標的解構函式在delete之後才會調用
對象的解構函式則是在程式運行完成之後

#include <iostream>using namespace std;class aClass{public:    aClass()    {total++;}    ~aClass()    {total--;    cout<<"caonima"<<endl;}    int gettotal()    {        return total;    }private :    static int total;};int aClass::total=0;int main(){    aClass o1,o2,o3;    cout<<o1.gettotal()<<"   O in "<<endl;    aClass *p;    p=new aClass;    cout<<o1.gettotal()<<"   O in a a"<<endl;    delete p;    cout<<o1.gettotal()<<"   o in a  2"<<endl;    return 0;}

8.重載++符號

成員運算子多載  不需要加 &(引用號)

友元運算子多載   需要加&(引用號)

#include <iostream>using namespace std;class Coord{public :    Coord(int i=0,int j=0)    {        x=i;        y=j;    }    void print()    {        cout<<"x: "<<x<<" y:"<<y<<endl;    }    Coord operator++()       {                             ++x;                          ++y;        return *this;   //返回的是當前對象     }    friend Coord operator++(&ob)  //引用 如果沒有引用則無法進行++    {        ++ob.x;                         ++ob.y;        return ob;    }private :    int x,y;};int main(){   Coord ob(10,20);   ob.print();   ++ob;   ob.print();}

9.建構函式定義Complex (double r=0.0,double i=0.0);必須定義的時候加上=0.0,如果缺少的話,在定義一些對象的時候會因為賦隨機值出錯

#include <iostream>using namespace std;//對雙目運算子而言,成員運算子多載函數的形參表中僅有一個參數//它作為運算子的右運算元,做運算元是隱含的,是該類當前對象//是通過this指標隱含的傳給函數的class Complex{public :    Complex (double r=0.0,double i=0.0);    void print();    Complex operator +(Complex c);    friend Complex operator-(Complex &a,Complex &b);private :    double real;    double imag;};Complex::Complex(double r,double i){    real=r;    imag=i;}Complex Complex::operator+(Complex c){    Complex temp;    temp.imag=imag+c.imag;    temp.real=real+c.real;    return temp;}Complex operator -(Complex &a,Complex &b){    Complex temp;    temp.imag=a.imag-b.imag;    temp.real=a.real-b.real;    return temp;}void Complex::print(){    cout<<real<<"  "<<imag<<endl;}int main(){    Complex  A1(2.1,4.1),A2(2.3,4.6),A3,A4;    A3=A1+A2;    A4=A2-A1;    A3.print();    A4.print();    return 0;}







著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.