Witam, posiadam klasę Figura a w niej operator << wyświetlający elementy klasy.
Mam utworzyć klasę dziedziczącą Koło po klasie Figura i w niej również zawrzeć operator <<
Jednak otrzymuję błąd, iż ten operator nie posiada praw do tych zmiennych.
Error 1 error C2248: 'Figura::nazwa' : cannot access private member declared in class 'Figura' c:\users\paweł kreis\documents\visual studio 2013\projects\lab3\lab3\source.cpp 53 1 Lab3
Error 2 error C2248: 'Figura::pole' : cannot access private member declared in class 'Figura' c:\users\paweł kreis\documents\visual studio 2013\projects\lab3\lab3\source.cpp 53 1 Lab3
Same klasy ze sobą się przyjaźnią
Poniżej kod:
#include <iostream>
#include <string>
using namespace std;
class Kolo;
class Kwadrat;
class Figura
{
public:
Figura(string name = "figura" , double pl = 5)
{
nazwa = name;
pole = pl;
ilosc++;
}
virtual ~Figura()
{
ilosc--;
}
friend ostream& operator<< (ostream &wyjscie, Figura const& fi)
{
wyjscie << "Nazwa figury: " << fi.nazwa << " Pole: " << fi.pole << endl;
return wyjscie;
}
private:
string nazwa;
double pole;
static int ilosc;
public:
static int IleElementow()
{
cout << ilosc;
return ilosc;
}
friend class Kolo;
friend class Kwadrat;
};
class Kolo : public Figura
{
Kolo(string name = "figura", double pl = 5, double pr = 2)
{
nazwa = name;
pole = pl;
promien = pr;
ilosc++;
}
public:
friend ostream& operator << (ostream &wyjscie, Kolo fi)
{
wyjscie << "Nazwa figury: " << fi.nazwa << " Pole: " << fi.pole << endl;
return wyjscie;
}
private:
double promien;
};
class Kwadrat : public Figura
{
friend class Figura;
public:
int sprawdz()
{
cout << nazwa;
}
private:
double promien;
};
int Figura::ilosc = 0;
int main()
{
Figura f1("kwadrat", 4);
Figura f2("kolo", 4);
Figura f3;
f1.IleElementow();
cout << f1;
}
Jak dokonać zatem by to zadziałało?