该基类的基类和派生类可以有一个公共朋友班,这是两个类的朋友吗?

发布于 2025-01-18 14:26:52 字数 135 浏览 4 评论 0原文

我知道朋友班可以访问类的私人元素,所以我想知道天气派生的班级可以使用朋友班来访问基类的私人成员,如果朋友班级是基础类的朋友,并且派生的班级?

我是编程的完整初学者,我刚刚开始学习C ++的OOP;当我了解朋友功能/课程时,我只是想问这个问题。

I know that a friend class can access the private elements of a class, so I was wondering weather a derived class can use a friend class to access the private members of the base class, if the friend class is friends with both the base class and the derived class?

I am a complete beginner in programming and I have just started learning OOP in C++; I just wanted to ask this question as it came to my mind when I learned of friend functions/classes.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

归属感 2025-01-25 14:26:52

在C ++中,
为了授予对班级的私人或受保护成员的访问,我们应该定义朋友类或功能(或其他类的方法)。

友谊没有继承。

因此,为了访问基类和派生类的私人或受保护的成员,从另一个类中,您应该将“另一个”类定义为基类的朋友,并将其定义。

为了
“ ....如果朋友类是基类和派生类的朋友,派生的类可以使用朋友类访问基类的私人成员吗?....”问题。

友谊不是相互的。如果Y是Y的朋友,那么Y不会自动成为X的朋友。
因此,派生的类无法使用基类的朋友访问基类的私人成员。

可以使用朋友F类访问来自派生的D类B类B类的私人成员的方法。B

  1. 应该将F定义为朋友。 (f可以访问b)b)
  2. f应该将d定义为朋友。(d可以访问私人和受保护的成员)
  3. f应定义访问和/修改B的方法。F。F
  4. 应定义一个属性在类型B中,并将此属性设置为构造函数或方法中的参数。
  5. D应该在F类型中定义一个属性,并使用4中定义的方法或约束器将其设置为此属性。

在末尾D可以通过类型F中的属性访问B的所有私人成员。

In C++,
In order to grant access to private or protected members of a class, we should define a friend class or a function (or a method of another class).

Friendship is not inherited.

So in order to access private or protected members of a base class and derived class, from another class you should define the "another" class as friend of both base class and it s derived class.

For the
"....can a derived class use a friend class to access the private members of the base class if the friend class is friends with both the base class and the derived class?...." question.

Friendship is not mutual. If a class Y is a friend of Y, then Y doesn’t become a friend of X automatically.
So derived class can not access private members of base class using friend of base class.

There can be a way to access private members of a base class B from derived class D by using friend class F.

  1. B should define F as a friend. (F can access private and protected members of B)
  2. F should define D as a friend.(D can access private and protected members of F)
  3. F should define methods to access and/modify all private members of B.
  4. F should define an attribute in type B and set this attribute as a parameter in constructor or in a method.
  5. D should define an attribute in type F and set itself to this attribute using the method or construtor defined in 4.

At the end D can access all private members of B via the attribute in type F.

脸赞 2025-01-25 14:26:52

您可以在同一文件中定义同一类,以便两者可以访问朋友函数。 交易的正向声明允许customer
类参考事务类,而不会导致编译器错误。
如果您想进一步了解这一点,请考虑阅读一本书,因为互联网对学习并不是有时也有毒。

#include<iostream>
using namespace std;
class Transaction;
class Customer
{
   friend void applyTransaction(Customer, Transaction);
private:
   int custNum;
   double balanceDue;
public:
   Customer(int = 0, double = 0.0);
};
Customer::Customer(int num, double balance)
{
   custNum = num;
   balanceDue = balance;
}
class Transaction
{
   friend void applyTransaction(Customer, Transaction);
private:
   int transactionNum;
   int custNum;
   double amount;
public:
   Transaction(int = 0, int = 0, double = 0.0);
};
Transaction::Transaction(int trans, int cust, double amt)
{
   transactionNum = trans;
   custNum = cust;
   amount = amt;
}
void applyTransaction(Customer cust, Transaction trans)
{
   cout << "Customer #" << cust.custNum <<
   " original balance of $" <<
   cust.balanceDue << endl;
   cust.balanceDue += trans.amount;
   cout << "After transaction #" <<
   trans.transactionNum << " for " <<
   trans.amount << " the new balance is $" <<
   cust.balanceDue << endl;
}

You can define the same class in the same file so that the two can access the friend function. The forward declaration of Transaction allows the Customer
class to refer to the Transaction class without causing a compiler error.
If you want to know more about this things consider reading a book because internet is not really good for learning also rather toxic at times.

#include<iostream>
using namespace std;
class Transaction;
class Customer
{
   friend void applyTransaction(Customer, Transaction);
private:
   int custNum;
   double balanceDue;
public:
   Customer(int = 0, double = 0.0);
};
Customer::Customer(int num, double balance)
{
   custNum = num;
   balanceDue = balance;
}
class Transaction
{
   friend void applyTransaction(Customer, Transaction);
private:
   int transactionNum;
   int custNum;
   double amount;
public:
   Transaction(int = 0, int = 0, double = 0.0);
};
Transaction::Transaction(int trans, int cust, double amt)
{
   transactionNum = trans;
   custNum = cust;
   amount = amt;
}
void applyTransaction(Customer cust, Transaction trans)
{
   cout << "Customer #" << cust.custNum <<
   " original balance of 
quot; <<
   cust.balanceDue << endl;
   cust.balanceDue += trans.amount;
   cout << "After transaction #" <<
   trans.transactionNum << " for " <<
   trans.amount << " the new balance is 
quot; <<
   cust.balanceDue << endl;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文