IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 移动开发 -> 11 C++面向对象继承与操作符重载 -> 正文阅读

[移动开发]11 C++面向对象继承与操作符重载

Android Ndk 学习笔记(目录)

1 运算符重载


/**
 * 1 定义外 外部的重载
 * @param pos1
 * @param pos2
 * @return
 */
Position operator + (Position pos1 ,Position pos2){

   int x = pos1.getX() + pos2.getX();
    int y = pos1.getY() + pos2.getY();

   return Position(x,y);
}


class Position{
private:
    int x,y ;
public:
    void setX(int x){
        this->x = x ;
    }
    void setY(int Y){
        this->y = y ;
    }
    int getX(){
        return this->x;
    }
    int getY(){
        return this->y;
    }

public:
	// 无参改造函数
	Position(){};

    Position(int x,int y) :x(x),y(y){};
//    //2 内部定义 ,
//    Position operator + (Position pos){
//        int x = this->getX() + pos.getX();
//        int y = this->getY() + pos.getY();
//        return Position(x,y);
//    }
    //3  内部定义 常量
    Position operator + (const Position & pos){
        int x = this->getX() + pos.x;
        int y = this->getY() + pos.y;
        return Position(x,y);
    }

    //2  对象++ 运算符 重载
    void operator ++() { //  ++对象
        this->x = this->x + 1;
        this->y = this->y + 1;
    }
    void operator ++ (int) { // 对象++
        this->x = this->x + 1;
        this->y = this->y + 1;
    }

    //3  ostream 输出
    friend void operator << (ostream & _START, Position pos) {
        // 输出换行:<< endl;
        _START << "  " << pos.x << " ! " << pos.y << "  " << endl;
    }
    //4  istream 输入 系统的
    friend istream & operator >> (istream & _START, Position & pos) {
        _START >> pos.x >> pos.y;
        return _START;
    }
};


void function(){

    Position pos1(100,200) ;
    Position pos2(300,400) ;

    //正常情况下 + 无法使用 ;
    // 如果想使用 则需要重载运算符
    Position pos3 = pos1 + pos2 ;

    cout << pos3.getX() << "," << pos3.getY() << endl ;

	Position result(1, 2);
    result++;
    ++result;
    
    cout << result; // 单个的

    Position res;
    cin >> res; // >> 是我们自己重载的哦
    cout << "你输入的是:" << res.getX() << endl;
    cout << "你输入的是:" << res.getY() << endl;

}
//5  C++ 继承

class Person{

public:
    char * name ;
    int age ;
    Person(char * name ,int age):name(name),age(age){
    }
    void show(){
    }
};

/*
 * 继承父类 默认 是 private 继承
 * private 继承 内部可以使用父类的成员变量
 * 外部不可以使用 ,
 * 可以修改为 public 继承
 * 外部依然可以使用 父类的成员变量
 */
class Worker : public Person{
public:
    char * type ; // 工种
    // 继承时 需要 继承父类的构造方法  也可以用逗号继续为自己的值赋值
    Worker(char * name , int age, char * type) : Person(name,age) , type(type) {}
    void sayName(){
        // 无论 那种继承 ,内部都可以使用
        cout << name <<endl;
    }
};

void functino02(){
    Worker worker("张三",23,"钳工") ;
//class Worker : Person{ 或 class Worker : private Person{
//    cout << worker.name <<endl;  // 当继承是 private 时 该方法无法访问
//class Worker : public Person{
    cout << worker.name <<endl;  // 当继承是 public 时 该方法可以访问
}

// C++ 是 多继承
class BaseClass1{
public:
    int  number = 0;
    void show(){
    }
};

class BaseClass2{
public:
    int  number = 0;
    void show(){
    }
};

class ZiClass : public BaseClass1,public BaseClass2{
public:
    int  number = 0;
    void show(){
    }
};

void functino03(){
    ZiClass ziClass ;
    
    ziClass.show();// 子类如果有 show方法 则直接调用  子类没有 show方法 则调用父类的
    ziClass.BaseClass1::show(); //多个父类 冲突时 可以通过 class:: 来区分
    ziClass.BaseClass2::show();
    
    cout << ziClass.number  << endl;// 子类如果有 number 则直接调用  子类没有 number 则调用父类的
    cout << ziClass.BaseClass1::number  << endl;  //多个父类 冲突时 可以通过 class:: 来区分
    cout << ziClass.BaseClass2::number  << endl;
}

// 一级父类
class Object{
public:
    int number;
    void show() {
        cout << "Object show run..." << endl;
    }
};

// virtual 通过 virtual 的修饰 ,所有的 二级父类和一级父类共享 成员变量 与 方法 ,子类使用时 可以直接调用
// 二级父类1
class BaseActivity1 : virtual public Object {
};

// 二级父类2
class BaseActivity2 : virtual public Object {
};

// 子类
class Zi : public BaseActivity1, public BaseActivity2 {

};

void functino04(){

    // virtual 通过 virtual 的修饰 ,所有的 二级父类和一级父类共享 成员变量 与 方法 ,
    //子类使用时 可以直接调用

    Zi zi ;
    Object ob1 ;
    BaseActivity1 base1 ;
    BaseActivity2 base2 ;

    zi.number  = 100 ;
    ob1.number  = 200 ;
    base1.number  = 300 ;
    base2.number  = 400 ;

    cout << zi.number  << endl;
    cout << ob1.number  << endl;
    cout << base1.number  << endl;
    cout << base2.number  << endl;

}
E:\C\Project\C++\cmake-build-debug\C__.exe
100
200
300
400

Process finished with exit code 0
  移动开发 最新文章
Vue3装载axios和element-ui
android adb cmd
【xcode】Xcode常用快捷键与技巧
Android开发中的线程池使用
Java 和 Android 的 Base64
Android 测试文字编码格式
微信小程序支付
安卓权限记录
知乎之自动养号
【Android Jetpack】DataStore
上一篇文章      下一篇文章      查看所有文章
加:2021-07-29 11:46:00  更:2021-07-29 11:48:32 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/2 13:12:34-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码