Creating a 3D Box Class with Comparison Operasions
Let's design a C++ class to represent a three-dimensional box with methods to calculate surface area and volume, plus compariosn functionality.
#include <iostream>
using namespace std;
class Box3D {
private:
int m_length;
int m_width;
int m_height;
public:
// Accessor methods
int getLength() const { return m_length; }
void setLength(int value) { m_length = value; }
int getWidth() const { return m_width; }
void setWidth(int value) { m_width = value; }
int getHeight() const { return m_height; }
void setHeight(int value) { m_height = value; }
// Calculate surface area
int calculateSurfaceArea() const {
return 2 * ((m_length * m_width) + (m_length * m_height) + (m_height * m_width));
}
// Calculate volume
int calculateVolume() const {
return m_length * m_width * m_height;
}
// Member function to compare with another box
bool isIdentical(const Box3D& other) const {
return (m_length == other.getLength() &&
m_width == other.getWidth() &&
m_height == other.getHeight());
}
};
// Global function for box comparison
bool areBoxesEqual(const Box3D& box1, const Box3D& box2) {
return (box1.getLength() == box2.getLength() &&
box1.getWidth() == box2.getWidth() &&
box1.getHeight() == box2.getHeight());
}
int main() {
Box3D box1;
box1.setLength(10);
box1.setWidth(10);
box1.setHeight(10);
cout << "Surface Area: " << box1.calculateSurfaceArea() << endl;
cout << "Volume: " << box1.calculateVolume() << endl;
Box3D box2;
box2.setLength(10);
box2.setWidth(10);
box2.setHeight(1);
bool result = areBoxesEqual(box1, box2);
cout << "Global comparison: " << (result ? "Boxes are equal" : "Boxes are different") << endl;
result = box1.isIdentical(box2);
cout << "Member comparison: " << (result ? "Boxes are equal" : "Boxes are different") << endl;
return 0;
}
Analyzing Point-Circle Geometric Relationships
This example demonstrates how to detemrine whether a point lies inside, on, or outside a circle using object-oriented design.
#include <iostream>
#include <cmath>
using namespace std;
class Coordinate2D {
private:
int m_xPos;
int m_yPos;
public:
void setX(int x) { m_xPos = x; }
int getX() const { return m_xPos; }
void setY(int y) { m_yPos = y; }
int getY() const { return m_yPos; }
};
class Circle2D {
private:
int m_radius;
Coordinate2D m_center;
public:
void setRadius(int r) { m_radius = r; }
int getRadius() const { return m_radius; }
void setCenter(const Coordinate2D& center) { m_center = center; }
Coordinate2D getCenter() const { return m_center; }
};
void checkPointCirclePosition(const Circle2D& circle, const Coordinate2D& point) {
// Calculate squared distance between point and center
int distanceSquared =
pow(circle.getCenter().getX() - point.getX(), 2) +
pow(circle.getCenter().getY() - point.getY(), 2);
// Calculate squared radius
int radiusSquared = pow(circle.getRadius(), 2);
// Determine relative position
if (distanceSquared == radiusSquared) {
cout << "Point lies on the circle" << endl;
} else if (distanceSquared > radiusSquared) {
cout << "Point is outside the circle" << endl;
} else {
cout << "Point is inside the circle" << endl;
}
}
int main() {
// Create a circle
Circle2D myCircle;
myCircle.setRadius(10);
Coordinate2D center;
center.setX(10);
center.setY(0);
myCircle.setCenter(center);
// Create test point
Coordinate2D testPoint;
testPoint.setX(10);
testPoint.setY(10);
// Check position
checkPointCirclePosition(myCircle, testPoint);
return 0;
}
Code Organization with Header and Implementation Files
For better code organization and maintainability, split class definitions and implementations into separate files:
- .h (header) files: Contain class declarations, member function prototypes, and data members
- .cpp (implementation) files: Contain the actual implementation of member functions
This separation improves compilation times, enables better code reuse, and creates clearer project structure. When using separate files, include the header file in the implementation file using #include "classname.h".