美文网首页
电商专业学习嵌入式软件开发第六十天

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

作者: 雨打梨花闭门寒 | 来源:发表于2017-04-12 00:22 被阅读0次
    • 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](https://img.haomeiwen.com/i4254001/5c7d4033960ac8de.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240

    相关文章

      网友评论

          本文标题:电商专业学习嵌入式软件开发第六十天

          本文链接:https://www.haomeiwen.com/subject/rilpattx.html