单.cpp文件定义多类时,下层类函数无法被main访问的问题求助
Fixing Your C++ OOP Program Issues
Let's walk through the problems in your code and get it working properly:
Key Issues Identified
- Private Member Access in Circle Class: In C++, when you define a class using the
classkeyword, all members (variables and functions) are private by default. That means yoursetR,area, andcircumferencefunctions can't be called frommain()—they're only accessible within the Circle class itself. - Syntax Error in Main: There's a typo in the circle area output line:
cout << "The area of the circle is " << c.area(); << "\n"l;has an extra semicolon afterc.area()and a mistyped newline ("\n"linstead of"\n";). - Inefficient Pi Declaration: Your
pivariable is a non-static member of Circle, which means every Circle object will store its own copy. Since pi is a constant, making it aconst staticmember is better practice.
Corrected Code
#include <iostream> #include <string> using namespace std; /*----------------RECTANGLE CLASS----------------*/ class rectangle { int w, h; public: void setVals(int, int); int area(); int perimeter(); }; void rectangle::setVals(int x, int y) { w = x; h = y; } int rectangle::area() { return w * h; } int rectangle::perimeter() { return (2 * w) + (2 * h); } /*----------------CIRCLE CLASS----------------*/ class circle { const static double pi; // Declare static constant pi double r; public: // Added public access specifier for these functions void setR(double); double area(); double circumference(); }; // Define the static constant outside the class const double circle::pi = 3.14159265358979; void circle::setR(double radius) { r = radius; } double circle::area() { return pi * (r * r); } double circle::circumference() { return 2 * pi * r; } /*----------------MAIN FUNCTION----------------*/ int main() { int choice; cout << "Enter 1 if you would like to calculate the area of a rectangle, Enter 2 if you would like calculate the area of a circle\n"; cin >> choice; if (choice == 1) { int width, height; cout << "Enter the width of the rectangle "; cin >> width; cout << "Enter the height of the rectangle "; cin >> height; rectangle r; r.setVals(width, height); cout << "The area of the rectangle is " << r.area() << "\n"; cout << "The perimeter of the rectangle is " << r.perimeter() << "\n"; } else if (choice == 2) { double r; cout << "Enter the radius of the circle "; cin >> r; circle c; c.setR(r); // Fixed syntax errors here cout << "The area of the circle is " << c.area() << "\n"; cout << "The circumference of the circle is " << c.circumference() << "\n"; } return 0; }
Explanation of Changes
- Public Access for Circle Functions: Added the
public:specifier abovesetR,area, andcircumferencein the Circle class, allowingmain()to call these methods directly. - Fixed Syntax Error: Removed the extra semicolon after
c.area()and corrected the mistyped newline to"\n";to resolve the compilation failure. - Static Constant Pi: Changed
pito aconst staticmember, defined outside the class. This ensures only one copy of pi exists in memory, which is more efficient and aligns with best practices for constant values shared across all class instances.
内容的提问来源于stack exchange,提问作者lazy-zebra
相关产品推荐
相关产品推荐

