Is it wrong to mix struct with class in C++?

Asked

Viewed 466 times

0

I have the following class:

#ifndef PESSOA_H_INCLUDED
#define PESSOA_H_INCLUDED

#include <string>

struct aniver{

    int dia;
    int mes;
    int ano;
};

class Pessoa{

    private:

        std::string nome;
        std::string sexo;

        aniver nascimento;
        float altura;

    public:

        void setNome(std::string nome_);
        std::string getNome();
        void setSexo(std::string sexo_);
        std::string getSexo();
        void setNascimento(int dia, int mes, int ano);
        std::string getNascimento();
        void setAltura(float altura_);
        float getAltura();

    public:

        int calcIdade();

    public:

        void toString();

    };

#endif // PESSOA_H_INCLUDED

Its implementation:

#include "Pessoa.h"
#include <ctime>
#include <sstream>
#include <iostream>

void Pessoa::setNome(std::string nome_){

     nome=nome_;
}

std::string Pessoa::getNome(){

     return nome;
}

void Pessoa::setSexo(std::string sexo_){

     sexo=sexo_;
}

std::string Pessoa::getSexo(){

     return sexo;
}

void Pessoa::setNascimento(int dia, int mes, int ano){

     nascimento.dia=dia;
     nascimento.mes=mes;
     nascimento.ano=ano;
}

std::string Pessoa::getNascimento(){

     std::ostringstream nascimento_str;

     nascimento_str << nascimento.dia << "-" << nascimento.mes << "-" << nascimento.ano;

     return nascimento_str.str();
}

void Pessoa::setAltura(float altura_){

     altura=altura_;
}

float Pessoa::getAltura(){

    return altura;
}

int Pessoa::calcIdade(){

    struct tm *birth;
    time_t now;

    double seconds;
    int years;

    time(&now);

    birth=localtime(&now);

    birth->tm_mday=nascimento.dia;
    birth->tm_mon=nascimento.mes;
    birth->tm_year=nascimento.ano-1900;

    seconds=difftime(now, mktime(birth));

    years=seconds/60/60/24/365;

    return years;
}

void Pessoa::toString(){

     std::cout << "Nome........: " << getNome() << std::endl;
     std::cout << "Sexo........: " << getSexo() << std::endl;
     std::cout << "Idade.......: " << calcIdade() << std::endl;
     std::cout << "Peso........: " << getAltura() << std::endl;
     std::cout << "Nascimento..: " << getNascimento() << std::endl <<   std::endl;

}

As already said in the question I wonder if it is wrong to use struct along with class in c++ or if the way I applied struct in my code was unnecessary. So it is wrong to use struct along with class in C++?

2 answers

2

In C++ class and structure are the same thing. The only difference is that structure has its public members by default and class they are private by default. If you explain the visibility in the code, it makes no difference to use one or the other. People choose to use one or the other by convention, to indicate how it will normally be used, but it is not something that language requires.

So it’s normal to use guys who are struct or class within a struct or class. In a way it already does. A int it’s like a struct very simple, so simple that it doesn’t even need to be stated like that, but deep down it’s the same thing.

Actually there are a lot of other things that are very weird in the code. It works, but it doesn’t seem to be a C code++.

The biggest problem, and there is error is that it is corrupting memory by creating a pointer to the structure and not initializing it. It is true that probably these pointer is not necessary if use needs to do everything right. Interestingly, there are other members who should be (intelligent) pointers. And then there are other problems that will make the code misbehave.

In any language the error attempt does not work very well, but they give a reasonable hit rate when compiling. In C++ does not work. The hit rate is low when you don’t know deeply what you’re doing. It is very easy to run and be wrong and the problem only appear long after.

So the question problem does not exist, but the code has several other problems.

1


It is quite normal and common to create classes with structure type fields.

struct Point2D {
    float x , y ;
} ;

class Transform {
    Point2D translation ;
    float xAxisRotationAngle ;
    float yAxisRotationAngle ;
    Point2D scaleFactors ;
    // ...
} ;

Weird was the semantic issue involved. The type structure is called aniver including the year, but a person’s birthday date does not have a specific year, every year has birthday. It is better that the structure is called data (thus plays the same role with year included and still gains a broader meaning, making sense its use not only on birth and birthday dates but also other dates) and the class field be a date of birth.

Any further doubt?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.