Doing a job of assigning names , age , code and note when I try to assign an inheritance to then a set
with AlumnoH
, I get an error. I think it may be the way I'm inheriting it but I really don't have much idea, the error says that a '{'
(yes, sorry, this error was given to me in the void line StudentH::setValue2(string _name, int _age , string _codigo, float _notafinal) : Person(_name, _age){}, right where it says : Person(_name, _age), exactly in the ':') so that it is correct, but I don't think that is the problem. In an example I saw they didn't need that.
If you could recommend me something to review that part, it would be more than great. Thank you!
inheritance.CPP
#include <iostream>
#include <string>
#include "herencia.h"
using namespace std;
Persona::Persona(){}
void Persona::setValue(string _name, int _age){
this-> name = _name;
this-> age = _age;
}
const string Persona::getName() const{
return name;
}
const int Persona::getAge() const{
return age;
}
void AlumnoH::setValue2(string _name, int _age, string _codigo, float _notafinal) : Persona(_name, _age){
this-> codigo = _codigo;
this-> notafinal = _notafinal;
}
Persona::~Persona(){}
inheritance.h
#include <iostream>
#include <string>
using namespace std;
class Persona{
private:
string name;
int age;
public:
Persona();
virtual ~Persona();
Persona(string, int);
virtual ~Persona();
void setValue(string _name, int _age);
const string getName() const;
const int getAge() const;
};
class AlumnoH : public Persona{
private:
string codigo;
float notafinal;
public:
AlumnoH();
virtual ~AlumnoH();
void setValue2(string, int, string, float) : Persona(string, int);
const string getName() const;
const int getAge() const;
const string getCodigo() const;
const float getNotafinal() const;
};
main.cpp
#include <iostream>
#include "herencia.h"
using namespace std;
template<typename T>
void print(T objeto){
cout << objeto.getName() << endl;
cout << objeto.getAge() << endl;
}
int main(){
Persona P1;
P1.setValue("Hernan", 12);
print(P1);
}
Based on the information from this compiler :
You are declaring the destructor 2 times:
You can delegate constructors but not functions. What you can do is call parent functions like so:
Also, there is no constructor
Persona(string, int)
.Delegating a function is an implementation detail. So you should not write the delegation as part of the declaration. For example, you add a constructor to
Persona
:In fact, since the declaration only needs a name, parameters, and return type, if you write it like:
The compiler interprets that you are giving a definition. That's why he says
expected '{' or ','
. Because it expects the body of the function.The file
herencia.h
would look like this:The error is that you do an inheritance in the method:
This receives parameters, but ideally it would be to send those parameters to the constructor directly;
I see that you have the empty person constructor and you are inheriting the person constructor to a method, which causes the inheritance error, the ideal would be the following:
It is the most recommended thing to do