【发布时间】:2020-02-12 02:35:50
【问题描述】:
当将 freeSeats 设置为 0 时,我的代码仍然显示一个人在他/她的车中有可用的座位。
我创建了两个类。一个用于汽车,一个用于人。 Car 类具有查看车内是否有空闲座位的功能。一个人-对象可以有一辆汽车。在检查此人是否有可用座位时,即使我输入“0”,我的代码也会响应“是”。为什么?
#pragma once
#include <iostream>
//Here is class Car declaration
class Car {
private:
unsigned int freeSeats;
public:
bool hasFreeSeats() const;
void reserveFreeSeat();
Car( unsigned int freeSeats);
};
//Here is function definition
#include "Car.h"
bool Car::hasFreeSeats() const {
if (freeSeats > 0)
return true;
return false;
}
void Car::reserveFreeSeat() {
--freeSeats;
}
Car::Car(unsigned int freeSeas) :
freeSeats{ freeSeats }
{
}
//Here is class Person declaration
class Person {
private:
std::string name;
std::string email;
Car *car; //pointer to a car
public:
Person(std::string name, std::string email, Car *car = nullptr);
std::string getName() const;
std::string getEmail() const;
void setEmail();
bool hasAvalibaleSeats() const;
friend std::ostream& operator << (std::ostream& os, const Person& p);
};
//Here is function definition
Person::Person(std::string name, std::string email, Car *car) :
name{ name }, email{ email }, car{ car }
{
}
std::string Person::getName() const {
return name;
}
std::string Person::getEmail() const {
return email;
}
void Person::setEmail() {
std::string newEmail;
std::cout << "What is the e-mail adress?";
std::cin >> newEmail;
email = newEmail;
std::cout << "E-mail has been set." << std::endl;
}
bool Person::hasAvalibaleSeats() const {
if (car != nullptr) { //check if there is a car
return car->hasFreeSeats();
}
return false;
}
std::ostream& operator << (std::ostream& os, const Person& p) {
std::string seats = "No";
if (p.hasAvalibaleSeats())
seats = "Yes";
return os << "Name: " << p.name << "\nE-mail: " << p.email << "\nHas free seats: " << seats << std::endl;
}
//From main im calling
#include "Car.h"
#include "Person.h"
int main() {
Car ferrari{ 2 };
Car bugatti{ 3 };
Car jeep{0};
Person one{ "Aleksander","aleks@aleks.com", &ferrari };
Person two{ "Sara","sara@sara.com", &bugatti };
Person three{ "Daniel", "daniel@daniel.com", &jeep };
Person four{ "Chris", "chris@chris.com" };
std::cout << one << std::endl;
std::cout << two << std::endl;
std::cout << three << std::endl;
std::cout << four << std::endl;
system("pause");
return 0;
}
我明白了
姓名:亚历山大 电子邮箱:aleks@aleks.com 有空位:是
姓名:萨拉 邮箱:sara@sara.com 有空位:是
姓名:丹尼尔 电子邮箱:daniel@daniel.com 有空位:是
姓名:克里斯 电子邮箱:chris@chris.com 有空位:没有
但我希望 Daniel 有空位说“不”
【问题讨论】:
-
错字?
Car::Car(unsigned int freeSeas) : freeSeats{ freeSeats } {}构造函数参数freeSeas从未在构造函数中使用。 -
warning: field 'freeSeats' is uninitialized when used here [-Wuninitialized],warning: unused parameter 'freeSeas' [-Wunused-parameter]。打开你的编译器警告! -
题外话:请不要写
if(condition) return true; else return false;之类的代码——只要有return condition;,这样的代码更简洁。 -
@MarkStorer 在构造函数中有一个参数和一个同名的成员是完全可以的。当使用成员时,初始化列表的语法很明显(
: member(...),在任何其他位置,它被参数隐藏(但可以通过例如this->member引用)。 -
我不同意必须更改名称以使其不同。通常成员字段和构造函数参数将具有相同的名称,因为它们是相同的并且它们的含义相同。强制规则为这两者使用不同的名称不可避免地会导致匈牙利表示法(
m_field {field}或field {providedField}之类的东西)。