電商專業(yè)學(xué)習(xí)嵌入式軟件開發(fā)第六十天

  • C++第六天

今天講了純虛函數(shù),虛析構(gòu)函數(shù),友元和運(yùn)算符重載,講的東西感覺有點(diǎn)多,但是題目做的也多,即使還是寫不出來也是有印象了永毅。今天老師給我們留的作業(yè)內(nèi)容是課上都講過的,也算是有例題,原本以為把例題改一下就可以了,沒想到?jīng)]用,錯誤百出,試著改錯也改不出來,明天看看老師是咋改的吧镣衡。

//homoework:
#include <iostream>
#include <string>
using namespace std;

class Teacher
{
public:
    Teacher(){}
    Teacher(string name, float base, float subsidy, int num)
    {
        m_strName = name;
        m_fBaseSalary = base;
        m_fSubsidy = subsidy;
        m_iNum = num;
    }
    int getNum()
    {
        return m_iNum;
    }
    virtual float salary()=0;
    void show()
    {
        cout << "名字:" << m_strName << " 基本工資:" << m_fBaseSalary << " 補(bǔ)貼:" << m_fSubsidy << " 課時:" << m_iNum << " 總工資:" << salary() << endl;
    }
private:
    string m_strName;
    float m_fBaseSalary;
    float m_fSubsidy;
    int m_iNum;
};
class Lecturer: public Teacher
{
public:
    Lecturer(){}
    Lecturer(string name, float base, float subsidy, int num)
        : Teacher(name, base, subsidy, num)
    {}
    float salary()
    {
        return 2000+20*getNum();
    }
};
class Professor: public Teacher
{
public:
    Professor(string name, float base, float subsidy, int num)
        : Teacher(name, base, subsidy, num)
    {}
    float salary()
    {
        return 5000+50*getNum();
    }
};
class AssociateProfessor: public Teacher
{
public:
    AssociateProfessor(string name, float base, float subsidy, int num)
        : Teacher(name, base, subsidy, num)
    {}
    float salary()
    {
        return 3000+30*getNum();
    }
};
int main(void)
{
    Lecturer lec("lisi", 2000, 20, 90);
    lec.show();

    AssociateProfessor ap("wangwu", 3000,30,70);
    ap.show();

    Professor pro("zhaoliu", 5000, 50, 100);
    pro.show();
    return 0;
}
//虛析構(gòu)函數(shù)
#include <iostream>
#include <string.h>
using namespace std;

class A
{
public:
    A(const char *data=NULL)
    {
        m_data = NULL;
        if (NULL != data)
        {
            int len = strlen(data);
            m_data = new char[len+1];
            strcpy(m_data, data);
        }
        cout << "A(...)" << endl;
    }
    //若存在繼承派生的情況,則一般將基類的析構(gòu)函數(shù)定義為虛函數(shù),可以通過基類的指針來完全釋放派生類的對象
    virtual ~A()
    {
        if (NULL != m_data)
        {
            delete []m_data;
            m_data = NULL;
        }
        cout << "~A()" << endl;
    }
private:
    char *m_data;
};
class B: public A
{
public:
    B(const char *data=NULL, const char *a=NULL)
        : A(a)
    {
        m_data = NULL;
        if (NULL != data)
        {
            int len = strlen(data);
            m_data = new char[len+1];
            strcpy(m_data, data);
        }
        cout << "B(...)" << endl;
    }
    //若基類的析構(gòu)函數(shù)為虛函數(shù),則派生類中的析構(gòu)函數(shù)默認(rèn)為虛函數(shù)
    ~B()
    {
        if (NULL != m_data)
        {
            delete []m_data;
            m_data = NULL;
        }
        cout << "~B()" << endl;
    }
private:
    char *m_data;
};
int main(void)
{
#if 0
    A *pa = new A("hello");
    delete pa;

    B *pb = new B("asa", "ewqqqq");
    delete pb;
#endif
    A *pp = new B("wqq", "qwwqqqq");
    delete pp;

    return 0;
}
//重載加減運(yùn)算符
#include <iostream>
using namespace std;

