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 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

Similar threads

Back
Top