You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

类的两个实例间数据比较方法咨询:以半径对比为例

Comparing Class Instance Properties (Radius) in C++

Hey there! Since you’re coming from a struct background and already have your class set up with instances that can call printInfo() and changeRadius(), let’s walk through how to compare their radius properties—this ties right into understanding class encapsulation, which is probably the key difference you’re noticing from structs.

First, Let’s Recall the Struct vs. Class Difference

With structs in C++, members are public by default, so you could just do something like if (struct1.radius == struct2.radius) directly. But classes default to private members, which means you can’t access radius from outside the class unless you explicitly allow it.

Here are three straightforward ways to compare your class instances’ radii:


This is the cleanest approach because it keeps your radius property encapsulated (private) while letting you safely retrieve its value.

First, update your class declaration in the header file to include a getter:

// YourClass.h
class YourClass {
private:
    double radius; // Private by default in classes
public:
    // Your existing methods
    void printInfo();
    void changeRadius(double newRadius);
    
    // Add this getter
    double getRadius() const {
        return radius;
    }
};

Now you can easily compare the radii of two instances:

#include "YourClass.h"
#include <iostream>

int main() {
    YourClass circle1;
    YourClass circle2;
    
    // Set radii using changeRadius()
    circle1.changeRadius(5.0);
    circle2.changeRadius(3.5);
    
    // Compare using the getter
    if (circle1.getRadius() > circle2.getRadius()) {
        std::cout << "Circle 1 has a bigger radius!\n";
    } else if (circle1.getRadius() < circle2.getRadius()) {
        std::cout << "Circle 2 has a bigger radius!\n";
    } else {
        std::cout << "Both circles have the same radius!\n";
    }
    
    return 0;
}

2. Make radius Public (Quick, But Not Ideal)

If you just want to test things out and don’t care about encapsulation right now, you can make radius public (like structs do). But note: this breaks the encapsulation principle of classes, which is useful for protecting data from unintended changes.

Update your class:

// YourClass.h
class YourClass {
public: // Now members are public
    double radius;
    void printInfo();
    void changeRadius(double newRadius);
};

Then compare just like you would with a struct:

if (circle1.radius == circle2.radius) {
    std::cout << "Same radius!\n";
}

3. Overload Comparison Operators (More OOP-Friendly)

For a more elegant, object-oriented approach, you can define comparison operators directly in your class. This lets you compare instances directly, like if (circle1 > circle2).

Add these to your class declaration:

// YourClass.h
class YourClass {
private:
    double radius;
public:
    void printInfo();
    void changeRadius(double newRadius);
    double getRadius() const { return radius; }
    
    // Overload comparison operators
    bool operator==(const YourClass& other) const {
        return this->radius == other.radius;
    }
    
    bool operator>(const YourClass& other) const {
        return this->radius > other.radius;
    }
    
    bool operator<(const YourClass& other) const {
        return this->radius < other.radius;
    }
};

Now you can use these operators naturally:

if (circle1 == circle2) {
    std::cout << "Equal radii!\n";
} else if (circle1 > circle2) {
    std::cout << "Circle1 is larger!\n";
}

Start with the getter method first—it’s the best practice and will help you get comfortable with class encapsulation, which is one of the core benefits of using classes over structs for more complex code.

内容的提问来源于stack exchange,提问作者Lennac

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 08:30:29