PIC 10A Homework 9 Solution

PIC 10A Homework 9

Goals

To learn about pointers, dynamic memory allocation and data structures managed by pointers.

Your Assignment

Your assignment is exercise P7.1 on page 337. I have reproduced it here for your convenience. Implement class Person with the following fields:

  • name (string)
  • best_friend (Person *), a pointer to a persons best friend
  • popularity (int), a popularity counter that indicates how many other people have this person as their best friend.

Then write a program that gets names as input from the user (Read from the console using cin. You may tell the user to enter a name or q to quit entering names.) , allocates a new Person for each of them and stores the pointer to each of them in a vector . Then ask the user for the name of the best friend for each of the Person objects. Locate the object matching the friend's name and call a set_best_fiend method to update the best_friend pointer and the popularity counter. Finally, print out all Person objects , listing the name, best friend and popularity counter for each.

Other instructions

The main difficulty in this program is that you are dealing with pointers to Person objects. You are not directly dealing with Person objects. Keep this in mind as you write your code. Before you access any pointer, make sure it is not NULL. Write if statements as necessary to test for this. A persons popularity goes up if they are someones best friend. If you have a best friend that does not increase YOUR popularity. You need to write all necessary member functions for the class.

Note: This is not an assignment about separate compilation. You may write everything into the file hw9.cpp including your class declaration and definition.

What to Turn in.
Upload to ccle the source file hw9.cpp

Grading

Person class

Correctly implemented

4 points

Program functions as described

Correctly implemented

10 points

Program compiles

Compiles with no errors

4 points

Style

Variable names, comments, indentation, no global variables, uses functions wisely

2 points

 

TOTAL

20 points

 

    • Posted: 5 years ago
    Complete A++ Solution

    Purchase the answer to view it

    blurred-text
    Save time and money!
    Our teachers already did such homework, use it as a reference!