Using C++ (not C) to create a class, X, consisting of three data members, a char
ID: 3577646 • Letter: U
Question
Using C++ (not C) to create a class, X, consisting of three data members, a char*, an int, and a long. These data members are to be used to store a name (both first and last), an age, and a social security number. Add any necessary functions to your class. You do not have to write a copy constructor for this problem. Write an overloaded insertion operator so that you can print a class object. The overloaded insertion operator should display the members as:
The name should be left justified in a field of width 32.
The age should be right justified in a field of width 5 and displayed in hexadecimal with a base indicator.
The social security number should be left justified in field of width 12, displayed in decimal.
Instantiate a class object using your name, age (you can lie about your age), and social security number with a constructor that looks like this:
X object(“Joe Bentley”,25,123456789);
Use your insertion operator to write the X class object into an output file.
Turn in your program code and the output file.
main() should have only 4 lines, like this:
int main()
{
X object(“Joe Bentley”,25,123456789); // your name, age, ssn
?
?
return 0;
}
Explanation / Answer
Constructor to print the values is:
X::X(char str,int age,long ssn) {
cout <<setw(32)<< left<<"First and last name" <<str<< endl;
cout <<setw(5)<<right<< "Age" <<age<< endl;
cout << setw<<(12)<<left<<"SSN" << aan<<endl;
}
To write the object in an output file, do the following:
string filename = "X.txt";
ofstream outfile;
outfile.open (filename.c_str(), ios::out | ios::app);
outfile.write ( (char *)(&X), sizeof(X) );
outfile.close();
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.