Write C++ code to create a class for a cell phone service called DigitalMessage.
ID: 3914771 • Letter: W
Question
Write C++ code to create a class for a cell phone service called DigitalMessage. The class must include a field for the price of the message plus get and set methods for that field. Then, DERIVE two new classes from the DigitalMessage class. The first class must be called VoiceMail and the second class must be called TextMessage. Include a default constructor for each class (you don't need any parameterized constructors). The VoiceMail class must contain a float field for length of the message in minutes, as well as get and set methods for that field. The set method must populate the price of the message based on the length of the message: 10 cents per minute (be sure to use a named constant). The TextMessage class must contain an int field for the number of characters in the message, as well as get and set methods for that field. The set method must populate the price of the message based on the length of the message: 3 cents per character (be sure to use a named constant). Write a program that instantiates at least one object from each of the two derived classes. Include code and output to demonstrate that your classes and all of the get/set methods are working properly.
Explanation / Answer
Given below is the code for the question.
Please do rate the answer if it was helpful. Thank you
#include <iostream>
#include <string>
using namespace std;
class DigitalMessage{
private:
double price;
public:
double getPrice(){
return price;
}
void setPrice(double p){
price = p;
}
};
class VoiceMail :public DigitalMessage{
private:
int length; // length in minutes
constexpr static double COST_PER_MINUTE = 0.10; //10 cents
public:
VoiceMail(){
setLength(0);
}
int getLength(){
return length;
}
void setLength(int m){
length = m;
setPrice(length * COST_PER_MINUTE);
}
};
class TextMessage :public DigitalMessage{
private:
constexpr static double COST_PER_CHAR = 0.03; // 3 cents
int numChars; // no. of characters in message
public:
TextMessage(){
setNumChars(0);
}
int getNumChars(){
return numChars;
}
void setNumChars(int n){
numChars = n;
setPrice(numChars * COST_PER_CHAR);
}
};
int main(){
VoiceMail vm;
TextMessage tm;
cout << "setting length of voicemail to 2 mins" << endl;
vm.setLength(2); // 2 mins is the lenght of voice mail
cout << "setting the no. of characters for text message to 60" << endl;
tm.setNumChars(60);
cout << "Details of voice mail" << endl;
cout << " Length: " << vm.getLength() << " mins" << endl;
cout << " Price: $" << vm.getPrice() << endl << endl;
cout << "Details of text message" << endl;
cout << " No. of characters: " << tm.getNumChars() << endl;
cout << " Price: $" << tm.getPrice() << endl;
}
output
=====
setting length of voicemail to 2 mins
setting the no. of characters for text message to 60
Details of voice mail
Length: 2 mins
Price: $0.2
Details of text message
No. of characters: 60
Price: $1.8
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.