class Complex
{
public:
    Complex(float real=0, float vir=0)
    {
        m_fReal = real;
        m_fVir = vir;
    }
    void show()
    {
        cout << m_fReal << '+' << m_fVir << 'i' << endl;
    }
friend Complex operator+(Complex &c1, Complex &c2);
private:
    float m_fReal;
    float m_fVir;
};
Complex operator+(Complex &c1, Complex &c2)
{
    Complex c;
    c.m_fReal = c1.m_fReal+c2.m_fReal;
    c.m_fVir = c1.m_fVir+c2.m_fVir;
    return c;
}
int main(void)
{
    Complex c1(3, 7);
    Complex c2(13, 5);
    Complex c3 = c1 + c2;
    c3.show();
    return 0;   
}
//從載輸出輸入運(yùn)算符
#include <iostream>
using namespace std;

class Complex
{
public:
    Complex(float real=0, float vir=0)
    {
        m_fReal = real;
        m_fVir = vir;
    }
friend Complex operator+(Complex &c1, Complex &c2);
friend Complex operator-(Complex &c1, Complex &c2);
friend ostream& operator<<(ostream& out, const Complex &com);
private:
    float m_fReal;
    float m_fVir;
};
Complex operator+(Complex &c1, Complex &c2)
{
    Complex c;
    c.m_fReal = c1.m_fReal+c2.m_fReal;
    c.m_fVir = c1.m_fVir+c2.m_fVir;
    return c;
}
Complex operator-(Complex &c1, Complex &c2)
{
    Complex c;
    c.m_fReal = c1.m_fReal-c2.m_fReal;
    c.m_fVir = c1.m_fVir-c2.m_fVir;
    return c;
}
//cout << 90;
ostream& operator<<(ostream& out, const Complex &com)
{
    out << '('<<com.m_fReal<<')' << '+' << '('<<com.m_fVir<<')' << 'i' << endl;
    out << "operator<<(...)\n";
    return out;
}
int main(void)
{
    Complex c1(3, 7);
    Complex c2(13, 5);
    //Complex c3 = c1 + c2;
    Complex c3 = operator+(c1,c2);
    cout << c3;
    //Complex c4 = c2 - c1;
    Complex c4 = operator-(c2,c1);
    cout << c4;
    return 0;   
}
//重載自增
#include <iostream>
#include <unistd.h>
using namespace std;

class Time
{
public:
    Time(int min, int sec)
    {
        m_iMinute = min;
        m_iSecond = sec;
    }
    Time &operator++()//前加加
    {
        m_iSecond++;
        if (60 == m_iSecond)
        {
            m_iSecond= 0;
            m_iMinute++;
            if (60 == m_iMinute)
            {
                m_iMinute = 0;
            }
        }
        return *this;
    }
friend ostream &operator<<(ostream& out, const Time &time);
private:
    int m_iMinute;
    int m_iSecond;
};
ostream &operator<<(ostream& out, const Time &time)
{
    out << time.m_iMinute << ':' << time.m_iSecond;
    return out;
}
int main(void)
{
    Time t(59, 50);
    cout << t << endl;

    Time t2 = ++t;
    cout << "t:" << t << " t2:" << t2 << endl;
    while (1)
    {
        ++t;
        cout << t << endl;
        sleep(1);
    }
    return 0;
}
//重載自增
#include <iostream>
#include <unistd.h>
using namespace std;

