Search code examples
c++getter-setterderived-class

C++ Setters not assigning values to derivative classes


My setters are not setting. I've worked this problem on an off for about a week and a half now. It's like everything falls into place, code compiles and seems like everything is working hunky-dory, but my setters are not setting, even after correctly instantiating two different objects from the derivatives:

#include <iostream>
#include <conio.h> // _getch()
#include <string>
#include <ctype.h>

using namespace std;

#define TMSG_LEN_PRICE 0.02 // Price of Text Message


// 1st Derived Class, Text Message
class tMsg
{
private:
    float textMessageInCharacters;
public:
    tMsg() {
        textMessageInCharacters = 0.0f;
    }
    // Getter
    int getTextMessageLength()
    {
        return textMessageInCharacters;
    }

    // Setter
    void setTextMessageLength(int textLength)
    {
        textMessageInCharacters = textLength;
    }
};

int main()
{
    string usrInput;   // User Input
    string tMessage; // Text message string
    int vMessage_Min;    // Voice message length in minutes

    try
    {

        tMsg textService; // Text messaging service

        // Menu output for user.
        printf("Select an option:\n\nT. Text Message\nV. Voice Message\n\nOption: ");
        getline(cin, usrInput);

        // Get text message from user
        if (usrInput == "t") {
            printf("Input your message then hit [ENTER] when done: \nMessage: ");
            getline(cin, tMessage);
            textService.setTextMessageLength(tMessage.size());

        }
    }

    // Catch exception
    catch (exception& ex) {
        // Throw exception
        cout << ex.what();

        // Pause screen wait for user input
        _getch();
    }

    // Exit
    return 0;

}

It doesn't really create an output and when I BREAK on certain assignments within the derived classes, the program terminates as if nothing is being assigned. I ensure that the local variables are taking in the assigned values, but something with the instantiation it does not like.

Edit: Shortened Uneccessary Code needed.


Solution

  • Getter Setter were working. You were not calling getter in main. Hope it will help you!

    #include <iostream>
    #include <conio.h> // _getch()
    #include <string>
    #include <ctype.h>
    
    using namespace std;
    
    #define VMSG_LEN_PRICE 0.03 // Price of Voice Message
    #define TMSG_LEN_PRICE 0.02 // Price of Text Message
    
    // Base Class
    class qMsg
    {
    private:
        float messagePrice; // Final Return Price of Messages Voice/Text
    public:
        qMsg() {
            messagePrice = 0.0f;
        }
    
        // Getter
        int getMessagePrice()
        {
            return messagePrice;
        }
    
        // Setter
        void setMessagePrice(float price)
        {
            messagePrice = price;
        }
    };
    
    // 1st Derived Class, Text Message
    class tMsg : public qMsg
    {
    private:
        float textMessageInCharacters;
    public:
        tMsg() {
            textMessageInCharacters = 0.0f;
    
        }
    
        // Setter
        void setTextMessageLength(int textLength)
        {
            textMessageInCharacters = textLength;
    
        }
        // Getter
        int getTextMessageLength()
        {
            return textMessageInCharacters;
        }
    
    };
    
    // 2nd Derived Class, Voice Message
    class vMsg : public qMsg
    {
    private:
        float voiceMessageInMinutes;
    public:
        vMsg() {
            voiceMessageInMinutes = 0.0f;
        }
        // Getter
        int getVoiceMessageLength()
        {
            return voiceMessageInMinutes;
        }
    
        // Setter
        void setVoiceMessageLength(int voiceLength)
        {
            voiceMessageInMinutes = voiceLength;
        }
    
    };
    
    int main()
    {
        string usrInput;   // User Input
        string tMessage; // Text message string
        int vMessage_Min;    // Voice message length in minutes
    
        try
        {
            qMsg commServices; // Voice messaging service
            tMsg textService; // Text messaging service
    
                              // Menu output for user.
            printf("Select an option:\n\nT. Text Message\nV. Voice Message\n\nOption: ");
            getline(cin, usrInput);
    
            // Get text message from user
            if (usrInput == "t") {
                printf("Input your message then hit [ENTER] when done: \nMessage: ");
                getline(cin, tMessage);
                textService.setTextMessageLength(tMessage.size());
                cout << "GET MESSAGE LENGTH :  ";
                cout<<textService.getTextMessageLength();
                cout << endl;        
    
    
            }
            else if (usrInput == "v")
            {
                // Return minutes for voice message
                printf("Input the length of your voice message: ");
                cin >> vMessage_Min;
    
            }
    
        }
    
        // Catch exception
        catch (exception& ex) {
            // Throw exception
            cout << ex.what();
    
            // Pause screen wait for user input
            _getch();
        }
    
        // Exit
        system("pause");
        return 0;
    
    }