c++ - 构造函数未初始化对象成员数据

标签 c++ constructor

我正在尝试学习 C++,但我在编写这个程序时遇到了一些困难。本质上,该程序将所需尺寸的矩形写入屏幕。很简单。但是,我无法让程序创建其尺寸由构造函数参数初始化的矩形。我在构造函数中放置了一条 cout 语句,以验证构造函数是否正在获取参数并初始化维度变量,并且它似乎正在完成它的工作。但是,当我调用绘制矩形或计算面积的函数时,它分别不返回任何内容和 0。我确定我犯了一个愚蠢的错误,但我看不出我哪里错了。

代码如下:

#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";
        }

};

谢谢,我很感激你能给我的任何帮助。

最佳答案

Rectangle::Rectangle(int length,int width)
{
    int itsLength = length;
    int itsWidth = width;
}

在您的构造函数中,您声明了两个本地 变量,分别称为itsLengthitsWidth。它们会覆盖您类中的两个同名变量。

关于c++ - 构造函数未初始化对象成员数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19083864/

相关文章:

c++ - 如何在不先将整个文件读入内存的情况下使用 Boost::Spirit::Lex 对文件进行 lex?

c++ - Delta V 计算器问题与对数

java - 创建通用数组

python - 新实例具有旧实例属性值

kotlin - 当抽象类的构造函数在 kotlin 中是私有(private)的时会发生什么?

c++ - 这个 while 循环执行其正确的功能,但需要其他东西吗?

c++ - 使用 makefile 中的隐式规则进行清理

java - 面试问题 - 在排序数组 X 中搜索索引 i 使得 X[i] = i

class - 多参数构造函数的回调

c++ - 在 C++ 构造函数失败的情况下处理多个动态资源解除分配