I have written a short program based on an exercise found here
(Grading program), and I want to know if it's possible (out of curiosity) to replace the if statements in the .cpp
with a switch/case statement. Also, additional feedback is welcome since some of the things implemented here are new to me.
Header:
// include guard
#ifndef __MAINLAUNCH_H_INCLUDED__
#define __MAINLAUNCH_H_INCLUDED__
using namespace std;
class MainLaunch
{
int *grade;
public:
MainLaunch();
MainLaunch(int&);
~MainLaunch();
string getLetterGrade ();
int getGrade() {return *grade;};
};
#endif //__MAINLAUNCH_H_INCLUDED__
Cpp:
#include <iostream>
#include <string>
#include "MainLaunch.h"
using namespace std;
MainLaunch::MainLaunch()
{
grade=new int;
*grade=75;
}
MainLaunch::MainLaunch(int& x)
{
grade=new int;
*grade=x;
}
MainLaunch::~MainLaunch()
{
delete grade;
}
string MainLaunch::getLetterGrade()
{
int y = MainLaunch::getGrade();
if(y==100)
return "Perfect score!";
else if(y>90)
return "A";
else if(y>80)
return "B";
else if(y>70)
return "C";
else if(y>60)
return "D";
else
return "F";
}
void main ()
{
int input;
MainLaunch ml1;
cout << "Hello. Please enter your grade:" << endl;
cin >> input;
MainLaunch ml2(input);
cout << "Default Constructor Object Grade is " << ml1.getGrade() << "(" << ml1. getLetterGrade() << ")." << endl;
cout << "Declared Constructor Object Grade is " << ml2.getGrade() << "(" << ml2.getLetterGrade() << ")." << endl << endl;
system("pause");
}