class Time
{
public:
    Time(int min, int sec)
    {
        m_iMinute = min;
        m_iSecond = sec;
    }
    //后加加
    Time operator++(int)
    {
        Time t = *this;
        m_iSecond++;
        if (60 == m_iSecond)
        {
            m_iSecond= 0;
            m_iMinute++;
            if (60 == m_iMinute)
            {
                m_iMinute = 0;
            }
        }
        return t;
    }
friend ostream &operator<<(ostream& out, const Time &time);
private:
    int m_iMinute;
    int m_iSecond;
};
ostream &operator<<(ostream& out, const Time &time)
{
    out << time.m_iMinute << ':' << time.m_iSecond;
    return out;
}
int main(void)
{
    Time t(59, 50);
    Time t2 = t++;
    cout << "t2:" << t2 << " t:" << t <<endl;
    return 0;
}
//重載自減
#include <iostream>
#include <unistd.h>
using namespace std;

class Time
{
public:
    Time(int min, int sec)
    {
        m_iMinute = min;
        m_iSecond = sec;
    }
//后減減
    Time &operator--()
    {
        m_iSecond--;
        if (-1 == m_iSecond)
        {
            m_iSecond= 59;
            m_iMinute--;
            if (-1 == m_iMinute)
            {
                m_iMinute = 59;
            }
        }
        return *this;
    }
    Time operator--(int)
    {
        Time t = *this;
        m_iSecond--;
        if (-1 == m_iSecond)
        {
            m_iSecond= 59;
            m_iMinute--;
            if (-1 == m_iMinute)
            {
                m_iMinute = 59;
            }
        }
        return t;
    }
friend ostream &operator<<(ostream& out, const Time &time);
private:
    int m_iMinute;
    int m_iSecond;
};
ostream &operator<<(ostream& out, const Time &time)
{
    out << time.m_iMinute << ':' << time.m_iSecond;
    return out;
}
int main(void)
{
    Time t(59, 50);
    Time t2 = t--;
    cout << "t2:" << t2 << " t:" << t <<endl;
    return 0;
}
//重載賦值運(yùn)算符
#include <iostream>
#include <string.h>
using namespace std;
class MyString
{
public:
    ~MyString()
    {
        if (NULL != m_data)
        {
            delete []m_data;
        }
        cout << "~MyString()\n";
    }
    MyString &operator=(const MyString &other)
    {
        if (this == &other)
        {
            return *this;
        }
        if (NULL != m_data)
        {
            delete []m_data;
            m_data = NULL;
        }
        if (NULL == other.m_data)
        {
            return *this;
        }
        m_iLen = other.m_iLen;
        m_data = new char[m_iLen+1];
        strcpy(m_data, other.m_data);
        cout << "operator=(...)\n";
    }
    //拷貝構(gòu)造函數(shù)
    MyString(const MyString &other)
    {
        m_iLen = other.m_iLen;
        if (NULL == other.m_data)
        {
            m_data = NULL;
        }
        else
        {
            m_data = new char[m_iLen+1];
            if (NULL != m_data)
            {
                strcpy(m_data, other.m_data);
            }
        }
        cout << "MyString(MyString&)\n";
    }
    MyString(const char *data = NULL)
    {
        if (NULL == data)
        {
            m_data = NULL;
            m_iLen = 0;
        }
        else
        {
            int len = strlen(data);
            m_data = new char[len+1];
            if (NULL == m_data)
            {
                cout << "new failed\n";
                return;
            }
            strcpy(m_data, data);
            m_iLen = len;
            cout << m_data << ':';
        }
        cout << "MyString(...)\n";
    }
    const char *data()
    {
        return m_data;
    }
private:
    int m_iLen;
    char *m_data;
};
int main(void)
{
    MyString s("HelloWorld");
    //MyString s2 = s;  //MyString s2(s);
    //MyString s3;
    MyString s3("$$$$$$$$");
    s3 = s; 
    //s3.operator=(s);

    return 0;
}
//重載運(yùn)算符
#include <iostream>
using namespace std;

