【问题标题】:How to make public members of a class private?如何将类的公共成员设为私有?
【发布时间】:2014-09-22 20:17:52
【问题描述】:

我对编程(一般而言)和 C++(特别是)是全新的。我正在尝试采用以下公共成员变量并将它们设为私有:

int *coeff;
int order;

很遗憾,我看到以下错误:

'Poly::coeff' : 无法访问在类 'Poly' 中声明的私有成员

'Poly::order' : 无法访问在类 'Poly' 中声明的私有成员

这是我的代码:

#include "stdafx.h"
#include <iostream>
#include <cstdlib>

using namespace std;

class Poly
{
public:

    int *coeff;
    int order;

    int getData();
    int display(int *coeff, int order);
    void addition(Poly P1, Poly P2);
    void subtraction (Poly P1, Poly P2);
    void multiplication (Poly P1, Poly P2);

//  ~Poly();
};

int Poly::display(int *coeff, int order)
{
    int i;
    int j;
    for (i = order; i >= 0; i--)
    {
        cout << coeff[i] << "x^" << i;
        if ((i - 1) != -1)
        {
            cout << "+";
        }
    }
    cout << "\n";
    return 0;
}

int Poly::getData()
{
    int i;
    cout << "Please enter the order of the polynomial: ";
    cin >> order;
    coeff = new int[order + 1];
    for (i = order; i >= 0; i--)
    {
        cout << "Please enter the coefficient of x^" << i << " :";
        cin >> coeff[i];
    }
    return 0;
}

void Poly::addition(Poly P1, Poly P2)
{
    int max;
    int i;
    max = (P1.order > P2.order) ? P1.order : P2.order;
    int *add = new int[max + 1];

    if (P1.order == P2.order)
    {
        for (i = P1.order; i >= 0; i--)
        {
            add[i] = P1.coeff[i] + P2.coeff[i];
        }
    }

    if (P1.order > P2.order)
    {
        for (i = P1.order; i > P2.order; i--)
        {
            add[i] = P1.coeff[i];
        }
        for (i = P2.order; i >= 0; i--)
        {
            add[i] = P1.coeff[i] + P2.coeff[i];
        }
    }

    if (P1.order < P2.order)
    {
        for (i = P2.order; i > P1.order; i--)
        {
            add[i] = P2.coeff[i];
        }
        for (i = P1.order; i >= 0; i--)
        {
            add[i] = P1.coeff[i] + P2.coeff[i];
        }
    }
    cout << "\nAddition:";
    display(add, max);
    cout << "\n";
}

void Poly::subtraction(Poly P1, Poly P2)
{
    int max;
    int i;
    max = (P1.order > P2.order) ? P1.order : P2.order;
    int *sub = new int[max + 1];

    if (P1.order == P2.order)
    {
        for (i = P1.order; i >= 0; i--)
        {
            sub[i] = P1.coeff[i] - P2.coeff[i];
        }
    }

    if (P1.order > P2.order)
    {
        for (i = P1.order; i > P2.order; i--)
        {
            sub[i] = P1.coeff[i];
        }
        for (i = P2.order; i >= 0; i--)
        {
            sub[i] = P1.coeff[i] - P2.coeff[i];
        }
    }

    if (P1.order < P2.order)
    {
        for (i = P2.order; i > P1.order; i--)
        {
            sub[i] = -P2.coeff[i];
        }
        for (i = P1.order; i >= 0; i--)
        {
            sub[i] = P1.coeff[i] - P2.coeff[i];
        }
    }
    cout << "\nSubtraction:";
    display(sub, max);
    cout << "\n";
}

void Poly::multiplication(Poly P1, Poly P2)
{
    int i;
    int j;
    int max;

    max = P1.order + P2.order;
    int *mult = new int[max + 1];

    for (i = P1.order; i >= 0; i--)
    for (j = P2.order; j >= 0; j--)
    {
        mult[i + j] += P1.coeff[i] * P2.coeff[i];
    }
        cout << "\nMultiplication:";
        display(mult, max);
}



