如何将类的公共成员设为私有
How to make public members of a class private?
我对编程(一般来说)和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: Additionn2: Subtractionn3: Mutiplicationn0: 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是私有成员?事先非常感谢。对
制作private
的想法意味着您不允许访问成员或friend
函数之外(这是private
的目的)。代码在P1.display(P1.coeff, P1.order)
等main
中失败,因为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
隐式地拾取类的成员,这些成员先前被您传递的参数遮蔽。
"谁能提供指导,如何最好地修改我的代码,使int *coeff和int order是私有成员?"
你在main()
中的代码
P1.display(P1.coeff, P1.order);
仍然试图直接访问这些private
类成员,这当然是行不通的(这就是为什么我们把这些成员设为private
)。
对于您的情况,您根本不需要将这些变量作为参数传递给该函数,因为实际值已经可以用于class Poly
的P1
实例。
将成员函数签名更改为
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()
成员函数中访问order
或coeff
,可以读为等同于this->order
和this->coeff
。
类成员函数可以被认为隐式地拥有一个this
指针,并且可以直接访问(private
)类成员变量,而不需要将这些变量指定为参数。
按照上面的建议修改完之后,你可以直接调用
P1.display();
修改其他签名/访问coeff
, order
类似。
注意:
你的类声明中有更多的缺陷。比如
void addition(Poly P1, Poly P2);
不能很好地工作。您希望使用实际实例作为左手值,而另一个作为常量右手值进行操作。IMHO你的成员函数应该是这样的
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;
}
- 将成员变量添加到共享库中的类中,不会破坏二进制兼容性吗
- 对RValue对象调用的LValue ref限定成员函数
- 为什么使用 "this" 指针调用派生成员函数?
- 具有奇怪重复模板模式的派生类中的成员变量已损坏
- 助记符和指向成员语法的指针
- 用于访问容器<T>数据成员的正确 API
- 内置函数可查看CPP中的成员变量
- 是否可以初始化不可复制类型的成员变量(或基类)
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 找不到成员对象:没有名为get_event()的成员,也处理多态性和向量
- 嵌套在类中时无法设置成员数据
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 将函数类成员映射到类本身内部
- 基于另一个成员参数将函数调用从类传递给它的一个成员
- 将Ref对象作为类成员
- 将包含C样式数组的对象初始化为成员变量(C++)
- 静态数据成员的问题-修复链接错误会导致编译器错误
- 将公共但非静态的成员函数与ALGLIB集成
- 多成员Constexpr结构初始化
- 我们可以访问一个不存在的联盟的成员吗