This is my code:
#include <iostream>
#include <cstring>
using namespace std;
class ObiectCosmic{
protected:
char* nume;
float raza;
float masa;
public:
ObiectCosmic(char* nume = "", float raza = 0.0, float maza = 0.0);
virtual ~ObiectCosmic();
};
ObiectCosmic::ObiectCosmic(char* nume, float raza, float masa){
this->nume = new char[strlen(nume) + 1];
strcpy(this->nume, nume);
this->raza = raza;
this->masa = masa;
}
ObiectCosmic::~ObiectCosmic(){
delete[] nume;
}
class Planeta: public ObiectCosmic{
private:
float perioadaRotatie;
public:
Planeta(char* nume = "", float raza = 0.0, float masa = 0.0, float perioadaRotatie = 0.0);
~Planeta();
friend ostream& operator<<(ostream& os, const Planeta& p);
};
Planeta::Planeta(char* nume, float raza, float masa, float perioadaRotatie): ObiectCosmic(nume, raza, masa){
this->perioadaRotatie = perioadaRotatie;
}
Planeta::~Planeta(){}
ostream& operator<<(ostream& os, const Planeta& p){
os << "Nume: " << p.nume << ", raza: " << p.raza << "masa: " << p.masa << ", rotatie: " << p.perioadaRotatie << endl;
return os;
}
class Stea: public ObiectCosmic{
private:
float stralucire;
public:
Stea(char* nume = "", float raza = 0.0, float masa = 0.0, float stralucire = 0.0);
~Stea();
friend ostream& operator<<(ostream& os, const Stea& s);
};
Stea::Stea(char* nume, float raza, float masa, float stralucire): ObiectCosmic(nume, raza, masa){
this->stralucire = stralucire;
}
Stea::~Stea(){}
ostream& operator<<(ostream& os, const Stea& s){
os << "Nume: " << s.nume << ", raza: " << s.raza << "masa: " << s.masa << ", stralucire: " << s.stralucire << endl;
return os;
}
int main(){
Stea s("Soare", 123123.54, 3543454325.2, 343434.132);
Planeta p("Pamant", 34132.12, 567546.234, 3545.13);
Planeta p2;
ObiectCosmic* obiecte[] = {
&s,
&p,
&p2
};
for(int i = 0; i < sizeof(obiecte) / sizeof(ObiectCosmic*); i++)
cout << obiecte[i] << endl;
return 0;
}
He is printing the adress of objects instead of what i wrote.
I tried with cout << *obiecte[i] << endl;, but i have an error.
10.cpp:79:14: error: no match for 'operator<<' (operand types are 'std::ostream' {aka 'std::basic_ostream<char>'} and 'ObiectCosmic')
79 | cout << *obiecte[i] << endl;
| ~~~~ ^~ ~~~~~~~~~~~
| | |
| | ObiectCosmic
| std::ostream {aka std::basic_ostream<char>}
In file included from C:/msys64/mingw64/include/c++/12.2.0/iostream:39