Print person1's kids, apply the IncNumKids() function, and print again, outputting text as below. End each line with a newline.
Sample output for the below program:
Kids: 3 New baby, kids now: 4

Sample program:
// public class PersonInfo { private int numKids; public void setNumKids(int personsKids) { numKids = personsKids; return; } public void incNumKids() { numKids = numKids + 1; return; } public int getNumKids() { return numKids; } } //

Respuesta :

Answer:

// Program in C++

#include <iostream>

using namespace std;

class PersonInfo {

  private:

     int numKids;

  public:

     void setNumKids(int personsKids) {

        numKids = personsKids;

        return;

     }

     void incNumKids() {

        numKids = numKids + 1;

        return;

     }

     int getNumKids() {

        return numKids;

     }

};

int main() {

  PersonInfo person1;

  person1.setNumKids(3);

  cout << "Kids: " << person1.getNumKids() << endl;

  person1.incNumKids();

  cout << "New baby, kids now: " << person1.getNumKids() << endl;

  return 0;

}

Explanation:

  • Inside the main function, create an object of the PersonInfo class.
  • Call the setNumKids method with the help of person1 object and pass 3 as an argument.
  • Display the current number of kids using the getNumKids method.
  • Increment the number of kids by calling the incNumKids method.
  • Finally print the updated number of kids by calling the getNumKids method again.

Output:

Kids: 3

New baby, kids now: 4

ACCESS MORE
EDU ACCESS