用c++语言模拟完成健身会员卡的编程,请给我源代码和说明
时间: 2024-03-23 20:36:57 浏览: 18
好的,以下是一个简单的 C++ 健身会员卡模拟程序的源代码和说明:
```c++
#include <iostream>
#include <string>
using namespace std;
class GymMember {
private:
string name;
int age;
double weight;
double height;
bool isPremiumMember;
public:
GymMember(string n, int a, double w, double h, bool p) {
name = n;
age = a;
weight = w;
height = h;
isPremiumMember = p;
}
void printInfo() {
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
cout << "Weight: " << weight << endl;
cout << "Height: " << height << endl;
if (isPremiumMember) {
cout << "Membership level: Premium" << endl;
} else {
cout << "Membership level: Standard" << endl;
}
}
double calculateBMI() {
return weight / (height * height);
}
bool isEligibleForDiscount() {
if (isPremiumMember || age >= 60) {
return true;
}
return false;
}
};
int main() {
string name;
int age;
double weight, height;
bool isPremiumMember;
cout << "Enter your name: ";
cin >> name;
cout << "Enter your age: ";
cin >> age;
cout << "Enter your weight (in kg): ";
cin >> weight;
cout << "Enter your height (in meters): ";
cin >> height;
cout << "Are you a premium member? (0 for no, 1 for yes): ";
cin >> isPremiumMember;
GymMember member(name, age, weight, height, isPremiumMember);
cout << endl << "Member information:" << endl;
member.printInfo();
cout << endl << "BMI: " << member.calculateBMI() << endl;
if (member.isEligibleForDiscount()) {
cout << "You are eligible for a discount!" << endl;
} else {
cout << "Sorry, you are not eligible for a discount." << endl;
}
return 0;
}
```
这个程序定义了一个 `GymMember` 类,表示一个健身会员。它有名字、年龄、体重、身高和会员级别等属性,以及打印信息、计算 BMI 和判断是否有资格获得折扣等方法。
在 `main` 函数中,程序会提示用户输入自己的信息,然后创建一个 `GymMember` 对象,并调用它的方法来打印信息、计算 BMI 和判断是否有资格获得折扣。
这个程序是一个简单的健身会员卡模拟,可以根据实际需要进行修改和扩展。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)