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...
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)
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.
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...
Write a C++ program (using pointers and dynamic memory allocation only) to implement the following functions...
Write a C++ program (using pointers and dynamic memory allocation only) to implement the following functions and call it from the main function. (1)Write a function whose signature looks like (char*, char) which returns true if the 1st parameter cstring contains the 2nd parameter char, or false otherwise. (2)Create an array of Planets. Populate the array and print the contents of the array using the pointer notation instead of the subscripts.
PART C: A market is a forum in which people come together to exchange ownership of...
PART C: A market is a forum in which people come together to exchange ownership of goods; a place where goods or services are bought and sold. REQUIRED: 2.5 Describe the three (3) models of market competition. (6) 2.6 List three (3) characteristics of a monopolistic market. (3) 2.7 State five (5) unethical practices in oligopolistic markets. (5)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT