Question

In: Computer Science

This class models people moving in together in real life using pointers in C++. What test(s)...

This class models people moving in together in real life using pointers in C++.

What test(s) could be added with the code below? At the end of this task, add them! (Answer this.)

class Person {  
public:       
    Person(const string& name) : name(name) {}
    void movesInWith(Person& newRoomate) {
        roomie = &newRoomate;        // now I have a new roomie            
        newRoomate.roomie = this;    // and now they do too       
    }       
    const string& getName() const { return name; }
    // Don't need to use getName() below, just there for you to use in debugging.
    const string& getRoomiesName() const { return roomie->getName(); }  
private:
    Person* roomie;       
    string name;  
};           

// write code to model two people in this world       
Person joeBob("Joe Bob"), billyJane("Billy Jane");         

// now model these two becoming roommates       
joeBob.movesInWith(billyJane);         

// did this work out? (Answer this and explain why.)      
cout << joeBob.getName() << " lives with " << joeBob.getRoomiesName() << endl;
cout << billyJane.getName() << " lives with " << billyJane.getRoomiesName() << endl;

What changes can be made to the Person class above to keep the methods "safe"? For example, the movesInWith method.

I dont understand what this above question means, please help!

Solutions

Expert Solution

#include <iostream>

using namespace std;


class Person {  
        public:       
                Person(const string& name) : name(name) {
                        
                }
                void movesInWith(Person& newRoomate) {
                        
                        // If the passed person is not me..
                        // Because i can not be roomie of myself.
                        if(&newRoomate != this) {
                                roomie = &newRoomate;        // now I have a new roomie            
                                newRoomate.roomie = this;    // and now they do too       
                        }
                }       
                
                const string& getName() const { return name; }
                
                // Don't need to use getName() below, just there for you to use in debugging.
                const string& getRoomiesName() const { return roomie->getName(); }  
        private:
                Person* roomie;
                string name;  
};           

int main() {
// write code to model two people in this world       
Person joeBob("Joe Bob"), billyJane("Billy Jane");         

// now model these two becoming roommates       
joeBob.movesInWith(billyJane);         

// did this work out? (Answer this and explain why.)      
cout << joeBob.getName() << " lives with " << joeBob.getRoomiesName() << endl;
cout << billyJane.getName() << " lives with " << billyJane.getRoomiesName() << endl;

}
**************************************************
Added more safety in the case, where the same person is asked to be made roomie of itself.
Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.

Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.


Related Solutions

provide an example where people use nonparametric test in either business or real life using customer...
provide an example where people use nonparametric test in either business or real life using customer satisfaction. I need a full example please
c++ Define polymorphism. What is the benefit of using pointers with polymorphic functions?
c++ Define polymorphism. What is the benefit of using pointers with polymorphic functions?
What is the definition of class merger? Class expansion? Give real life examples of each
What is the definition of class merger? Class expansion? Give real life examples of each
this program is to be done in c language. Using Pointers Create a program pointerTester.c to...
this program is to be done in c language. Using Pointers Create a program pointerTester.c to experiment with pointers. Implement the following steps one by one in your program: YOU NEED TO ANSWER QUESTION Use printf to print your answers at the end(after 12). 1. Declare three integer variables a, b and c. Initialize them to 0, 100 and 225, respectively. 2. Print the value of each variable and its address. 3. Add the following declaration to your code: int...
This paper aims to test the module ILOs using a practical real-life case-study. In this case...
This paper aims to test the module ILOs using a practical real-life case-study. In this case study, you are going to play the role of an analyst for one of the corporates, let us call it Company-Z. Therefore, let us first introduce the case-study random variables:  is the Company-Z monthly revenue along the period Pre-COVID-19(January 2018 to December 2019);  is the same Company-Z monthly revenue but during the period Post-COVID-19(March 2020 to September 2020);  is the monthly operations cost during the period Pre-COVID-19(January...
Can you give examples of using functions in real life? By real life I understand not...
Can you give examples of using functions in real life? By real life I understand not only what we do in everyday living, but also science, economy, and similar.
Write a complete C program that searches an element in array using pointers. Please use the...
Write a complete C program that searches an element in array using pointers. Please use the function called search to find the given number. //Function Prototype void search (int * array, int num, int size)
C++ programming test 2, chapters 6,7,& 9 on Functions, Arrays, & Pointers 1. Create a one...
C++ programming test 2, chapters 6,7,& 9 on Functions, Arrays, & Pointers 1. Create a one dimensional array, Ages, which will hold 4 ages. Each age is an int.        b. Write a for loop and print, in reverse order the 4 values stored in memory assuming that the ages in the previous question have already been entered with a space between each value. Use subscript notation.                                     short cnt; c. Do the same as above, but use pointer...
Class Exercise: Constructor using JAVA Let’s define a Class together and have a constructor while at...
Class Exercise: Constructor using JAVA Let’s define a Class together and have a constructor while at it. - What should the Class object represent? (What is the “real life object” to represent)? - What properties should it have? (let’s hold off on the methods/actions for now – unless necessary for the constructor) - What should happen when a new instance of the Class is created? - Question: What are you allowed to do in the constructor? - Let’s test this...
In Visual Studios 2017, using c++, Create a class (Scores) that stores test scores (integers) in...
In Visual Studios 2017, using c++, Create a class (Scores) that stores test scores (integers) in an array (assume a maximum of 100 scores). The class should have a constructor that allows the client to specify the initial value of the scores (the same initial value applies to all of them) and another default constructor that initializes them to 0. The class should have functions as follows: 1. A member function that adds a score to the array.  The client must...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT