C++学习日记 day004

目录

1、类的继承

2、重载函数和重载运算符

3、数组


1、类的继承

访问权限:

访问     public protected  private
同一个类   yes         yes       yes
派生类    yes         yes        no
外部的类   yes      no             no
公有继承(public):当一个类派生自公有基类时,基类的公有成员也是派生类的公有成员,基类的保护成员也是派生类的保护成员,基类的私有成员不能直接被派生类访问,但是可以通过调用基类的公有和保护成员来访问。
保护继承(protected): 当一个类派生自保护基类时,基类的公有和保护成员将成为派生类的保护成员。
私有继承(private):当一个类派生自私有基类时,基类的公有和保护成员将成为派生类的私有成员。
# include<iostream>

using namespace std;
//Inherit
//access
/*
访问	    public	protected	private
同一个类	yes	     yes	    yes
派生类	yes	     yes	     no
外部的类	yes      no	         no
公有继承(public):当一个类派生自公有基类时,基类的公有成员也是派生类的公有成员,基类的保护成员也是派生类的保护成员,
基类的私有成员不能直接被派生类访问,但是可以通过调用基类的公有和保护成员来访问。

保护继承(protected): 当一个类派生自保护基类时,基类的公有和保护成员将成为派生类的保护成员。

私有继承(private):当一个类派生自私有基类时,基类的公有和保护成员将成为派生类的私有成员。
 */

// 除去基类的构造函数、析构函数、拷贝构造函数,重载运算符以及友元函数
// 子类继承所有的基类方法
class Animal {
public:
    int setWeight(double wei) {
        weight = wei;
        return weight;
    }

    Animal();

private:
    double weight;
protected:
    double height;
};

Animal::Animal() {
    cout << "The animal object was created." << endl;
}

class Pet {
public:
    Pet();  // 构造函数必须在public里面,不然声明对象时会报错


};

Pet::Pet() {
    cout << "Pet object was created." << endl;
}
// 一个类可以派生多个类,派生类(derived class)可以从基类(base class)继承数据和函数
//class Dog : public Animal {  // 其中public是访问修饰符access specifier
//
//};

// 多个派生类
class Dog : public Animal, public Pet {
public:
    double setHeight(double hei) {
        height = hei;
        return height;
    }
};

int main() {
    Dog littleDog;
    double weight = littleDog.setHeight(0.5);
    cout << "The dog' weight is " << weight << endl;
    return 0;
}

2、重载函数和重载运算符

重载函数主要是对相同命名的函数写入不一样的功能,例如python中对不同类型的数据求长度len功能。

重载运算符是对不同的运算符进行高等级的应用,例如对类创建的对象进行运算,例如python中列表相加就是用来这个功能。

//C++ 允许在同一作用域中的某个函数和运算符指定多个定义(多次声明),分别称为函数重载和运算符重载

#include <iostream>

using namespace std;

class printData {
public:
    // 重载函数(overload function)
    void print(int i) {
        cout << "int: " << i << endl;
    }

    void print(double f) {
        cout << "double: " << f << endl;
    }

    void print(char c[]) {
        cout << "string: " << c << endl;
    }
};

class Box {
public:
    Box operator+(const Box &b) {  // operator后面是重载运算符
        Box box;
        box.length = this->length + b.length;
        box.width = this->width + b.width;
        box.height = this->height + b.height;
        return box;
    }

private:
    double length = 1.1;
    double width = 1.2;
    double height = 1.3;
};

int main() {
    printData pd;

    // 输出整数
    pd.print(5);
    // 输出浮点数
    pd.print(500.263);
    // 输出字符串
    char c[] = "Hello C++";
    pd.print(c);

    Box box1;
    Box box2;
    Box box3;
    box3 = box1 + box2;

    return 0;
}

3、数组

#include <iostream>

using namespace std;

#include <iomanip>

using std::setw;  // 用于格式化输出

// 数组传参的三种方式
void modifiedArray(int *p);

void modifiedArray(int param[10]);

void modifiedArray(int param[]);

int *func() {
    static int arr[] = {1, 2, 3};  // 设置为静态的才能传出去,不然会报错
    return arr;
};

//数组的声明
int main() {
    double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0};
    double balance1[] = {1000.0, 2.0, 3.4, 7.0, 50.0};
    double a[4];
    a[4] = 4.1;
    cout << a[4] << endl;
    cout << sizeof(a) << setw(10) << sizeof(balance) << setw(10) << sizeof(balance1) << endl;
    int newArray[10];
    for (int i = 0; i < 10; i++) {
        newArray[i] = i;
    }
    for (int j = 0; j < 10; j++) {
        cout << setw(10) << newArray[j] << endl;
    }
    int *p;  //定义指针指向数组
    p = newArray;  // 指针不能指向二维数组

    cout << "The output of pointer:" << endl;
    for (int i = 0; i < 10; ++i) {

        cout << *(p + i) << endl;
    }
//    int multiDimArray[2][3];
    int multiDimArray[2][3] = {
            {1, 2, 3},
            {4, 5, 6}
    };
    int multiDimArray1[2][3] = {1, 2, 3, 4, 5, 6};  //这样的做法和上面的等价
    cout << multiDimArray[1][2] << endl;

    for (int i = 0; i < 2; ++i) {
        for (int j = 0; j < 3; ++j) {
            cout << multiDimArray[i][j];
        }
    }
    // 从函数中获取返回的数组
    int *t;
    t = func();
    cout << endl;
    for (int i = 0; i < 3; ++i) {
        cout << *(t + i) << endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_38901850/article/details/126396505