【C++重载操作符与转换】定义基类和派生类

目录

一、基类与派生类的基本概念

1.1 基类的定义

1.2 派生类的定义

二、继承方式与访问权限

2.1 公有继承(public)

2.2 私有继承(private)

2.3 保护继承(protected)

三、重载操作符在基类和派生类中的应用

3.1 运算符重载的基本概念

3.2 基类和派生类中运算符重载的特点

四、类型转换:类与内置类型的桥梁

4.1 隐式与显式类型转换

4.2 用户定义的类型转换

五、类型转换在基类和派生类中的应用

5.1 向上转型(Upcasting)

5.2 向下转型(Downcasting)

5.3 转换构造函数与类型转换运算符

六、总结


在C++面向对象编程中,面向对象编程(OOP)是核心范式之一,而基类和派生类作为 OOP 的重要组成部分,构建了类之间的层次结构。通过继承机制,派生类可以复用基类的属性和行为,并在此基础上进行扩展和修改。同时,重载操作符与类型转换在基类和派生类的场景下,能够进一步增强代码的灵活性和可读性。

一、基类与派生类的基本概念

1.1 基类的定义

基类是派生类的基础,它包含了一组通用的属性和方法,这些属性和方法可以被派生类继承。在 C++ 中,基类的定义与普通类类似,例如,我们定义一个表示 “形状” 的基类 Shape

#include <iostream>

class Shape {
protected:
    std::string name;
public:
    Shape(const std::string& s) : name(s) {}
    virtual void draw() const {
        std::cout << "Drawing a shape: " << name << std::endl;
    }
};

在这个 Shape 类中,name 用于表示形状的名称,通过 protected 访问修饰符,使得派生类可以访问该成员变量。draw 方法是一个虚函数,为后续在派生类中实现多态性奠定了基础。

1.2 派生类的定义

派生类通过继承基类,获取基类的属性和方法,并可以添加自己特有的成员,或者重写基类的成员函数。派生类的定义语法如下: 

class DerivedClass : access - specifier BaseClass {
    // 派生类的成员声明
};

其中,access - specifier 可以是 publicprivate 或 protected,用于指定基类成员在派生类中的访问权限。

例如,定义一个 Rectangle 类作为 Shape 类的派生类:

class Rectangle : public Shape {
private:
    double width;
    double height;
public:
    Rectangle(const std::string& s, double w, double h) : Shape(s), width(w), height(h) {}
    void draw() const override {
        std::cout << "Drawing a rectangle: " << name << ", width: " << width << ", height: " << height << std::endl;
    }
    double area() const {
        return width * height;
    }
};

在 Rectangle 类中,除了继承 Shape 类的 name 和 draw 方法外,还添加了 width 和 height 两个私有成员变量,以及用于计算面积的 area 方法,并通过重写 draw 方法,实现了特定于矩形的绘制逻辑。

二、继承方式与访问权限

