我正在尝试根据以下要求编写代码(面向对象编程),但是我bool
一直返回 true 而它应该是 false。希望你们能告诉我我的代码出了什么问题。
要求
Member(string = "xxx", bool = false, string = "addr")
Constructor with default values
setMember(string, bool, string): void
Set the values of data members to the respective pass-in values
setPremium(bool): void
Set the value of data member, premium to the pass-in value
getPremium():bool
Returns the value of data member, premium
setMember(string, bool, string): void
Set the values of data members to the respective pass-in values
displayMember():void
Uses cout to display the data members
我的代码
#include <iostream>
#include <string>
using namespace std;
class Member
{
private:
string name;
bool premium;
string address;
public:
Member(string = "xxx", bool = false, string = "addr");
void setMember(string, bool, string);
void setPremium(bool);
bool getPremium();
int index;
void DisplayMember();
};
Member::Member(string name, bool premium, string address)
{
this-> name = name;
this-> premium = premium;
this-> address = address;
}
void Member::setMember(string name, bool premium, string address)
{
this-> name = name;
this-> premium = premium;
this-> address = address;
}
void Member::setPremium(bool)
{
int i;
bool premium;
i = 0;
while (i != 4)
{
i = i + 1;
}
if (premium >= 4)
{
index = i;
premium = true;
}
else
premium = false;
}
bool Member::getPremium()
{
return premium;
}
void Member::DisplayMember()
{
cout<<"Name : "<<name<<endl;
cout<<"Premium : "<<boolalpha<<premium<<endl;
cout<<"Address : "<<address<<endl;
}
int main()
{
Member detail1("Martin ", "2 Tampines Avenue");
detail1.DisplayMember();
cin.ignore();
cin.ignore();
}