我是编程(一般)和C ++(特别是)的新手。我试图采用以下公共成员变量并将其设为私有:
int *coeff;
int order;
不幸的是,我发现了以下错误:
'聚::系数_' :无法访问类' Poly'
中声明的私有成员和
'聚::顺序' :无法访问类' 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是私有成员吗?
非常感谢提前。 -Ryan
答案 0 :(得分:3)
制作private
内容的想法意味着您 不允许访问成员或friend
函数之外(这是private
的目的)。代码在main
P1.display(P1.coeff, P1.order)
失败,因为main
不是Poly
类的成员,因此,不允许} 访问Poly::coeff
或Poly::order
(正如编译器告诉你的那样)。
我建议您将Poly::display(int *coeff, int order)
的定义更改为不要求您传递coeff
和order
,因为实例已经知道这些值是什么。我也会拿出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";
}
在该成员函数中,变量order
和coeff
隐含地拾取您传递的参数之前 shadowed 的类成员。
答案 1 :(得分:2)
“任何人都可以提供有关如何最好地修改我的代码的指导,以便int * coeff和int order是私有成员吗?”
您在main()
P1.display(P1.coeff, P1.order);
仍然会尝试直接访问这些private
类成员,这当然不起作用(这就是我们制作这些成员的原因private
)。
对于您的情况,您根本不需要将这些变量作为参数传递给该函数,因为实际值已经可用于P1
class Poly
实例。
将您的会员功能签名更改为
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;
}
请注意,访问order
成员函数中的coeff
或Poly::display()
,可以理解为等同于this->order
和this->coeff
。
可以认为类成员函数隐式具有this
指针,并且可以直接访问(private
)类成员变量,而无需将这些变量指定为参数。
按照上面的建议更改内容后,您只需调用
即可P1.display();
更改其他签名/访问coeff
,order
类似。
注意:
您的班级声明中存在更多缺陷。像
这样的东西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;
}