2.1 公有继承(public

当派生类使用 public 继承基类时,基类的 public 成员在派生类中仍然是 public 的,基类的 protected 成员在派生类中仍然是 protected 的。意味着在类外部,可以通过派生类对象访问基类的 public 成员,而派生类的成员函数和友元函数可以访问基类的 protected 成员。

class Circle : public Shape {
private:
    double radius;
public:
    Circle(const std::string& s, double r) : Shape(s), radius(r) {}
    void draw() const override {
        std::cout << "Drawing a circle: " << name << ", radius: " << radius << std::endl;
    }
    double area() const {
        return 3.14 * radius * radius;
    }
};

int main() {
    Circle circle("MyCircle", 5.0);
    circle.draw();  // 可以直接访问基类的 public 方法
    return 0;
}

 

2.2 私有继承(private

在私有继承中,基类的 public 和 protected 成员在派生类中都变为 private 成员。使得派生类的对象无法在类外部访问基类的任何成员,只有派生类的成员函数和友元函数可以访问这些成员。私有继承通常用于实现细节的隐藏,不希望基类的接口在派生类外部暴露。 

class PrivateRectangle : private Shape {
private:
    double width;
    double height;
public:
    PrivateRectangle(const std::string& s, double w, double h) : Shape(s), width(w), height(h) {}
    void draw() const {
        Shape::draw();  // 在派生类内部访问基类方法
        std::cout << "This is a privately inherited rectangle." << std::endl;
    }
};

int main() {
    PrivateRectangle privRect("PrivateRect", 4.0, 3.0);
    // privRect.draw();  // 编译错误,无法在类外部访问
    return 0;
}

2.3 保护继承(protected

保护继承时,基类的 public 和 protected 成员在派生类中都变为 protected 成员。意味着派生类的对象不能在类外部访问这些成员,但派生类的派生类可以访问这些 protected 成员。保护继承常用于基类的部分接口只希望在继承层次结构中使用的场景。

class ProtectedCircle : protected Shape {
private:
    double radius;
public:
    ProtectedCircle(const std::string& s, double r) : Shape(s), radius(r) {}
    void draw() const {
        Shape::draw();
        std::cout << "This is a protectedly inherited circle." << std::endl;
    }
};

class SubProtectedCircle : public ProtectedCircle {
public:
    SubProtectedCircle(const std::string& s, double r) : ProtectedCircle(s, r) {}
    void innerDraw() const {
        draw();  // 可以访问基类的 protected 方法
    }
};

三、重载操作符在基类和派生类中的应用

3.1 运算符重载的基本概念

运算符重载允许为自定义类型重新定义运算符的行为,使得自定义类型的对象可以像内置类型一样使用运算符。例如,我们可以重载 + 运算符,实现两个矩形的合并操作。 

class Rectangle {
private:
    double width;
    double height;
public:
    Rectangle(double w = 0, double h = 0) : width(w), height(h) {}
    Rectangle operator+(const Rectangle& other) const {
        return Rectangle(width + other.width, height + other.height);
    }
    void display() const {
        std::cout << "Width: " << width << ", Height: " << height << std::endl;
    }
};

int main() {
    Rectangle rect1(2.0, 3.0);
    Rectangle rect2(4.0, 5.0);
    Rectangle result = rect1 + rect2;
    result.display();
    return 0;
}

 

3.2 基类和派生类中运算符重载的特点

在基类和派生类的层次结构中,运算符重载需要考虑继承关系和多态性。例如,当我们在基类中定义了一个运算符重载函数,派生类可以重写该函数以实现特定的行为。

#include <iostream>

class Shape {
public:
    // 添加默认构造函数
    Shape() = default;
    virtual Shape* operator+(const Shape& other) const {
        std::cout << "Shape addition operation" << std::endl;
        return new Shape();
    }
    // 虚析构函数,防止内存泄漏
    virtual ~Shape() = default;
};

class Rectangle : public Shape {
private:
    double width;
    double height;
public:
    Rectangle(double w = 0, double h = 0) : width(w), height(h) {}
    Rectangle* operator+(const Shape& other) const override {
        if (const Rectangle* rect = dynamic_cast<const Rectangle*>(&other)) {
            return new Rectangle(width + rect->width, height + rect->height);
        }
        std::cout << "Unsupported addition operation" << std::endl;
        return new Rectangle();
    }
    void display() const {
        std::cout << "Width: " << width << ", Height: " << height << std::endl;
    }
};

int main() {
    Rectangle rect1(2.0, 3.0);
    Rectangle rect2(4.0, 5.0);
    Rectangle* result = rect1 + rect2;
    result->display();
    // 手动释放内存
    delete result;
    return 0;
}    

 

四、类型转换:类与内置类型的桥梁

4.1 隐式与显式类型转换

C++提供四种显式类型转换操作符:

  • static_cast:编译时检查,用于明确类型转换(如intdouble)。
  • dynamic_cast:运行时检查,用于多态类型的向下转型。
  • const_cast:移除constvolatile属性(需谨慎使用)。
  • reinterpret_cast:底层二进制转换(如指针与整数互转,风险高)。

4.2 用户定义的类型转换

通过转换构造函数类型转换运算符实现类与内置类型的转换。

(1)转换构造函数

将内置类型转换为类类型:

#include <iostream>

class SmallInt {
private:
    int val;
public:
    SmallInt(int i) : val(i) {} // 转换构造函数
    void print() const {
        std::cout << val << std::endl;
    }
};

int main() {
    SmallInt si = 42; // 隐式调用转换构造函数
    si.print(); // 输出: 42
    return 0;
}    

 

(2)类型转换运算符

将类类型转换为内置类型: 

#include <iostream>

class SmallInt {
private:
    int val;
public:
    SmallInt(int i) : val(i) {}
    operator int() const { // 类型转换运算符
        return val;
    }
};

int main() {
    SmallInt si(100);
    int i = si; // 隐式调用类型转换运算符
    std::cout << i << std::endl; // 输出: 100
    return 0;
}    

 

(3)避免隐式转换的歧义

使用explicit关键字禁止隐式转换: 

#include <iostream>

class SmallInt {
private:
    int val;
public:
    explicit SmallInt(int i) : val(i) {} // 禁止隐式转换
    operator int() const { return val; }
};

int main() {
    SmallInt si(100);
    // int i = si; // 错误:禁止隐式转换
    int i = static_cast<int>(si); // 必须显式转换
    std::cout << i << std::endl; // 输出: 100
    return 0;
}    

 

五、类型转换在基类和派生类中的应用

5.1 向上转型(Upcasting)

向上转型是将派生类对象转换为基类对象或基类指针 / 引用。这种转换是安全的,因为派生类对象包含了基类对象的所有成员。 

#include <iostream>

class Animal {
public:
    virtual ~Animal() = default;
    virtual void speak() const {
        std::cout << "Animal sound" << std::endl;
    }
};

class Dog : public Animal {
public:
    void speak() const override {
        std::cout << "Woof!" << std::endl;
    }
};

int main() {
    Dog d;
    Animal* a = &d; // 向上转型
    a->speak(); // 输出: Woof! (多态性)
    return 0;
}    

 

5.2 向下转型(Downcasting)

向下转型是将基类指针 / 引用转换为派生类指针 / 引用。由于基类对象可能不是实际的派生类对象,因此需要使用 dynamic_cast 进行安全的向下转型。

int main() {
    Animal* a = new Dog;
    // Dog* d = a; // 错误:不安全
    Dog* d = dynamic_cast<Dog*>(a); // 安全:运行时检查
    if (d) {
        d->speak(); // 输出: Woof!
    } else {
        cout << "Invalid cast!" << endl;
    }
    delete a;
    return 0;
}

 

5.3 转换构造函数与类型转换运算符

在基类和派生类中,也可以使用转换构造函数和类型转换运算符实现类型转换。例如,我们可以定义一个从 Rectangle 到 Shape 的类型转换运算符。 

#include <iostream>
#include <string>

// 定义 Shape 类
class Shape {
private:
    std::string name;
public:
    Shape(const std::string& s) : name(s) {}
    void draw() const {
        std::cout << "Drawing a shape: " << name << std::endl;
    }
};

class Rectangle {
private:
    double width;
    double height;
public:
    Rectangle(double w = 0, double h = 0) : width(w), height(h) {}
    operator Shape() const {
        return Shape("ConvertedShape");
    }
};

int main() {
    Rectangle rect(2.0, 3.0);
    Shape shape = rect;
    shape.draw();
    return 0;
}    

 

六、总结

C++ 中的基类和派生类通过继承机制构建了强大的类层次结构,结合重载操作符与类型转换,使得代码更加灵活和高效。继承方式决定了基类成员在派生类中的访问权限,运算符重载为自定义类型提供了更直观的操作方式,而类型转换则在基类和派生类之间架起了桥梁。在实际编程中,合理运用这些特性,可以提高代码的复用性、可维护性和可扩展性,更好地实现复杂的面向对象编程需求。


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

byte轻骑兵

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值