In: Computer Science
Question 4
A. 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;
}
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.
Furthermore, create an output file (named “output.txt”), and save the character to this output file. Last, close this output file.
int main ( )
{
int a = 3, b= 2, c= 1, d, e, f, g;
d = a&b; e = a | c; f = a >> 1, g = a << 1;
cout << “d= “ << d << “ e = “ << e << “ f = “ << f << “g = “ << g << endl;
}
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(){
char in_grade;
cout<<"Enter Grade: ";
cin>>in_grade;
Grade grade(in_grade);
cout<<"Entered Grade is: ";
grade.print();
ofstream myfile;
myfile.open ("output.txt");
myfile <<"Your Grade is: "<<in_grade;
cout<<endl <<"Grade stored in file...";
myfile.close();
}
Output:
2.
a=3,b=2
d=a&b
& is bitwise and operator it will perform & operation on a’s and b’s bit values:
(3)10=(0011)2
(2)10=(0010)2
0011
0010
0010
(0010)2=(2)10
So d=2.
a=3,c=2
e=a|c
| is bitwise OR operator it will perform OR operation on a’s and c’s bit values:
(3)10=(0011)2
(1)10=(0001)2
0011
0001
0011
(0011)2=(3)10
So e=3.
a=3
f=a>>1
>> is bitwise right shift operator it will shift 1 bit of a’s bit values:
(3)10=(0011)2
After right shift by 1 0001
(0001)2=(1)10
So f=1.
a=3
f=a<<1
>> is bitwise left shift operator it will shift 1 bit left of a’s bit values:
(3)10=(0011)2
After left shift by 1 0110
(0110)2=(6)10
So g=6.
So Output is:
d= 2 e = 3 f = 1 g = 6