构造函数未初始化对象成员数据
我正在尝试学习C ++,但是我在编写此程序时遇到了一些困难.本质上,程序将所需尺寸的矩形写入屏幕.很简单.但是,我无法获得程序来创建其矩形由构造函数参数初始化的矩形.我在构造函数中放置了一个cout语句,以验证构造函数正在使用参数并初始化维度变量,并且它似乎正在执行其工作.但是,当我调用该函数以绘制矩形或计算面积时,它分别不返回任何值和0.我确定我犯了一个愚蠢的错误,但是我看不到哪里出了问题.
I'm trying to learn C++, but i'm having a little bit of difficulty with this program i'm writing. Essentially, the program writes a rectangle of desired dimensions to the screen. Simple enough. However, I can't get the program to create the rectangle whose dimensions are initialized by the constructor parameters. I put a cout statement in the constructor to verify that the constructor is taking the parameters and initializing the dimension variables, and it seems to be doing its job. When I call the function to draw the rectangle or calculate the area however, it returns nothing and 0, respectively. I'm sure I've made a stupid mistake, but I cant see where I went wrong.
这是代码:
#include <iostream>
using namespace std;
enum choice {DrawRect=1,GetArea,GetPerim,ChangeDimensions,Quit};
//Rect Class Declaration
class Rectangle
{
public:
// Constructors
Rectangle(int,int);
~Rectangle();
//Accessors
int GetArea() const {return itsLength * itsWidth;}
int GetLength() const {return itsLength;}
int GetWidth() const {return itsWidth;}
int GetPerim() const {return 2*itsWidth + 2*itsLength;}
void ChangeDimensions(int length,int width);
//Member Data
int itsLength;
int itsWidth;
};
//Implementation of Methods not defined inline
Rectangle::Rectangle(int length,int width)
{
int itsLength = length;
int itsWidth = width;
cout << "The constructor is creating a Rectangle with length " << itsLength << " and width " << itsWidth << "\n";
}
Rectangle::~Rectangle() {}
void Rectangle::ChangeDimensions(int length,int width)
{
itsLength = length;
itsWidth = width;
}
int DoMenu();
void DoDrawRect(Rectangle);
void DoGetArea(Rectangle);
void DoGetPerim(Rectangle);
//Main
int main()
{
//Initialize Rectangle theRect
Rectangle theRect(5,30);
int fQuit=false;
int choice = DrawRect;
while (!fQuit)
{
choice = DoMenu();
if (choice < DrawRect || choice > Quit)
{
cout << "Invalid Choice, Try Again.\n\n";
continue;
}
switch(choice)
{
case DrawRect:
DoDrawRect(theRect);
break;
case GetArea:
DoGetArea(theRect);
break;
case GetPerim:
DoGetPerim(theRect);
break;
case ChangeDimensions:
int newLength,newWidth;
cout << "\nEnter new length: ";
cin >> newLength;
cout << "\nEnter new width: ";
cin >> newWidth;
theRect.ChangeDimensions(newLength,newWidth);
DoDrawRect(theRect);
break;
case Quit:
fQuit=true;
cout << "Exiting....\n\n";
break;
default:
cout << "Error in Choice!!\n";
fQuit=true;
break;
}
}
return 0;
}
int DoMenu()
{
int choice;
cout << "\n****************";
cout << "\n Menu\n";
cout << "****************\n";
cout << "1: Draw Rectangle\n";
cout << "2: Get Area of Rectangle\n";
cout << "3: Get Perimeter of Rectangle\n";
cout << "4: Set Dimensions of Rectangle\n";
cout << "5: Quit Program\n";
cout << ": ";
cin >> choice;
return choice;
};
void DoGetArea(Rectangle theRect)
{
cout << "The Area of the Rectangle is " << theRect.GetArea() << "\n";
};
void DoGetPerim(Rectangle theRect)
{
cout << "The Perimeter of the Rectangle is " << theRect.GetPerim() << "\n";
};
void DoDrawRect(Rectangle theRect)
{
int width = theRect.GetWidth();
int length = theRect.GetLength();
for (int i = 0;i < width;i++)
{
for (int j = 0; j < length;j++)
{
cout << "*";
}
cout << "\n";
}
};
谢谢,我能为您提供任何帮助.
Thanks, I appreciate any help you can give me.
Rectangle::Rectangle(int length,int width)
{
int itsLength = length;
int itsWidth = width;
}
在构造函数中,您将声明两个名为 itsLength
和 itsWidth
的 local 变量.这些将覆盖您的类中两个同名的变量.
In your constructor you're declaring two local variables called itsLength
and itsWidth
. These override the two identically named variables in your class.