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...
Develop a C program for matrix multiplication focusing on using malloc and pointers WITHOUT USING BRACKETS...
Develop a C program for matrix multiplication focusing on using malloc and pointers WITHOUT USING BRACKETS [] !! * DO not use [ ] 'brackets', focus on using malloc, calloc, etc... Program will ask user for the name of the text file to be read Read the datafile with format: 1 2 1 1 2 3 4 So the first three lines will represent m, n, p (Matrix A: m x n ||| Matrix B: n x p), follwing are...
*C PROGRAMMING LANGUAGE* a) Given an array of size n, sort the array using pointers using...
*C PROGRAMMING LANGUAGE* a) Given an array of size n, sort the array using pointers using malloc or calloc. Examples: Input: n = 5, A= {33,21,2,55,4} Output: {2,4,21,33,55} b) Write a function that declares an array of 256 doubles and initializes each element in the array to have a value equal to half of the index of that element. That is, the value at index 0 should be 0.0, the value at index 1 should be 0.5, the value at...
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 program that performs a merge-sort algorithm without using a recursion. Only using pointers. C++...
Write a program that performs a merge-sort algorithm without using a recursion. Only using pointers. C++ programming language; #include<iostream>
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)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT