output carrier overload

1

Given this structure:

struct Asignatura{
    string nombre;
    long id;
    int alumT;//numero de estudiantes que ven la asignatura
    int semestre;
    Profesor *prof;//puntero que señala la posicion de memoria del profesor que se le asigno
};

And this function to overload the operator < <:

ostream &operator<<(ostream &o,const Asignatura &as){
    return o<<"    "<<as.nombre<<endl<<"DATOS:"<<endl<<"ID.: " 
    <<as.id<<endl<<"Semestre: "<<as.semestre<<endl<<"Profesor: "<<as.prof- 
    >nombre<<"\n";
}

How do I add a conditional in <<"Profesor: "<<as.prof->nombre that says:

if(aux1->prof!=NULL){
    cout<<"Profesor: "<<aux1->prof->nombre;
} else {
    cout<<"\nSin profesor";
}

Because if not when a subject is shown the program closes because it is obviously trying to access a data that does not exist

* note: when I insert a new material I assure myself that prof=NULL

    
asked by Marcel Salazar 02.08.2018 в 00:57
source

2 answers

3

You do not have to return the ostream directly on the first line. On the other hand do not use NULL in C ++ you should use nullptr (to have more detail read the responses of this question ) In addition, the elements of a structure can have default values. And finally it is known that every element that is going to be printed has an overloaded ostream, so we save lines of code and consequently we have a more readable code.

#include <iostream>

using namespace std;

struct Profesor{
    string nombre;
};

struct Asignatura{
    string nombre;
    long id;
    int alumT;
    int semestre;
    Profesor *profesor = nullptr;
};

ostream &operator<<(ostream &o, const Profesor &profesor){
    return o << "Profesor: "<< profesor.nombre<<"\n";
}

ostream &operator<<(ostream &o, const Asignatura &as){
    o << as.nombre << "\nDATOS:\nID.: " << as.id << "\nSemestre: " << as.semestre <<"\n";
    if(as.profesor)
        o<< *as.profesor;
    return o;
}

int main()
{
    Profesor prof{"nombre_profesor"};
    Asignatura asig;
    asig.nombre = "nombre_de_asignatura";
    asig.id = 10;
    asig.alumT=10;
    asig.semestre = 2;
    cout<<"antes de asignar un profesor\n";
    cout<<asig;

    cout<<"despues de asignar un profesor\n";
    asig.profesor = &prof;
    cout<<asig;
    return 0;
}

Observation:

Many times it is preferable to use "\ n" than "std :: endl" (for more details read the responses of this post )

    
answered by 02.08.2018 / 05:26
source
3
  

How do I add in a conditional that says:

if(aux1->prof!=NULL){
    cout<<"Profesor: "<<aux1->prof->nombre;
} else {
    cout<<"\nSin profesor";
}

C ++ operators are functions like any other, the only difference is that they are invoked with a symbol instead of a function call:

  • operand1 simbolo operand2 .
  • símbolo( operand1 , operand2 ) .

So, like any function, it can have multiple exit points and can contain any compilable code. Therefore:

std::ostream &operator<<(std::ostream &o, const Asignatura &as){
    o << as.nombre << ' '
      << as.id << ' '
      << as.alumT << ' '
      << as.semestre << ' ';

    if (as.prof){
        o << "Profesor: " << as.prof->nombre;
    } else {
        o << "\nSin profesor";
    }

    return o;
};

Other things to consider.

The data stream data injection operator ( operator << ) is invoked when there is a data flow on the left side of the operator (it can be a flow to file std::ofstream or a flow to console std::cout ) and on the right side the overloaded object; as first parameter you will receive the flow of data that intervenes in the operation, and it is the one that you should always use within this operator; I say it because you used std::cout in your example.

It is discouraged to work with raw pointers, your object Asignatura contains a pointer to Profesor that should be replaced by a smart pointer or for a reference.

    
answered by 02.08.2018 в 08:24