class Complex
{
public:
    //explicit:防止隱式類型轉(zhuǎn)換
    explicit Complex(float real = 0,float vir = 0)
    {
        m_fReal = real;
        m_fVir = vir;
        cout << "Complex(...)\n";
    }
    void show()
    {
        cout << m_fReal << '+' << m_fVir << 'i' << endl;
    }
friend Complex operator+(Complex &c1,Complex &c2);
friend Complex operator-(Complex &c1,Complex &c2);
private:
    float m_fReal;
    float m_fVir;
};
Complex operator+(Complex &c1,Complex &c2)
{
    Complex c;
    c.m_fReal = c1.m_fReal+c2.m_fReal;
    c.m_fVir = c1.m_fVir+c2.m_fVir;
    return c;
}
Complex operator-(Complex &c4,Complex &c5)
{
    Complex c;
    c.m_fReal = c4.m_fReal-c5.m_fReal;
    c.m_fVir = c4.m_fVir-c5.m_fVir;
    return c;
}
void fun(Complex com)
{
    com.show();
}
int main(int argc,char *argv[])
{
    //隱式類型轉(zhuǎn)換
    fun(23);
    return 0;
}
//重載運(yùn)算符
#include <iostream>
using namespace std;

class Complex
{
public:
    //explicit:防止隱式類型轉(zhuǎn)換
    //explicit Complex(float real=0, float vir=0)
    Complex(float real=0, float vir=0)
    {
        m_fReal = real;
        m_fVir = vir;
        cout << "Complex(...)\n";
    }
    void show()
    {
        cout << '('<<m_fReal<<')' << '+' << '('<<m_fVir<<')' << 'i' << endl;
    }
friend Complex operator+(const Complex &c1, const Complex &c2);
friend Complex operator-(Complex &c1, Complex &c2);
private:
    float m_fReal;
    float m_fVir;
};

Complex operator+(const Complex &c1, const Complex &c2)
{
    Complex c;
    c.m_fReal = c1.m_fReal+c2.m_fReal;
    c.m_fVir = c1.m_fVir+c2.m_fVir;
    return c;
}
Complex operator-(Complex &c1, Complex &c2)
{
    Complex c;
    c.m_fReal = c1.m_fReal-c2.m_fReal;
    c.m_fVir = c1.m_fVir-c2.m_fVir;
    return c;
}
//Complex com(23);
void fun(Complex com)
{
    com.show();
}
//void fun(int a){}
int main(void)
{
    //隱式類型轉(zhuǎn)換
    //fun(23);
    Complex c1(2, 3);
    Complex c2 = c1 + 18;
    return 0;   
}

友元:

#include <iostream>
#include <string>
using namespace std;
class Girl
{
public:
    Girl(string name, string phone)
    {
        m_strName = name;
        m_strPhone = phone;
    }
    //將一個函數(shù)定義為類的友元,則可以在該函數(shù)中通過對象直接訪問類的私有成員
    //友元破壞了類的封裝性,能不用盡量不用
    friend void fun();
private:
    string m_strName;
    string m_strPhone;
};
void fun()
{
    Girl g("阿菲", "1818181818");
    cout << g.m_strName << endl;
}
int main(void)
{
    fun();
    return 0;
}
//重載小括號
#include <iostream>
using namespace std;

class Test 
{
public:
    void operator()(int a,int b)
    {
        int max = a;
        if(a < b)
        {
            max = b;
        }
        cout << "max:" << max << endl;
    }
};
int main(void)
{
    Test t;
//  t.operator()(34,89);
    t(34,89);//函數(shù)對象
    return 0;
}
//純虛函數(shù)
#include <iostream>
using namespace std;

class Shape
{
public:
    Shape(){}
    //純虛函數(shù)
    //包含純虛函數(shù)的類稱之為抽象類
    //抽象類不能定義對象
    //抽象類:為了實(shí)現(xiàn)多態(tài),統(tǒng)一接口

    //派生類中摇庙,如果沒有對純虛函數(shù)進(jìn)行定義,則派生類仍然為抽象類覆旱,不能定義對象
    
