0

I am learning inheritance in C++ and trying to return value from a function "age". All I get back is 0. I have spent hours to figure out, but no luck. This is my code below. I'll greatly appreciate any help on this!

.h class

#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;
}

main

#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;
familyGuy
  • 425
  • 2
  • 5
  • 22
  • 1
    Is Mother::age supposed to set `this->ag`? Right now you just return it. – Falmarri Sep 23 '16 at 23:58
  • I'm sorry, i did not understand your comments, can you please elaborate? Thanks! – familyGuy Sep 24 '16 at 00:01
  • 1
    It sounds like you need to go back further in learning C++ if you don't understand my comment. You should learn about objects, and variables, and values, and methods before you learn about inheritance. – Falmarri Sep 24 '16 at 00:02
  • Off topic: `using namespace std;` in a header is often a bad idea. Everyone who includes the header now has to de3al with the fallout. What fallout? Read more here: http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice – user4581301 Sep 24 '16 at 00:07
  • I'll definitely read and go by the guidelines in future. Thanks! – familyGuy Sep 24 '16 at 00:08
  • 1
    Off topic: Mother.hpp lacks include guards. This can wreak untold havoc. More here: http://stackoverflow.com/questions/21090041/why-include-guards and here: http://stackoverflow.com/questions/8020113/c-include-guards – user4581301 Sep 24 '16 at 00:09

3 Answers3

3

Your mom.print() does this:

cout<<"I am " <<ag <<endl;

So the problem here is: ag = 0

Your mom.age(40) does this:

return a;

See, it doesn't save your mom's age to your mom variable, it only return what you pass ( here is 40 ), so how could it print ?

Therefore, there are many ways to solve this, if you want to return your mom's age, do cout << mom.age(40) in main() Or, just:

void Mother::age(double a)
{
    ag = a;
}
Loi Ly
  • 164
  • 12
2

function age doesn't assign value a to the member ag but instead it returns the value a which it takes as parameter it's such a bad thing. to get what I want to say in main write:

cout << mom.age(40) << endl; // 40

to make it correct change you function age to:

double Mother::age(double a)
{
    ag = a;
    return a; // it's redundant to do so. change this function to return void as long as we don't need the return value
}

*** another thing you should do:

make "getters" const to prevent changing member data and only let "setters" not constant. eg in your code: class mother:

double getH()const; // instead of double getH() const prevents changing member data "ag" 
Raindrop7
  • 3,889
  • 3
  • 16
  • 27
2

You have to use correctly setter and getter. Use the setter to change the age like this :

void setAge(const double &_age) {
    ag = _age;
}

If you want to retrieve the value use the getter.

double getAge() const {
    return ag;
 }
doudouremi
  • 186
  • 1
  • 6