int main()
{
    int choice;
    Poly P1, P2, P3;
    cout << "-------- Instructions --------" << endl;

    cout << "For polynomial 1... " << endl;
    P1.getData();

    cout << endl;

    cout << "For polynomial 2... " << endl;
    P2.getData();

    while (1)
    {
        cout << "\n******** Menu Selection ********" << endl;
        cout << "1: Addition\n2: Subtraction\n3: Mutiplication\n0: Exit" << endl;
        cout << "Please enter your choice (1, 2, 3 or 0):";
        cin >> choice;

        switch (choice)
        {
        case 1:
            cout << "\n-------- Addition --------\n";
            cout << "Polynomial 1: ";
            P1.display(P1.coeff, P1.order);
            cout << "Polynomial 2: ";
            P2.display(P2.coeff, P2.order);
            P3.addition(P1, P2);
            cout << "--------------------------\n";
            break;

        case 2:
            cout << "\n------ Subtraction ------\n";
            cout << "Polynomial 1: ";
            P1.display(P1.coeff, P1.order);
            cout << "Polynomial 2: ";
            P2.display(P2.coeff, P2.order);
            P3.subtraction(P1, P2);
            cout << "--------------------------\n";
            break;

        case 3:
            cout << "\n------ Subtraction ------\n";
            cout << "Polynomial 1: ";
            P1.display(P1.coeff, P1.order);
            cout << "Polynomial 2: ";
            P2.display(P2.coeff, P2.order);
            P3.multiplication(P1, P2);
            cout << "--------------------------\n";
            break;

        case 0:
            cout << "The program will now terminate.  Thank you." << endl;
            exit(0);

        default:
            cout << endl;
            cout << "You have entered an invalid selection." << endl;
            cout << "Please enter a positive integer between 0 and 3.";
            cout << endl;
        }
    }

    return 0;
}

谁能提供有关如何最好地修改我的代码以使 int *coeff 和 int order 成为私有成员的指导?

提前非常感谢。 -瑞安

【问题讨论】:

  • 对于刚接触 C++ 的人来说,这是一大堆代码……也许从较小的程序开始。
  • 添加允许访问私有数据成员的成员函数。而你的班级需要遵循Rule of Three,或者更好的是,将int *coeff替换为std::vector&lt;int&gt; coeff
  • 您可以通过private: 将其设为私有。错误消息表明您已经尝试过,错误表明您正在尝试从类外部访问私有变量。让你的函数接受名为coefforder 的参数也会令人困惑,因为在函数体中,这些名称隐藏了类成员。我怀疑你想要做的是让display不带参数,并将P1.display(P1.coeff, P1.order);更改为P1.display()

标签: c++ class private-members


【解决方案1】:

制作private 的想法意味着您不允许在成员或friend 函数之外访问(这就是private 的目的)。代码在main 中失败,例如P1.display(P1.coeff, P1.order),因为main 不是Poly 类的成员,因此不允许 访问Poly::coeffPoly::order(正如编译器告诉你的那样)。

我建议您将Poly::display(int *coeff, int order) 的定义更改为不需要您传入coefforder,因为实例已经知道这些值是什么。我也会拿出return 0,因为它不会给你买任何东西。

void Poly::display()
{
    int i;
    int j;
    for (i = order; i >= 0; i--)
    {
        cout << coeff[i] << "x^" << i;
        if ((i - 1) != -1)
        {
            cout << "+";
        }
    }
    cout << "\n";
}

在该成员函数中,变量 ordercoeff 隐式地获取了您的类的成员,这些成员之前被您传递的参数遮蔽

【讨论】:

    【解决方案2】:

    “谁能提供有关如何最好地修改我的代码以使 int *coeff 和 int order 成为私有成员的指导?”

    您在main()中的代码

    P1.display(P1.coeff, P1.order);
    

    现在仍然尝试直接访问这些private 类成员,这当然行不通(这就是我们将这些成员设为private 的原因)。

    对于您的情况,您根本不需要将这些变量作为参数传递给该函数,因为实际值已经可用于 class PolyP1 实例。

    将您的成员函数签名更改为

    int display();
    

    和定义到

    int Poly::display() {
        int i;
        int j;
        for (i = order; i >= 0; i--) {
            cout << coeff[i] << "x^" << i;
            if ((i - 1) != -1) {
                cout << "+";
            }
        }
        cout << "\n";
        return 0;
    }
    

    请注意,在Poly::display() 成员函数中访问ordercoeff 可以被视为等效于this-&gt;orderthis-&gt;coeff

    类成员函数可以认为是隐含了this指针,可以直接访问(private)类成员变量,不需要指定这些为参数。

    按照上述建议更改内容后,您可以调用

    P1.display();
    

    将其他签名/访问权限更改为coefforder 类似。


    注意:

    您的类声明中存在更多缺陷。像

    void addition(Poly P1, Poly P2);
    

    效果不好。您希望将实际实例作为左侧值进行操作,而将另一个作为常量右侧值进行操作。恕我直言,您的成员函数应如下所示

    Poly& addition(const Poly& rhs) {
         // perform addition operations with this and rhs
         return *this;
    }
    
    Poly addition(const Poly& rhs) const {
         Poly result = *this;
         // perform addition operations with result and rhs
         return result;
    }
    

    【讨论】:

      猜你喜欢
      • 2013-06-08
      • 2011-11-02
      • 2013-10-10
      • 2014-10-27
      • 2014-06-03
      • 2016-04-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多