Use Speaker Class that prints an error of name InvalidSpeakerNumber if the numbe
ID: 2246930 • Letter: U
Question
Use Speaker Class that prints an error of name InvalidSpeakerNumber if the number of identification is not within the range 0 to 999. Implement a main program that shows that the exception class condition it works.
#include <string>
template <class ItemType>
class Speaker
{
private:
string name;
string phone;
string topic;
string fee;
string idnum;
public:
//**************************************Constructor**************************************
Speaker();
Speaker(string aName, string aPhone, string aTopic, string aFee, string anIDnum);
~Speaker();
//**************************************Setters******************************************
void setName(string aName);
void setPhone(string aPhone);
void setTopic(string aTopic);
void setFee(string aFee);
void setIDnum(string anIDnum);
//**************************************Getters******************************************
string getName() const;
string getPhone() const;
string getTopic() const;
string getFee() const;
string getID() const;
};
//Cpp
template <class ItemType>
Speaker<ItemType> ::Speaker() : name("Juan del Pueblo"), phone("000-000-0000"),
topic("No Topic"), fee("0.0"), idnum("0")
{
}// end default constructor
template <class ItemType>
Speaker<ItemType> ::Speaker(string aName, string aPhone, string aTopic, string afee, string anIDnum)
{
setName(aName);
setPhone(aPhone);
setTopic(aTopic);
setFee(afee);
setIDnum(anIDnum);
}
template <class ItemType>
void Speaker<ItemType> ::setName(string aName)
{
name = aName;
}
template <class ItemType>
string Speaker<ItemType> ::getName() const {
return name;
}
template <class ItemType>
void Speaker<ItemType> ::setPhone(string aPhone) {
phone = aPhone;
}
template <class ItemType>
string Speaker<ItemType> ::getPhone() const {
return phone;
}
template <class ItemType>
void Speaker<ItemType> ::setTopic(string aTopic) {
topic = aTopic;
}
template <class ItemType>
string Speaker<ItemType> ::getTopic() const {
return topic;
}
template <class ItemType>
void Speaker<ItemType> ::setFee(string aFee) {
fee = aFee;
}
template <class ItemType>
string Speaker<ItemType> ::getFee() const {
return fee;
}
template <class ItemType>
void Speaker<ItemType> ::setIDnum(string anIDnum) {
idnum = anIDnum;
}
template <class ItemType>
string Speaker<ItemType> ::getID() const {
return idnum;
}
#endif
Explanation / Answer
Well since most of the program you already know, I will just tell you the needful functions (as in C++) and their implementation.
#include<iostream>
#include<stdio>
class Speaker{
void idNumTest(string anIDnum){ // method to check the given id number
try{
int idnum = stoi(anIDnum) ;
if(idnum < 0 || idnum > 999){
throw "InvalidSpeakerNumber" ;
}
else{
//you can continue here with the necessary function you want
}
}
catch(char *c){ // Exception is a superclass of all kinds of exception classes
cout<<c
}
}
void main(){
string anIDnum;
idNumTest(anIDnum); // will call the function to check the necessary value
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.