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

  • C/C++
  • Thread starter ineedhelpnow
  • Start date
  • Tags
    C++ Classes
In summary, the program uses a PersonInfo class to set and get the number of kids a person has. By using the IncNumKids() function, the number of kids is incremented when a new baby is born, and the updated number is printed using GetNumKids().
  • #1
ineedhelpnow
651
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
  • #2
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)
 
  • #3
Code:
cout << "Kids: " <<person1.GetNumKids()<< endl;
PersonInfo IncNumKids;
person1.IncNumKids();
cout << "New baby, kids now: " <<person1.GetNumKids() <<endl;

it worked :eek:
 

Related to C++ Classes: Print and Update Person's Kids

What is a class in C++?

A class in C++ is a user-defined data type that contains both data members (variables) and member functions (methods) to manipulate those data members. It provides a blueprint or template for creating objects which are instances of the class.

How do you declare a class in C++?

To declare a class in C++, you use the "class" keyword followed by the name of the class and a pair of curly braces. Inside the curly braces, you can define the data members and member functions of the class. For example:

class Car {  private:    string make;    string model;    int year;  public:    void drive();    void stop();};

What is the difference between a member function and a regular function in C++?

A member function is a function that is associated with a specific class and can only be called by objects of that class. It has access to the private data members of the class. On the other hand, a regular function is not associated with any class and can be called from anywhere in the program. It does not have access to private data members unless they are passed as parameters.

How do you define a member function outside of a class in C++?

To define a member function outside of a class in C++, you use the scope resolution operator (::) to specify the name of the class followed by the name of the function. For example:

void Car::drive() {  // code to implement the drive function}
Remember to include the class name before the function name and to use the class name before the scope resolution operator.

What is encapsulation in C++ classes?

Encapsulation in C++ classes refers to the ability to hide the implementation details of a class and only expose a public interface to the user. This allows for better data protection and helps to prevent unintended modifications to the data members of a class. It also allows for easier maintenance and modification of the code in the future. The private data members can only be accessed through public member functions, providing a level of control over how the data is manipulated.

Similar threads

  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
2
Replies
35
Views
2K
  • Programming and Computer Science
Replies
13
Views
2K
  • Programming and Computer Science
Replies
23
Views
1K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
3
Replies
89
Views
4K
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
23
Views
2K
  • Programming and Computer Science
Replies
5
Views
2K
Back
Top