zoukankan      html  css  js  c++  java
  • C++知识点案例 笔记-4

    1.纯虚函数
    2.抽象类
    3.内部类
    4.运算符重载
    5.类的函数重载
    6.友元的函数重载

    1.纯虚函数

    ==纯虚函数==
    
    //有时基类中无法给出函数的具体体现,定义纯虚函数可以为派生函数保留一个函数接口
    #include <iostream>
    #include <cstring>
    using namespace std;
    class Animal
    {
    public:
        Animal(char *name);
        void print_name();
        virtual void print_color() = 0;
        virtual ~Animal();
    private:
        char *m_Name;
    
    };
    Animal::Animal(char *name)
    {
    
        m_Name = new char[strlen(name)+1];
        strcpy(m_Name,name);
    }
    Animal::~Animal()
    {
       cout<<"Animal diluting !!!"<<endl;
       if(m_Name)
       {
           delete[]m_Name;
       }
    }
    void Animal::print_name()
    {
        cout<<"name: "<<m_Name<<endl;
    }
    class Cat:public Animal
    {
    public:
        Cat(char *name,char *color);
        virtual void print_color();
        virtual ~Cat();
    private:
        char *m_Color;
    };
    Cat::Cat(char *name,char *color):Animal(name)
    {
        m_Color = new char[strlen(color)+1];
        strcpy(m_Color,color);
    }
    void Cat:: print_color()
    {
        cout<<"Cat color :"<<m_Color<<endl;
    }
    Cat::~Cat()
    {
        cout<<"Cat diluting !!!"<<endl;
        if(m_Color)
        {
            delete[]m_Color;
        }
    }
    int main()
    {
        Animal *p;
        p = new Cat("catmimi","yellow");
        p->print_name();
        p->print_color();
        delete p;
        return 0;
    
    }
    

    2.抽象类

    ==抽象类==
    
    //含有纯虚函数的基类,若派生类继承后仍有纯虚函数也为抽象类,直至无纯虚函数为止
    #include<iostream>
    #include<cstring>
    using namespace std;
    const double IP = 3.1415926;                                                                             
    class Shapes
    {
    public:
        Shapes(int x,int y = 0);
        virtual ~Shapes();
        virtual void disp() = 0;
    protected:
        int m_X,m_Y;
    };
    Shapes::Shapes(int x,int y)
    {
        m_X = x;
        m_Y = y;
    }
    Shapes::~Shapes()
    {
        cout<<"Shapes diluting !!!"<<endl;
    }
    class Squre:public Shapes
    {
    public:
        Squre(int squre_x,int squre_y);
        virtual void disp();
        virtual ~Squre();
    
    };
    Squre::Squre(int squre_x,int squre_y):Shapes(squre_x,squre_y)
    {
    
    }
    void Squre::disp()
    {
        cout<<"The squre area:"<<m_X*m_Y<<endl;
    }
    Squre::~Squre()
    {
        cout<<"Squre diluting !!!"<<endl;
    }
    class Circle:public Shapes
    {
    public:
        Circle(int R);
        virtual void disp();
        virtual ~Circle();
    };
    Circle::Circle(int R):Shapes(R)
    {
    
    }
    void Circle::disp()
    {
        cout<<"The circle area : "<<IP * m_X * m_X<<endl;
    }
    Circle::~Circle()
    {
        cout<<"Circle diluting !!!"<<endl;
    }
    int main()
    {
        Shapes *p[2];
        p[0] = new Squre(5,10);
        p[0] ->disp();
        p[1] = new Circle(2);
        p[1] ->disp();
        for(int i;i<2;i++)
        {
            delete p[i];
        }
        return 0;
    }
    

    3.内部类

    ==内部类==
    
    //在(基)类内部定义的类叫做内部类,(基)类相当于该类的外部类
    #include <iostream>
    using namespace std;
    class Outer
    {
    public:
        class Inner{
        private:
            int Inner_n;
        public:
            void set_outer_n(Outer &ref_outer){ref_outer.outer_n = 100;}
            void set_inner_n(){Inner_n = 666;}
            void show(Outer &ref_outer)
            {
                ref_outer.show();
                cout<<"Inner: "<<Inner_n<<endl;
                }
        };
       void show(){cout<<"Out: "<<outer_n<<endl;}
    private:
        int outer_n;
    };
    int main()
    {
        Outer outer_ojb;
        Outer::Inner inner_ojb;
    
        inner_ojb.set_outer_n(outer_ojb);
        inner_ojb.set_inner_n();
        inner_ojb.show(outer_ojb);
        return 0;
    }
    
    

    4.运算符重载

    ==运算符重载==
    //如对象的相加,把特殊符号定义为函数易于表达
    #include<iostream>
    using namespace std;
    class Q
    {
    private:
        int x,y;
    public:
        Q(int x1=0,int y1=0):x(x1),y(y1){}
        void show() const;
        Q operator + (const Q&a)const ;###########重载+运算符
        Q operator - (const Q&a)const ;
    };
    void Q::show() const
    {
        cout<<"(x,y)="<<"("<<x<<","<<y<<")"<<endl;
    }
    Q Q::operator + (const Q&a)const
    {
        return Q(x + a.x,y + a.y);
    }
    Q Q::operator - (const Q&a)const
    {
        return Q(x - a.x,y - a.y);
    }
    int main()
    {
        Q a1(7,2);
        Q a2(5,1);
        Q a;
        Q b;
        a = a1 + a2;
        b = a1 - a2;
        cout<<"a1:";
        a1.show();
        cout<<"a2:";
        a2.show();
        cout<<"a:";
        a.show();
        cout<<"b:";
        b.show();
        cout<<"a1+a2:";
        a.show();
        cout<<"a1-a2:";
        b.show();
        return 0;
    
    }
    

    5.类的函数重载

    ==类的函数重载==
    
    
    #include<iostream>
    using namespace std;
    class V
    {
    private:
        int a,b;
    public:
        V(int a1 = 0,int b1 = 0):a(a1),b(b1){}
        void show() const;
        V operator ++();
        V operator ++(int);
    
    
    };
    void V::show() const
    {
        cout<<"(a,b)="<<"("<<a<<","<<b<<")"<<endl;
    }
    V V::operator ++()  //++前置函数
    {
        ++a;
        ++b;
        return *this;
    }
    
    V V::operator ++(int)  //"k=i++":后缀++表示先k=i之后i自增
    {
        V a = *this;
        ++(*this);          //调用前置函数
        return a;
    }
    int main()
    {
        V m(1,1);
        V n(0,0);
        (m++).show();
        (++n).show();
        return 0;
    }
    
    

    6.友元的函数重载

    ==友元的函数重载==
    
    //定义时不用指明函数位置,方便私有数据成员的调用
    #include<iostream>
    using namespace std;
    class G
    {
    private:
        int m,n;
    public:
        G(int m1 = 0,int n1 = 0):m(m1),n(n1){}
        void show() const;
        friend G operator + (const G&a1,const G&a2);
        friend G operator - (const G&a1,const G&a2);
    
    };
    void G::show() const
    {
        cout<<"(m,n)="<<"("<<m<<","<<n<<")"<<endl;
    }
    G operator + (const G&a1,const G&a2)
    {
        return G(a1.m +a2.m , a1.n +a2.n);
    }
    G operator - (const G&a1,const G&a2)
    {
        return G(a1.m - a2.m, a1.n - a2.n);
    }
    int main()
    {
        G j(2,2);
        G k(1,1);
        G a;
    
        j.show();
        k.show();
        a.show();
    
        a = j + k;
        a.show();
        a = j - k;
        a.show();
    
        return 0;
    
    }
    
    
    
    
  • 相关阅读:
    Qt 打开UI是提示Runtime Error! 。。。 然后奔溃
    Qt exe和动态 库获取运行所需库
    区分EXE或者动态库dll是32位或者64位方法
    QFile 读2进制文件
    MFC 动态库编译错误
    Qt 编译错误
    Qt QNetworkProxy类帮助翻译
    Qt QHttpMultiPart类帮助翻译
    Qt QNetworkCookie帮助翻译
    Qt QHttpPart翻译
  • 原文地址:https://www.cnblogs.com/hugboy/p/12906562.html
Copyright © 2011-2022 走看看