电商专业学习嵌入式软件开发第六十天

  • C++第六天

今天讲了纯虚函数,虚析构函数,友元和运算符重载,讲的东西感觉有点多,但是题目做的也多,即使还是写不出来也是有印象了。今天老师给我们留的作业内容是课上都讲过的,也算是有例题,原本以为把例题改一下就可以了,没想到没用,错误百出,试着改错也改不出来,明天看看老师是咋改的吧。

//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 << " 补贴:" << 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;
}
//虚析构函数
#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;
    }
    //若存在继承派生的情况,则一般将基类的析构函数定义为虚函数,可以通过基类的指针来完全释放派生类的对象
    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;
    }
    //若基类的析构函数为虚函数,则派生类中的析构函数默认为虚函数
    ~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;
}
//重载加减运算符
#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;   
}
//从载输出输入运算符
#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;
}
//重载赋值运算符
#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";
    }
    //拷贝构造函数
    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;
}
//重载运算符
#include <iostream>
using namespace std;

class Complex
{
public:
    //explicit:防止隐式类型转换
    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[])
{
    //隐式类型转换
    fun(23);
    return 0;
}
//重载运算符
#include <iostream>
using namespace std;

class Complex
{
public:
    //explicit:防止隐式类型转换
    //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)
{
    //隐式类型转换
    //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;
    }
    //将一个函数定义为类的友元,则可以在该函数中通过对象直接访问类的私有成员
    //友元破坏了类的封装性,能不用尽量不用
    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);//函数对象
    return 0;
}
//纯虚函数
#include <iostream>
using namespace std;

class Shape
{
public:
    Shape(){}
    //纯虚函数
    //包含纯虚函数的类称之为抽象类
    //抽象类不能定义对象
    //抽象类:为了实现多态,统一接口

    //派生类中,如果没有对纯虚函数进行定义,则派生类仍然为抽象类,不能定义对象
    
    //若派生类中对纯虚函数进行定义了,则可以生成对象
    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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 202,607评论 5 476
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,047评论 2 379
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 149,496评论 0 335
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,405评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,400评论 5 364
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,479评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,883评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,535评论 0 256
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,743评论 1 295
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,544评论 2 319
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,612评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,309评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,881评论 3 306
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,891评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,136评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,783评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,316评论 2 342

推荐阅读更多精彩内容