C++ Setters 不为导数赋值 类
C++ Setters not assigning values to derivative classes
我的设置器没有设置。我已经解决这个问题大约一个半星期了。就像一切就绪,代码编译,看起来一切正常,但我的设置器没有设置,即使在从派生对象正确实例化两个不同的对象之后也是如此:
#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;
}
它并没有真正创建输出,当我在派生的 类 中对某些赋值进行 BREAK 时,程序终止,就好像没有任何赋值一样。我确保局部变量接受分配的值,但它不喜欢实例化的东西。
编辑:需要缩短不必要的代码。
Getter Setter 正在工作。您没有在 main 中调用 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;
}
我的设置器没有设置。我已经解决这个问题大约一个半星期了。就像一切就绪,代码编译,看起来一切正常,但我的设置器没有设置,即使在从派生对象正确实例化两个不同的对象之后也是如此:
#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;
}
它并没有真正创建输出,当我在派生的 类 中对某些赋值进行 BREAK 时,程序终止,就好像没有任何赋值一样。我确保局部变量接受分配的值,但它不喜欢实例化的东西。
编辑:需要缩短不必要的代码。
Getter Setter 正在工作。您没有在 main 中调用 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;
}