我想制作一个包含 3 个文件的程序。 “Persona.hpp” “Persona.cpp” “main.cpp” 我不确定它是如何完成的。
这是“Persona.hpp”
#ifndef __PERSONA_HPP
#define __PERSONA_HPP
#include <string>
using namespace std;
class Persona{
public:
Persona();
void setNombre(string N);
void setFechaNac(string F);
void setPeso(float P);
void setEstatura(float E);
string getNombre(void);
string getFechaNac(void);
float getPeso(void);
float getEstatura(void);
void mostrarDat(void);
private:
string nombre;
string fechaNac;
float peso;
float estatura;
};
#endif
“Persona.cpp”
#include "Persona.hpp"
#include <iostream>
#include <string>
using namespace std;
Persona::Persona(){
nombre = "";
fechaNac = "";
peso = estatura = 0;
}
void Persona::setNombre(string N){
nombre = N;
}
void Persona::setFechaNac(string F){
fechaNac = F;
}
void Persona::setPeso(float P){
peso = (P>1 && P<500) ? P : 0;
}
void Persona::setEstatura(float E){
estatura = (E>30 && E<280) ? E : 0;
}
string Persona::getNombre(void){
return nombre;
}
string Persona::getFechaNac(void){
return fechaNac;
}
float Persona::getPeso(void){
return peso;
}
float Persona::getEstatura(void){
return estatura;
}
void Persona::mostrarDat(){
cout << "\nNombre: " << getNombre();
cout << "\nFecha de nacimiento: " << getFechaNac();
cout << "\nPeso: " << getPeso() << " Kg";
cout << "\nEstatura: " << getEstatura() << " Cm";
}
“main.cpp”:
#include "Persona.hpp"
#include <iostream>
using namespace std;
int main(){
Persona humano;
cout << "\nConstructor default: ";
humano.mostrarDat();
humano.setNombre("Jose Ramon Garibay Alvarez");
humano.setFechaNac("27 Octubre de 1989");
humano.setPeso(80);
humano.setEstatura(175.5);
cout << "\n\nEstableciendo datos validos: ";
humano.mostrarDat();
humano.setNombre("Karina Nogueira Briseno");
humano.setFechaNac("15 Agosto de 1985");
humano.setPeso(0.457);
humano.setEstatura(17);
cout << "\n\nEstableciendo datos Invalidos: ";
humano.mostrarDat();
return 0;
}
首先我得到一个错误:http://oi40.tinypic.com/2v96quo.jpg 我不知道我是否正确地处理了“#including”文件。我记得我的教授使用了类似“#ifndef SOMETHING”的东西,但我不知道是否有必要,谢谢你的回答! :)
最佳答案
使用 #include "Persona.hpp"
而不是 #include <Persona.hpp>
关于c++ - 不同文件中的头文件、函数定义、main,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21132661/