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

  • Thread starter Thread starter ineedhelpnow
  • Start date Start date
  • Tags Tags
    C++ Classes
Click For 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
 
Anthropic announced that an inflection point has been reached where the LLM tools are good enough to help or hinder cybersecurity folks. In the most recent case in September 2025, state hackers used Claude in Agentic mode to break into 30+ high-profile companies, of which 17 or so were actually breached before Anthropic shut it down. They mentioned that Clause hallucinated and told the hackers it was more successful than it was...

Similar threads

  • · Replies 35 ·
2
Replies
35
Views
4K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
Replies
89
Views
6K
  • · Replies 1 ·
Replies
1
Views
8K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
Replies
12
Views
3K
  • · Replies 17 ·
Replies
17
Views
2K