代码区域



##### 1

#include< iostream>
#include< cassert>
using namespace std;
class Point {
public:
    Point() :x(0), y(0) {
        cout << "P构造函数" << endl;
    }
    Point(int x, int y) :x(x), y(y) {
        cout << "P构造函数" << endl;
    }
    ~Point(){
        cout << "P析构函数" << endl;
    }
    int getX() const { return x; }
    int getY() const { return y; }
    void move(int newx, int newy) {
        x = newx;
        y = newy;
    }
private:
    int x, y;
};

class Arryofpoint {
public:
    Arryofpoint(int size) :size(size) {
        points = new Point[size];//别忘记了new这个
        cout << "A构造函数" << endl;
    }
    ~Arryofpoint(){ 
        cout << "A析构函数" << endl;
        delete[]points;
    }
    Point& element(int index) {
        assert(index >= 0 && index < size);
        return points[index];
    }
private:
    Point* points;
    int size;
};

int main()
{
    Arryofpoint points(2);
    points.element(0).move(1, 2);
    points.element(1).move(11, 22);
    return 0;
}

##### 2

#include< iostream>
using namespace std;

class A
{
public:
    A(int x,int y):real(x),image(y){}
    void show()const { cout << real << image << endl; }//运算符重载
    A operator+(const A& c)const;
private:
    int real, image;
};

A A::operator+(const A& c)const {
    return **A(real + c.real, image + c.image);**

​    注意函数体内是什么

}

int main()
{
    A a(1,2);
    A b(2, 4);
    a = a + b;
    a.show();
    return 0;

}



##### 3

#include< iostream>
using namespace std;

class A
{
public:
    A(int x,int y):real(x),image(y){}
    void show()const { cout << real << image << endl; }//运算符重载
    friend ostream &operator << (ostream &out, const A& c);
    friend A operator +(const A& c1, const A& c2);
private:
    int real, image;
};

A operator +(const A& c1, const A& c2)
{
    return A(c1.real + c2.real, c1.image + c2.image);
}

ostream &operator << (ostream &out, const A& c)//<<重载&均不可以少
{
    out << "(" << c.real << "," << c.image << ")" << endl;
    return out;
}

int main()
{
    A a(1,2);
    A b(2, 4);
    a = a + b;
    a.show();
    cout << a;
    cout << "asd";
    return 0;
}



##### 4

#include< iostream>
using namespace std;
class A
{
public:
    void show() { cout << "1" << endl; }

​    // virtual void show() { cout << "1" << endl; }

};class B:public A
{
public:
    void show() { cout << "2" << endl; }

​    //virtual void  show() { cout << "2" << endl; }**虚函数不是内联的**

};class C: public B
{
public:
    void show() { cout << "3" << endl; }    

​    // virtual  void show() { cout << "3" << endl; }

};

void fun(A* ptr)//必须是老的基类
{
    ptr->show();
}

int main()
{
    A a;
    B b;
    C c;
    fun(&a);
    fun(&b);
    fun(&c);//这时都只访问A,并没有我们所计划的通过A,B,C访问
    return 0;
}

##### 5



#include< iostream>
using namespace std;
class A
{
public:
    virtual void show() = 0;虚函数不是内联的
};class B:public A
{
public:
    void show() { cout << "2" << endl; }
};class C: public B
{
public:
    void  show() { cout << "3" << endl; }

​    //void  show() override { cout << "3" << endl; }

};

void fun(A* ptr)//同样可以创建抽象类的指针,只是不能创建对象
{
    ptr->show();
}

int main()
{
    B b;
    C c;
    fun(&b);
    fun(&c);//这时都只访问A
    return 0;
}

##### 6

#include< iostream>
#include< cassert>
using namespace std;

template < typename T>
class Array
{
private:
    T* p;//指向首地址的指针
    int size;//数组大小
public:
    Array(int size = 50);//构造函数
    Array(const Array& a);//复制构造函数只读
    ~Array();//析构
    Array & operator = (const Array & rhs);//重载赋值运算符
    //注意函数写法
    Array& operator[] (int n);//检查【】是否越界
    operator T*();//*重载//**为什么这里前面不用Array**
};

template Array::Array(int sz)//构造函数
{
    if (sz > 0)
        size = sz;
    p = new T[size];//分配空间
}

template 
Array::~Array()//析构函数
{
    delete[]p;
}

template 
Array::Array(const Array& a)
{
    size = a.size;
    p = new T[size];//注意这里必须是深复制
    for (int x = 0; x < size; x++)
        p[x] = a.p[x];
}

template 
Array& Array ::operator =(const Array& rhs)
{    //注意函数写法
    //赋值,意思是个对象已经存在了一段时间后再进行
    //复制构造是这个对象刚刚创建时候就复制
    if (rhs != this)//表明复制的和所操作的不是同一个
    {
        if (size != rhs.size)
        {
            delete[] p;
            size = rhs.size;
            p = new T[size];
        }
        for (int i = 0; i < size; i++)
            p[i] = rhs.p[i];//深复制
    }
    return *this;//返回当前对象的引用
}

template
Array& Array::operator[](int n)//设置为引用或者const引用
{
    assert(n > 0 && n < size);//检查越界问题 头文件cassert
    return p[n];
}

template
Array::operator T*()//对对象直接转换为T*
{
    return p;
}

void read(int* p, int n)
{
    for (int i = 0; i < n; i++)
        cin >> p[i];
}

int main()
{
    Array a(10);
    //read(a, 10);这里需要*重载,直接将对象转换为指针类型
    read(a, 10);
    return 0;
}







1--5几种排列方式











































小问题区域

1.数组int a【5】;a=a+2;错误,这个首地址无法改变 只能a+2访问