In: Computer Science
Suppose the following letter grade class has been defined globally in a program.
#include <iostream> using namespace std;
class Grade { private:
char grade; public:
Grade(char in_grade); }; void print( );
Grade::Grade ( char in_grade) { } grade = in_grade;
void Grade::print ( ) {
cout << grade; }
1.Write a main function that reads one character from the keyboard, create a Grade object containing that character, and then have the object print itself.
2. Furthermore, create an output file (named “output.txt”), and save the character to this output file. Last, close this output file. (Answer this question).
Below is your code:
#include <iostream>
#include <fstream>
using namespace std;
class Grade {
private:
char grade;
public:
Grade(char in_grade);
void print( );
};
Grade::Grade ( char in_grade) {
grade = in_grade;
}
void Grade::print ( ) {
cout << grade;
}
int main() {
//getting character from user input
char c;
cout<<"Enter a character: ";
cin>>c;
//creating object of Grade class
Grade gr(c);
//calling print method to print the grade object
gr.print();
//creating an output file
ofstream fileStream;
fileStream.open ("output.txt");
//writing to file
fileStream<<c;
//closing the file
fileStream.close();
}
Output