0

I stumbled across what seems to me a very interesting problem. I am not asking for a solution to this problem, only advices on how to continue with my code and I'd like to know if derived classes inherit the friends of the base class.

This is the problem:

Class matrix, friend of class array is the base class for the class diagonal_matrix. The derived class must contain a parameterized constructor through which to highlight the transmission of parameters towards the constructor from the base class, destructor and a method to check if the matrix is square and diagonal ( all elements other than the ones from the principal diagonal are equal to zero). Illustrate the concept of virtual function (pure if it's more natural in implementation).

This is what I wrote so far (not very creative today on how to continue):

#include <iostream>

using namespace std;

class arry
{
private:
    int *vec;
    int n;
public:
    int i,nrelem;
    arry(){};
    ~arry();
    void readArray(int);

};
arry::~arry()
{
    n=0;
    delete [] vec;
}
void arry::readArray(int elem)
 {
    n=elem;
    vec=new int[n];
   for(i=1;i<=elem;i++)
   {
       cout<<"vec["<<i<<"]=";
       cin>>vec[i];
   }
}

class matrix
{
public:
    friend class arry;
    matrix(int , int);
    ~matrix();
    };

int main()
{
    return 0;
} 
Deduplicator
  • 44,692
  • 7
  • 66
  • 118
  • 2
    You said, "I'd like to know if derived classes inherit the friends of the base class." No, they don't. Think of it this way - your father's friend is not automatically your friend. โ€“ R Sahu May 28 '14 at 20:14
  • You also said, "Class matrix, friend of class array...". Looking at the class definitions, it is the other way around. Perhaps your understanding of `friend` is different from what the language defines a `friend`. โ€“ R Sahu May 28 '14 at 20:17
  • I've read the problem again , it definitely says "Class matrix , friend of class array" โ€“ Vlad Gheorghe May 28 '14 at 20:58

1 Answers1

1

friend of class array is the base class for the class diagonal_matrix

Class array has no friends. It is this class array that is a friend of class matrix, becuase it si matrix that has stated

class matrix
{
public:
    friend class arry;  // this is my friend
    matrix(int , int);
    ~matrix();
};

You asked for

advices on how to continue with my code

Start from declaring a friendship in class array.

and I'd like to know if derived classes inherit the friends of the base class

If base class declares class A as it's friend it is not a friend to derived class unless this derived class will declare class A a friend too.

C++ Standard n3337 ยง 11.3/10 Friends

Friendship is neither inherited nor transitive.

4pie0
  • 29,204
  • 9
  • 82
  • 118