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
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Back
Top