C/C++ C++ Classes: Print and Update Person's Kids

  • Thread starter Thread starter ineedhelpnow
  • Start date Start date
  • Tags Tags
    C++ Classes
AI Thread Summary
The discussion focuses on how to correctly implement a code snippet for a class that tracks the number of kids a person has. The key points include initializing the number of kids using the SetNumKids method, incrementing the count with the IncNumKids method, and retrieving the updated count with GetNumKids. The correct implementation involves calling person1.IncNumKids() after printing the initial count, followed by printing the updated count. The final output should reflect the initial number of kids and the incremented number after the addition of a new baby. The successful code snippet provided is: cout << "Kids: " << person1.GetNumKids() << endl; person1.IncNumKids(); cout << "New baby, kids now: " << person1.GetNumKids() << endl; This results in the expected output of the number of kids before and after the increment.
ineedhelpnow
Messages
649
Reaction score
0
Print person1's kids, apply the IncNumKids() function, and print again, outputting text as below. End each line with newline.
Sample output for below program:

Kids: 3
New baby, kids now: 4
Sample program:
Code:
#include <iostream>
using namespace std;

class PersonInfo {
   public:
      void   SetNumKids(int personsKids);
      void   IncNumKids();
      int    GetNumKids() const;
   private:
      int    numKids;
};

void PersonInfo::SetNumKids(int personsKids) {
   numKids = personsKids;
   return;
}

void PersonInfo::IncNumKids() {
   numKids = numKids + 1;
   return;
}

int PersonInfo::GetNumKids() const {
   return numKids;
}

int main() {
   PersonInfo person1;

   person1.SetNumKids(3);

   <STUDENT CODE>

   return 0;
}

Below, do not type an entire program. Only type the portion indicated by the above instructions (and if a sample program is shown above, only type the <STUDENT CODE> portion.)

ok so i got this part so far which is correct according to the homework:
Code:
cout << "Kids: " <<person1.GetNumKids()<< endl;
PersonInfo IncNumKids;
cout << "New baby, kids now: " << I DONT KNOW WHAT TO PUT HERE<<endl;
i can't figure out what will display the number afterwards. I am guessing it has to do with IncNumKids but i don't know what.
 
Technology news on Phys.org
ineedhelpnow said:
Print person1's kids, apply the IncNumKids() function, and print again, outputting text as below. End each line with newline.
Sample output for below program:

Kids: 3
New baby, kids now: 4
Sample program:
Code:
#include <iostream>
using namespace std;

class PersonInfo {
   public:
      void   SetNumKids(int personsKids);
      void   IncNumKids();
      int    GetNumKids() const;
   private:
      int    numKids;
};

void PersonInfo::SetNumKids(int personsKids) {
   numKids = personsKids;
   return;
}

void PersonInfo::IncNumKids() {
   numKids = numKids + 1;
   return;
}

int PersonInfo::GetNumKids() const {
   return numKids;
}

int main() {
   PersonInfo person1;

   person1.SetNumKids(3);

   <STUDENT CODE>

   return 0;
}

Below, do not type an entire program. Only type the portion indicated by the above instructions (and if a sample program is shown above, only type the <STUDENT CODE> portion.)

ok so i got this part so far which is correct according to the homework:
Code:
cout << "Kids: " <<person1.GetNumKids()<< endl;
PersonInfo IncNumKids;
cout << "New baby, kids now: " << I DONT KNOW WHAT TO PUT HERE<<endl;
i can't figure out what will display the number afterwards. I am guessing it has to do with IncNumKids but i don't know what.

Hi Pippy! (Smile)Writing [m]person1.GetNumKids()[/m] is like asking person1 how many kids he has.
Then you can print that with [m]cout[/m] as you have. Good! (Nod)When he gets a baby, you need to tell him he has an extra kid by writing [m]person1.IncNumKids()[/m].
That is, that person1 should increment the number of kids he has. (Thinking)Afterwards, you can ask again how many kids he has with [m]person1.GetNumKids()[/m] and print that. (Wasntme)
 
Code:
cout << "Kids: " <<person1.GetNumKids()<< endl;
PersonInfo IncNumKids;
person1.IncNumKids();
cout << "New baby, kids now: " <<person1.GetNumKids() <<endl;

it worked :o
 
Thread 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...

Similar threads

Back
Top