Create a class called Character that models the char primitive type. Your class
ID: 3571904 • Letter: C
Question
Create a class called Character that models the char primitive type. Your class should hold a char as the data section. This class will be used to provide information about a char and convert it to other types
Functionality
Constructors
• Default - Sets the data section to the space character ' '
• Overloaded - Sets the data section to a value passed as an argument.
Mutators
• setChar - sets the data section to a value passed as an argument Accessors
• getChar -returns the data section as a char type
• getInt -returns the data section as an int type
• getString -returns the data section as a string type
• toUpper -returns the data section as an upper case character
• toLower -returns the data section as a lower case character.
Please Answer in C++
Explanation / Answer
#include <iostream>
using namespace std;
class Character {
public:
char data;// data-section to store the value
Character();//default constructor
Character(char a);//parameterized constructor
void setChar( char a );// set char mutator
char getChar();//accessor
int getInt();// to convert char to int
string getString();// to convert char to string
char toUpper();// get the upper Case of char
char toLower();//get the lower case of char
};
// Member functions definitions including constructor
Character::Character(void) {
data=' ';
}
Character::Character(char a) {
data=a;
}
Character::setChar(char a) {
data=a;
}
char Character::getChar() {
return data;
}
int Character::getInt() {
return (int)data;
}
string Character::getString() {
return string(1, data);
}
char Character::toUpper() {
return toupper(data);
}
char Character::toLower() {
return tolower(data);
}
int main()
{
Character chars1;
Character chars2(d);
cout<<"toLower: "<<chars2.toLower()<<endl;
cout<<"toUpper: "<<chars2.toUpper()<<endl;
cout<<"getInt: "<<chars2.getInt()<<endl;
cout<<"getString: "<<chars2.getString()<<endl;
return 0;
}
-----------output--------
toLower: d
toUpper: D
getInt: 100
getString: d
-----------output--------
input-d(small case d)
Note: You can play around the class to invoke differnet method.Feel free to ask doubts/question. God bless you
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.