【发布时间】:2014-06-26 15:14:58
【问题描述】:
我根据here 找到的练习编写了一个简短的程序
(评分程序),我想知道是否可以(出于好奇)用 switch/case 语句替换 .cpp 中的 if 语句。此外,欢迎提供额外反馈,因为这里实施的一些东西对我来说是新的。
标题:
// 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");
}
【问题讨论】:
标签: c++