C ++设置方法未将值分配给派生类

问题描述 投票:0回答:1

我的设置者未设置。我已经将这个问题解决了大约一个半星期。好像一切都准备就绪,代码已编译,似乎一切都在正常运行,但是即使正确地从派生实例化了两个不同的对象,我的设置器也没有设置:

#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;
    }
    // Getter
    int getTextMessageLength()
    {
        return textMessageInCharacters;
    }

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

// 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());

        }
        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
    return 0;

}

它并没有真正创建输出,当我在派生类中对某些分配进行BREAK时,程序将终止,就像没有分配任何东西一样。我确保局部变量采用指定的值,但是带有实例化的东西不喜欢。

c++ getter-setter derived-class
1个回答
0
投票

Getter Setter正在工作。您不是在主叫getter。希望对您有帮助!

#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;

}
© www.soinside.com 2019 - 2024. All rights reserved.