我正在学习C++中的继承,并尝试从函数“age”中返回值。我回来的只有0个。我花了好几个小时才弄清楚,但没有运气。这是我的代码如下。我将不胜感激任何帮助!无法返回函数的值
.H类
#include <stdio.h>
#include <string>
using namespace std;
class Mother
{
public:
Mother();
Mother(double h);
void setH(double h);
double getH();
//--message handler
void print();
void sayName();
double age(double a);
private:
double ag;
};
的.cpp
#include <iostream>
#include <string>
#include "Mother.hpp"
#include "Daughter.hpp"
using namespace std;
Mother::Mother(){}
Mother::Mother(double h)
{
ag=h;
}
void setH(double h){}
double getH();
void Mother::print(){
cout<<"I am " <<ag <<endl;
}
void Mother::sayName(){
cout<<"I am Sandy" <<endl;
}
double Mother::age(double a)
{
return a;
}
主要
#include <iostream>
#include "Mother.hpp"
#include "Daughter.hpp"
using namespace std;
int main(int argc, const char * argv[]) {
Mother mom;
mom.sayName();
mom.age(40);
mom.print();
//Daughter d;
//d.sayName();
return 0;
是母亲::年龄应该设置'this-> ag'?现在你只需返回它。 – Falmarri
对不起,我不明白你的意见,请你详细说明一下吗?谢谢! – familyGuy
这听起来像是如果你不了解我的评论,你需要回头继续学习C++。在了解继承之前,您应该了解对象,变量,值和方法。 – Falmarri