C++

作者: fo0Old | 来源:发表于2017-05-17 18:28 被阅读0次

    框架:

    #include<iostream>
    
    class super
    {
    public:
        int member;
    //构造器,前面不能加void
        super(int member);
        super(const super& another);
    //析构器,前面不能加void,基类最好定义为虚方法
        virtual ~super();
    //虚方法
        virtual void operation(void);
    //重载运算符
        super operator+(super &anthor);
        friend std::ostream& operator<<(std::ostream& os,super another);
    protected://这个类和它的子类
    private:  //只有这个类
    };
    
    super::super(int member)
    {
        this->member=member;
    }
    
    super::~super()
    {
    
    }
    
    void super::operation(void)
    {
        std::cout<<"A"<<std::endl;
    }
    
    //继承
    
    class sub:public super
    {
    public:
        int member;
        sub(int member);
        ~sub();
        void operation(void);
    protected:
    private:
    };
    
    sub::sub(int member):super(member){}
    
    sub::~sub(){}
    
    void sub::operation(void)
    {
        std::cout<<'B'<<std::endl;
    }
    
    //该类为抽象类,不能有实例
    class other
    {
    //友元
        friend class super;
    public:
    //抽象方法
        virtual void abstract(void)=0;
    protected:
    private:
    };
    
    //抽象方法
    
    class other2:public other
    {
    public:
        virtual void abstract(void);
    };
    
    void other2::abstract(void)
    {
        std::cout<<"abstract method"<<std::endl;
    }
    
    //重载运算符
    super super::operator+(super &anthor)
    {
        return super(member+anthor.member);
    }
    //重载<<运算符
    std::ostream& operator<<(std::ostream& os,super another)
    {
        os<<another.member;
    }
    //副本构造器
    super::super(const super& another)
    {
        this->member=another.member;
    }
    
    int main()
    {
    //虚方法
        super *sub_pointer=new sub(0);
        sub_pointer->operation();
    //重载运算符
        super A(1),B(2);
        std::cout<<A+B<<std::endl;
    //抽象方法
        other2 C;
        C.abstract();
    //delete:删除数组:delete[]
        delete sub_pointer;
        return 0;
    }
    

    构造函数:

    class super
    {
    public:
        int member1,member2;
        super(int member1,int member2);
    //隐式转换构造函数
        super(const int& member4):member1(member4)
        {
    
        }
    //复制构造函数
        super(const super& other)
        {
    
        }
    };
    //初始化列表
    super::super(int member1,int member3):
        member1(member1),member2(member3)
    {
    
    }
    一般声明为public
    因为创建对象一般是在外部进行的
    若声明为protected或private
    那就意味着在外部创建对象是错误的
    只能由类内部实例化(不通常的做法)
    一般来说:不应同时使用 构造函数的重载 和 带默认参数的构造函数(会产生歧义)
    带默认参数的构造函数只能在类内部定义
    explicit 前缀定义 构造函数防止 隐式转换,在类外部定义不能重复加explicit
    

    继承中的访问控制:

    public:继承会保留基类中成员(包括函数和变量等)的可见性不变
    protected:继承会将基类中的public可见性的成员修改成为protected可见性
    private:继承会将基类中的public和protected可见性的成员修改成为private可见性
    

    不允许重载的符号:

    .  //成员访问运算符
    .* //成员指针访问运算符
    :: //域运算符
    sizeof
    ?: //条件运算符
    

    成员指针与成员函数指针:

    #include<iostream>
    
    class super
    {
    public:
        int member;
        super(int member)
        {
            this->member=member;
        }
    //const限定改函数不能改变成员的值
        int operation(int member)const
        {
            return this->member;
        }
    };
    
    int main()
    {
    //成员指针
        int super::*pointer_member=&super::member;
    //成员函数指针
        int (super::*pointer_operation)(int member)const=&super::operation;
        super A(2);
        super *p=&A;
        std::cout<<p->*pointer_member<<std::endl;
        std::cout<<A.*pointer_member<<std::endl;
        std::cout<<(p->*pointer_operation)(3)<<std::endl;
        std::cout<<(A.*pointer_operation)(3)<<std::endl;
        return 0;
    }
    

    const的限定:

    class super1
    {
    public:
        int member1;
        super1(int member1):member1(member1){}
    //可修改该常对象的值
        mutable int member2;
    };
    //常对象(2种定义)
    //所有数据成员都不能被修改
    //只能调用常函数,不能调用非常函数
    //不能被非常函数访问,只能被常函数访问
    //只能被 指向常对象的指针 指向
    const super1 A(2);
    super1 const B(2);
    
    class super2
    {
    //常数据成员
    //只能通过构造函数的初始化列表进行初始化
    //常函数与非常函数都可以访问,但不能修改
    public:
    //const数据成员
    //既可以被常函数访问也可以被非常函数访问
        const int a;
        int operation(void)const;
        const int operation(void);
    };
    //常函数
    //可以访问常数据成员或非常数据成员
    //不能改变数据成员的值
    //不能调用非常函数
    int super2::operation(void)const
    {
    
    }
    //返回值为const
    const int super2::operation(void)
    {
    
    }
    
    super1 C(2);
    //指向对象的常指针
    //不能修改它的指向
    //必须在定义的时候被初始化
    super1* const p1=&C;
    
    //指向常对象的指针
    //不能通过它改变指向对象的值
    //可以改变指向
    const super1* p2;
    
    //对象的常引用
    const super1& D(2);
    

    类中静态函数与变量:

    #include<iostream>
    //无需对象即可调用
    class super
    {
    public:
        static int cont;
        static int getcont(void)
        {
            return cont;
        }
    };
    
    int super::cont=2;
    
    int main()
    {
        std::cout<<super::getcont()<<std::endl;
        return 0;
    }
    

    多继承:

    class super
    {
    public:
        int member;
        super(int member)
        {
            this->member=member;
        }
    protected:
    private:
    };
    
    class sub1:public super
    {
    public:
        int member1;
        sub1(int member,int member1):super(member)
        {
            this->member1=member1;
        }
    };
    
    class sub2:public super
    {
    public:
        int member2;
        sub2(int member,int member2):super(member)
        {
            this->member2=member2;
        }
    };
    
    class subsub:public sub1,public sub2
    {
    public:
        subsub(int member,int member1,int member2):
            sub1(member,member1),sub2(member,member2)
        {
    
        }
    };
    

    虚继承:

    class super
    {
    public:
        int member;
        super(int member)
        {
            this->member=member;
        }
    protected:
    private:
    };
    
    class sub1:virtual public super
    {
    public:
        int member1;
        sub1(int member,int member1):super(member)
        {
            this->member1=member1;
        }
    };
    
    class sub2:virtual public super
    {
    public:
        int member2;
        sub2(int member,int member2):super(member)
        {
            this->member2=member2;
        }
    };
    
    class subsub:public sub1,public sub2
    {
    public:
        subsub(int member,int member1,int member2):
            sub1(member,member1),sub2(member,member2),super(member)
        {
    
        }
    };
    

    虚函数:

    #include<iostream>
    
    //虚函数多态的条件:
    //类之间的继承符合赋值兼容
    //改写了同名虚函数
    //根据赋值兼容规则使用指针或引用
    //静态,内联,构造 函数不能是虚函数
    class super
    {
    public:
        virtual int operation(void)
        {
            return 1;
        }
    };
    
    class sub:public super
    {
    public:
    //不加virtual也是虚函数
        int operation(void)
        {
            return 2;
        }
    };
    
    void print(super *X,int(super::*Y)(void))
    {
        std::cout<<(X->*Y)()<<std::endl;
    }
    
    int main()
    {
        sub A;
        super *B=&A,&C=A;
        std::cout<<B->operation()<<std::endl;
        std::cout<<C.operation()<<std::endl;
        print(&A,&super::operation);
        return 0;
    }
    

    重载运算符:

    成员函数版:
    class super
    {
        int member1,member2;
    public:
        super(int member1=0,int member2=0):
            member1(member1),member2(member2) {}
        super operator+(super &another);
    //前置单目运算符重载:
        super operator++();
    //后置单目运算符重载:
        super operator++(int);
        super& operator+=(super& another);
    //重载<<和>>(只能用友元函数)
        friend std::ostream& operator<<(std::ostream& os,const super& other);
        friend std::istream& operator>>(std::istream& is,super& other);
    //重载强制转换运算符(不需指明返回值类型,默认和转换类型相同)
        operator int();
        void print(void)
        {
            std::cout<<member1<<","<<member2<<std::endl;
        }
    };
    
    super super::operator+(super &another)
    {
        return super(member1+another.member1,member2+another.member2);
    }
    
    super super::operator++()
    {
        return super(++member1,++member2);
    }
    
    super super::operator++(int)
    {
        return super(member1++,member2++);
    }
    
    super& super::operator+=(super& another)
    {
        member1+=another.member1;
        member2+=another.member2;
        return *this;
    }
    
    std::ostream& operator<<(std::ostream& os,const super& other)
    {
        os<<other.member1<<","<<other.member2<<std::endl;
        return os;
    }
    
    std::istream& operator>>(std::istream& is,super& other)
    {
        is>>other.member1>>other.member2;
        return is;
    }
    
    super::operator int()
    {
        return member1+member2;
    }
    
    友元函数版:
    class super
    {
        int member1,member2;
    public:
        super(int member1=0,int member2=0):
            member1(member1),member2(member2){}
        friend super operator+(const super& a,const super& b);
    //前置单目运算符重载:
        friend super operator++(super& a);
    //后置单目运算符重载:
        friend super operator++(super& a,int);
        void print(void)
        {
            std::cout<<member1<<","<<member2<<std::endl;
        }
    };
    
    super operator+(const super& a,const super& b)
    {
        return super(a.member1+b.member1,a.member2+b.member2);
    }
    
    super operator++(super& a)
    {
        return super(++a.member1,++a.member2);
    }
    
    super operator++(super& a,int)
    {
        return super(a.member1++,a.member2++);
    }
    

    模板:

    //函数模板
    template<typename T,typename U,typename V>
    void fun(T param)
    {
    
    }
    
    //类模板(带默认形参)
    template<class T,class U,int V=2,class W=int>
    class super
    {
    public:
        T member;
        super();
        ~super();
        void operation(T val);
    };
    
    template<class T,class U,int V,class W>
    super<T,U,V,W>::super()
    {
    
    }
    
    template<class T,class U,int V,class W>
    super<T,U,V,W>::~super()
    {
    
    }
    
    template<class T,class U,int V,class W>
    void super<T,U,V,W>::operation(T val)
    {
    
    }
    定义方式:
    super<int,int>A;
    super<int,int,5>B;
    super<int,int,5,int>C;
    

    内联模板:

    //函数模板
    template<typename T,typename U,typename V>
    inline void fun(T param)
    {
    
    }
    
    //类模板
    template<class T,class U,int V>
    class super
    {
    public:
        T member;
        super(){}
        ~super(){}
        void operation(T val)
        {
    
        }
    };
    模板的使用:
    super<int,double,5>X;
    

    命名空间:

    //不能用于 函数,结构体,类内部
    namespace A
    {
        int a;
    }
    
    namespace B
    {
        int a,b;
        namespace C
        {
            int c,d;
            namespace very_long_name_namespace
            {
                int e,f;
            }
        }
    }
    
    //可以没有名字
    namespace
    {
        
    }
    
    //简化名字
    namespace D=B::C::very_long_name_namespace;
    
    //可以不连续
    namespace A
    {
        int b;
    }
    
    using std::cout;
    
    using namespace std;
    

    强制转换:

    class super
    {virtual void operation(void){}};
    
    class sub:public super
    {};
    
    int main()
    {
        super *A,D;
        sub B,*C;
    //动态转换
    //必须是指针类型的转换
        A=dynamic_cast<super*>(&B);
    //子类转换为基类
    //基类必须有虚函数
        C=dynamic_cast<sub*>(&D);
    //静态转换
        double fo0Old=2.33333333;
        int fold=static_cast<int>(fo0Old);
        return 0;
    }
    

    迭代器:

    std::容器类<T>::iterator iter=容器对象.begin();
    

    重载,覆盖,隐藏

    重载:
    相同的范围(在同一个类中)函数名字相同;
    参数不同;
    virtual关键字可有可无
    覆盖:
    不同的范围(分别位于派生类与基类);
    函数名字相同
    参数相同
    基类函数必须有virtual关键字
    隐藏:
    如果派生类的函数与基类的函数同名,但是参数不同,不论有无virtual关键字,基类的函数将被隐藏
    如果派生类的函数与基类的函数同名,并且参数也相同,但是基类函数没有virtual 关键字,此时,基类的函数被隐藏
    

    相关文章

      网友评论

          本文标题:C++

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