美文网首页c/c++
c++基础-操作符的操作、虚函数、虚继承和模板函数

c++基础-操作符的操作、虚函数、虚继承和模板函数

作者: Peakmain | 来源:发表于2019-02-23 16:23 被阅读1次

操作符的重载

  • 首先写个基本的类
class Vector{

public:
    Vector(int x,int y){
        this->x = x;
        this->y = y;
    }
private:
    int x;
    int y;
public:
    int setX(int x){
        this->x = x;
    }
    int setY(int yx){
        this->y = y;
    }
    int getX(){
        return this->x;
    }
    int getY(){
        return this->y ;
    }
};
  • 1.重载运算符+:这个是定义在外面,一般是定义在里面,因为外面是不可以加上const关键字
Vector operator +(Vector vector1, Vector vector2){
    int x = vector1.getX() + vector2.getX();
    int y = vector1.getY() + vector2.getY();
    Vector vector(x, y);
    return vector;
}
  • 2.重载运算符-
    //引用的目的防止重复创建对象
    //const关键字变量,防止修改其变量的值
    Vector operator -(const Vector &vector){
        int x = this->getX() - vector.x;
        int y = this->getY() - vector.y;
        Vector res(x, y);
        return res;
    }
  • 3.自增减运算符
void operator ++(){//前++
        this->x = this->x++;
        this->y = this->y++;
    }
    //区分前++和后++
    void operator ++(int){//后++
        this->x = this->x++;
        this->y = this->y++;
    }
  • 4.输出运算符
friend ostream & operator <<(ostream &ostream,const Vector &vector){
        ostream << vector.x << "," << vector.y << endl;
        return ostream;
    }
  • 5.条件运算符
    bool operator ==(const Vector &vector){
        return (this->x == vector.x) && (this->y == vector.y);
    }
  • 6.[]运算符:重写一个类
class Array{
public:
    Array(int size){
        this->size = size;
        this->array = (int*)malloc(sizeof(int)*size);
    }
    //析构函数
    ~Array(){
        if (this->array){
            free(this->array);
            this->array = NULL;
        }
    };
    //深拷贝
    Array(const Array&array){
        this->size = size;
        this->array = (int*)malloc(sizeof(int)*array.size);
        //值也要被赋值
        for (int i = 0; i < array.size; i++){
            this->array[i] = array.array[i];
        }
    }
private:
    int size;
    int*array;
public:
    void set(int index,int value){
        array[index] = value;
    }
    int get(int index){
        return array[index];
    }
    int getSize(){
        return this->size;
    }
    //操作符[]
    int operator[](int index){
        return this->array[index];
    }
};
void printArray(Array array){
    for (int i = 0; i < array.getSize(); i++){
        cout << array[i]<<endl;
    }
}
void main(){
    Array *array = new Array(5);
    array->set(0, 0);
    array->set(1, 1);
    array->set(2, 2);
    array->set(3, 7);
    array->set(4,9);
    printArray(*array);
    delete(array);
    getchar();
}

类的继承

class Person
{
protected:
    char* name;
    int age;
public:
    Person(char* name, int age){
        this->name = name;
        this->age = age;
    }
};

class Course
{
private:// java String
    string name;
public:
    Course(string name){
        this->name = name;
    }

public:
    string _name(){
        return this->name;
    }
};

class Student : public Person
{
private:
    // char* courseName;
    Course course;
public:
    Student(char* name, int age) : Person(name, age), course("数学"){ // 初始化父类的属性
        // this->courseName = courseName;
    }
    void print(){
        cout << name << "," << age << "," << course._name().c_str() << endl;
    }
};

多继承

class Person{
private :
    char*name;
public:
    Person(char*name){
        this->name = name;
    }
    char* _name(){
        return this->name;
    }
};
class Child
{
    int age;
public:
    Child(int age){
        this->age = age;
    }

    int _age(){
        return this->age;
    }
};
class Student:public Person,public Child{//多继承
public:
    Student(char*name,int age):Person(name),Child(age){
    
    }
};

虚继承(解决二义性):virtual

比如以下代码,如果不使用virtual,调用C的时候就报错,因为继承过来的相同属性或者函数不明确

class A{
public:
    char* name;
};

class B : virtual public A{ // 确保继承过来的相同属性或者函数,只存在一份拷贝
};

class C :virtual public A{
};

多态

class Activity{
public :
    virtual void onCreate(){
        cout << "Activity中的onCreate" << endl;
    }
};
class MainActivity :public Activity{
public:
    void onCreate(){
        cout << "MainActivity中的onCreate" << endl;
    }
};
class WelcomActivity :public Activity{
public:
    void onCreate(){
        cout << "WelcomActivity中的onCreate" << endl;
    }
};
void startActivity(Activity* activity){
    activity->onCreate();
}
void main(){
    Activity *activity = new MainActivity();//父类=new 子类的对象
    startActivity(activity);
    getchar();
}

父类方法不设置virtual 的话,上面main实际调用的是父类的activity中的onCreate方法,如果添加了则会调用相应子类的onCreate的方法

抽象类:对方法添加virtual,而对于子类如果不实现父类的虚函数,那么子类也会变成抽象类,抽象类是不允许被实例化的

class BaseActivity{
public:
    void onCreate(){//普通函数
        initView();
        initData();
    }
    //子类必须实现
    virtual void initView() = 0;//类似java中的抽象方法
    virtual void initData() = 0;

};
//如果不实现父类的虚函数,那么MainActivivity也会变成抽象类,抽象类不允许被实例化
class MainActivity :public BaseActivity{
public:
    void initView(){
        cout << "initView" << endl;
    };
    void initData(){
        cout << "initData" << endl;
    };

};

接口:所有的函数都是虚函数,那么就可以认为是接口

class clickListener{//所有的函数都是虚函数,那么就可以认为是接口
public:
    virtual void click()=0;
};
class ImageClickListener :public clickListener{
public:
    void click(){
        cout << "图片被点击了" << endl;
    }
};

模板函数相当于java中的泛型

template<typename T> 
 T add(T number1, T number2){
    return number1 + number2;
}

相关文章

网友评论

    本文标题:c++基础-操作符的操作、虚函数、虚继承和模板函数

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