Can someone help me please? I need help writing this code using C++ language . S
ID: 3847767 • Letter: C
Question
Can someone help me please? I need help writing this code using C++ language.
Step 1 The Char Type Create a complex type called Char that models the primitive type char. In your class you are going to want to have the following constructors: Java Description Char0 Char0 Default constructor that sets the data section of the class to null (binary 0) overloaded constructor that takes a primitive char as an argument. It should set the data Char (char c) Char(char c) section of the class to the argument. Char(int c) Chamant Overloaded constructor that takes a primitive int as a parameter. The data section of the c) class should be set as a character from the argument. Char(const Char(inal overloaded constructor that takes the complex Char type as a parameter. The data section Char &c;) Char c) of the class should be set with the data section of the argument Char(string Char (String overloaded Constructor that takes a string type as a parameter The data section of the class should be set to the first character in the string. Your class should have the following mutators.Explanation / Answer
Covered all points of step 1:
#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
using namespace std;
class Char{
char ch;
public:
// Accessors
char toChar(){
return ch;
}
int toInt(){
return ch - '0';
}
string toString(){
string s(1,ch);
return s;
}
string toHexString(){
std::ostringstream os;
os << std::hex << ch;
std::string hexString = os.str();
return hexString;
}
// Mutators
void equals(Char &c){
ch = c.toChar();
}
void equals(char c){
ch = c;
}
void equals(int c){
ch = '0'+c;
}
// Constructors
Char(){}
Char(char c){ ch = c;}
Char(int c){ ch = '0'+c;}
Char(Char &c){ ch = c.toChar();}
Char(string c){ ch = c.c_str()[0];}
// Operator overloading
string operator+(char c) {
string s1(1,ch);
string s2(1,c);
return s1 + s2;
}
// Operator overloading
string operator+(Char &c) {
string s1(1,ch);
string s2(1,c.toChar());
return s1 + s2;
}
};
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.