SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,kenrick Mock
Chapter:
Inheritance
Exercise:
Programming Projects
Question:8 | ISBN:9780132846813 | Edition: 5

Question

Create a class for a simple blog. The owner of the blog should be able to (a) post a new message, (b) numerically list and display all messages, and (c) select a specific message and delete it.

Viewers of the blog should only be able to numerically list and display all posted messages.

Create a class Viewer and a class Owner that uses inheritance to help implement the blog functionality. Store the data messages using any format you like (a vector of type string may be easiest). A menu function should be implemented for each class that outputs the legal choices for the type of user. To test your classes, the main function of the program should allow the user to invoke the menus from the Viewer and Owner objects.


TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

PROGRAM CODE:

#include <iostream>
#include <vector>
#include <string>
using namespace std;

class Blog {
protected:
    vector<string> messages;

public:
    // Function to post a new message
    void postMessage(const string& message) {
        messages.push_back(message);
    }

    // Function to list and display all messages
    void listMessages() const {
        int count = 1;
        for (const string& message : messages) {
            cout << count << ". " << message << endl;
            count++;
        }
    }

    // Function to delete a specific message
    void deleteMessage(int index) {
        if (index >= 1 && index <= messages.size()) {
            messages.erase(messages.begin() + index - 1);
        } else {
            cout << "Invalid message index!" << endl;
        }
    }
};

class Viewer : public Blog {
public:
    // Function to access the menu for viewers
    void viewerMenu() {
        int choice;
        do {
            cout << "Viewer Menu:" << endl;
            cout << "1. List all messages" << endl;
            cout << "2. Display all messages" << endl;
            cout << "3. Exit" << endl;
            cout << "Enter your choice: ";
            cin >> choice;

            switch (choice) {
                case 1:
                    listMessages();
                    break;
                case 2:
                    displayAllMessages();
                    break;
                case 3:
                    cout << "Goodbye!" << endl;
                    break;
                default:
                    cout << "Invalid choice. Try again." << endl;
            }
        } while (choice != 3);
    }

    // Function to display all messages
    void displayAllMessages() const {
        cout << "All Messages:" << endl;
        listMessages();
    }
};

class Owner : public Blog {
public:
    // Function to access the menu for owners
    void ownerMenu() {
        int choice;
        do {
            cout << "Owner Menu:" << endl;
            cout << "1. Post a new message" << endl;
            cout << "2. List all messages" << endl;
            cout << "3. Display all messages" << endl;
            cout << "4. Delete a message" << endl;
            cout << "5. Exit" << endl;
            cout << "Enter your choice: ";
            cin >> choice;

            switch (choice) {
                case 1:
                    postNewMessage();
                    break;
                case 2:
                    listMessages();
                    break;
                case 3:
                    displayAllMessages();
                    break;
                case 4:
                    deleteMessage();
                    break;
                case 5:
                    cout << "Goodbye!" << endl;
                    break;
                default:
                    cout << "Invalid choice. Try again." << endl;
            }
        } while (choice != 5);
    }

    // Function to post a new message
    void postNewMessage() {
        string message;
        cout << "Enter your new message: ";
        cin.ignore(); // Ignore newline character from previous input
        getline(cin, message);
        postMessage(message);
    }

    // Function to display all messages
    void displayAllMessages() const {
        cout << "All Messages:" << endl;
        listMessages();
    }

    // Function to delete a message
    void deleteMessage() {
        int index;
        cout << "Enter the index of the message to delete: ";
        cin >> index;
        deleteMessage(index);
    }
};

int main() {
    Blog blog;
    Viewer viewer;
    Owner owner;

    int userChoice;
    do {
        cout << "Choose user type:" << endl;
        cout << "1. Viewer" << endl;
        cout << "2. Owner" << endl;
        cout << "3. Exit" << endl;
        cout << "Enter your choice: ";
        cin >> userChoice;

        switch (userChoice) {
            case 1:
                viewer.viewerMenu();
                break;
            case 2:
                owner.ownerMenu();
                break;
            case 3:
                cout << "Goodbye!" << endl;
                break;
            default:
                cout << "Invalid choice. Try again." << endl;
        }
    } while (userChoice != 3);

    return 0;
}

 

OUTPUT:

Choose user type:
1. Viewer
2. Owner
3. Exit
Enter your choice: 1
Viewer Menu:
1. List all messages
2. Display all messages
3. Exit
Enter your choice: 1
1. Hello, this is a test message.
2. Welcome to the blog!
3. Another message here.
Viewer Menu:
1. List all messages
2. Display all messages
3. Exit
Enter your choice: 2
All Messages:
1. Hello, this is a test message.
2. Welcome to the blog!
3. Another message here.
Viewer Menu:
1. List all messages
2. Display all messages
3. Exit
Enter your choice: 3
Goodbye!
Choose user type:
1. Viewer
2. Owner
3. Exit
Enter your choice: 2
Owner Menu:
1. Post a new message
2. List all messages
3. Display all messages
4. Delete a message
5. Exit
Enter your choice: 1
Enter your new message: This is a new message from the owner.
Owner Menu:
1. Post a new message
2. List all messages
3. Display all messages
4. Delete a message
5. Exit
Enter your choice: 2
1. Hello, this is a test message.
2. Welcome to the blog!
3. Another message here.
4. This is a new message from the owner.
Owner Menu:
1. Post a new message
2. List all messages
3. Display all messages
4. Delete a message
5. Exit
Enter your choice: 4
Enter the index of the message to delete: 3
Owner Menu:
1. Post a new message
2. List all messages
3. Display all messages
4. Delete a message
5. Exit
Enter your choice: 2
1. Hello, this is a test message.
2. Welcome to the blog!
3. This is a new message from the owner.
Owner Menu:
1. Post a new message
2. List all messages
3. Display all messages
4. Delete a message
5. Exit
Enter your choice: 5
Goodbye!
Choose user type:
1. Viewer
2. Owner
3. Exit
Enter your choice: 3
Goodbye!

 

0 0

Discussions

Post the discussion to improve the above solution.