    //若派生類中對純虛函數(shù)進(jìn)行定義了,則可以生成對象
    virtual float area() = 0;
};
class Test: public Shape
{
public:
    Test(int i = 90): m_i(i){}
    int m_i;
};
class Square: public Shape
{
public:
    Square(float w=0): m_fWidth(w)
    {}
    float area()
    {
        cout << "Square::area()\n";
        return m_fWidth*m_fWidth;
    }
private:
    float m_fWidth;
};
class Triangle: public Shape
{
public:
    Triangle(float b=0, float h=0)
    {
        m_fBottom = b;
        m_fHight = h;
    }
    float area()
    {
        cout << "Triangle::area()\n";
        return m_fBottom*m_fHight/2;
    }
private:
    float m_fBottom;
    float m_fHight;
};
void show(Shape *pshape)
{
    cout << pshape->area() << endl;
}
int main(void)
{
//  Shape shape;  // X
    //Test t(123);

    cout << "shape:" << sizeof(Shape) << endl;

    return 0;
}

homework:
![圖片.png](http://upload-images.jianshu.io/upload_images/4254001-5c7d4033960ac8de.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末痘番,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子蝙茶,更是在濱河造成了極大的恐慌艺骂,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,470評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件隆夯,死亡現(xiàn)場離奇詭異钳恕,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)蹄衷,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,393評論 3 392
  • 文/潘曉璐 我一進(jìn)店門忧额,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人宦芦,你說我怎么就攤上這事宙址。” “怎么了调卑?”我有些...
    開封第一講書人閱讀 162,577評論 0 353
  • 文/不壞的土叔 我叫張陵抡砂,是天一觀的道長大咱。 經(jīng)常有香客問我,道長注益,這世上最難降的妖魔是什么碴巾? 我笑而不...
    開封第一講書人閱讀 58,176評論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮丑搔,結(jié)果婚禮上厦瓢,老公的妹妹穿的比我還像新娘。我一直安慰自己啤月,他們只是感情好煮仇,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,189評論 6 388
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著谎仲,像睡著了一般浙垫。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上郑诺,一...
    開封第一講書人閱讀 51,155評論 1 299
  • 那天夹姥,我揣著相機(jī)與錄音,去河邊找鬼辙诞。 笑死辙售,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的飞涂。 我是一名探鬼主播旦部,決...
    沈念sama閱讀 40,041評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼封拧!你這毒婦竟也來了志鹃?” 一聲冷哼從身側(cè)響起夭问,我...
    開封第一講書人閱讀 38,903評論 0 274
  • 序言:老撾萬榮一對情侶失蹤泽西,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后缰趋,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體捧杉,經(jīng)...
    沈念sama閱讀 45,319評論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,539評論 2 332
  • 正文 我和宋清朗相戀三年秘血,在試婚紗的時候發(fā)現(xiàn)自己被綠了味抖。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,703評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡灰粮,死狀恐怖仔涩,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情粘舟,我是刑警寧澤熔脂,帶...
    沈念sama閱讀 35,417評論 5 343
  • 正文 年R本政府宣布佩研,位于F島的核電站,受9級特大地震影響霞揉,放射性物質(zhì)發(fā)生泄漏旬薯。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,013評論 3 325
  • 文/蒙蒙 一适秩、第九天 我趴在偏房一處隱蔽的房頂上張望绊序。 院中可真熱鬧,春花似錦秽荞、人聲如沸骤公。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,664評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽淋样。三九已至,卻和暖如春胁住,著一層夾襖步出監(jiān)牢的瞬間趁猴,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,818評論 1 269
  • 我被黑心中介騙來泰國打工彪见, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留儡司,地道東北人。 一個月前我還...
    沈念sama閱讀 47,711評論 2 368
  • 正文 我出身青樓余指,卻偏偏與公主長得像捕犬,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子酵镜,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,601評論 2 353

推薦閱讀更多精彩內(nèi)容