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

1

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

Your 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 I said in the question I would like to know if it is wrong to use struct together with class in c ++ or if the way I applied struct to my code was unnecessary. So is it wrong to use struct along with class in C ++?

    
asked by anonymous 17.12.2017 / 06:18

2 answers

0

It's totally 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 ;
    // ...
} ;

Bizarre was the semantic question involved. The structure type is called aniver including the year, but a person's birthday does not have a specific year, every year has a birthday. It is best that the structure be called data (so it plays the same role with year included and still gains a wider meaning, making sense its use not only on birthdays and anniversary but also other dates) and the field of the class is a date of birth.

Any further questions?

    
17.12.2017 / 09:02
1

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 make explicit the visibility in the code, it does not make any difference to use one or the other. People choose to use one or the other by convention, to indicate how they will normally be used, but it is not something the language requires.

So it's normal to use types that are struct or class within a struct or class . In a way you already do that. A int is like a struct very simple, so simple that it does not even need to be declared like this, but deep down it is the same thing.

Actually it has a lot of other things that are very weird in the code. It works, but it does not 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 need to do everything correct. Curiously, it may have other members who should be (smart) pointers. And then there are other issues that will make the code behave wrong.

In any language the error attempt does not work very well, but they give a reasonable hit rate when compiling. In C ++ it does not work. The hit rate is low when you are not deeply aware of what you are doing. It is very easy to run and be wrong and the problem only appears a lot later.

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

    
17.12.2017 / 10:51