Search code examples
c++header-files

C++ Null output when a function is called


Below is a snippet of code from my main program

My H file

class Person{
public:
     std::string name;
     int rangeStance;
     int initialStance;

Person(std::string name, int rangeStance, int initialStance){
     name = name;
     rangeStance = rangeStance;
     initialStance = initialStance;

     setName(getName());
     setRangestance(getRangeStance());
     setinitalStance(getRangeStance());
}
Person();
    void setName(std::string name);
    void setRangestance(int range);
    void setinitalStance(int stance);

    std::string getName();
    int getRangeStance();
    int getinitalStance();
    double impact(int rangeStance, int  initalStance);

};

class Leader: public Person {

    public:
     int popularity;
     int totalcountryVotes;

     Leader(std::string name, int rangeStance, int initialStance,int popularity, int totalcountryVotes)
     :Person(name,  rangeStance, initialStance), popularity(popularity), totalcountryVotes(totalcountryVotes){

          popularity = popularity;
          totalcountryVotes = totalcountryVotes;
          setPopularity(getPopularity());
          setTotalcountryVotes(getTotalcountryVotes());
     }

     Leader();   
     void setPopularity(int popularity);
     void setTotalcountryVotes(int totalcountryVotes);
     int getPopularity(); 
     int getTotalcountryVotes();   
};

The corresponding functions in the main cpp file.

Person::Person() {
}

      void Person::setName(string Name)
        {
           name = Name;
        } 

        string Person::getName() {
              return name;
        }


        void Person::setRangestance(int Range)
        {

      rangeStance = Range;

        }

        int Person::getRangeStance() {
              return rangeStance;
        }

          void Person::setinitalStance(int stance)
        {

            initialStance = stance;                                            

        } 

        int Person::getinitalStance() {
              return initialStance;
        }



Leader::Leader() {

}

    void Leader::setPopularity(int popularity) {

          popularity = popularity;

     }


    void Leader::setTotalcountryVotes(int totalcountryVotes) {
          totalcountryVotes = totalcountryVotes;
     } 


     int Leader::getPopularity() {

           return popularity;
     }


    int Leader::getTotalcountryVotes() {
          return totalcountryVotes;
    }

Within main the needed funtions are called appropriately

int main(int argc, char* argv[]) {

Leader labourLeader("George Lopez",100,50,50, 75);//sets record for the labour party leader

cout << "--Party Leader--" << endl;
cout << labourLeader.getName() << endl;

return 0;
}

However when this snippet of code is compiled, no outcome is returned where it should be printing out "George Lopez". Im fairly "noob" with c++, am i using my contructor right or should I be delcaring it within my h file? Thankyou.


Solution

  • A couple of things wrong in this code

    Person(std::string name, int rangeStance, int initialStance){
        name = name;
        rangeStance = rangeStance;
        initialStance = initialStance;
        setName(getName());
        setRangestance(getRangeStance());
        setinitalStance(getRangeStance());
    }
    

    Firstly it's not necessary to call setters and to do assignments, so lets drop those, leaving

    Person(std::string name, int rangeStance, int initialStance){
        name = name;
        rangeStance = rangeStance;
        initialStance = initialStance;
    }
    

    Now think about what name = name does. Does that look curious to you at all? It takes the parameter name and assigns it to the parameter name! The member variable also called name is completely unchanged. This situation where one name hides another similar name is called shadowing.