尽管这是我第一次学习课程的活动,但我们已经处理了多个文件,所以我很难做出任务要求我做的事情。
活动7如下: 为CarMechanic创建一个新类。 CarMechanic.h,CarMechanic.cpp
CarMechanic应具有以下属性: 字符串名称;
CarMechanic类应该有以下方法: CarMechanic(字符串名称); //构造函数 void repairCar(Car carToFix); 在repairCar方法中,它应该调用作为参数传入的Car对象的修复方法。
主要通过创建CarMechanic对象并修复汽车进行测试。
所以我试着这样做。 这是我的CarMechanic.h文件:
#pragma once
#include <iostream>
#include <string>
#include "Car.h"
using namespace std;
class CarMechanic
{
private:
bool brokenDown = false;
string name;
public:
CarMechanic(string name); //constructor
~CarMechanic();
void repairCar(Car carToFix);
void repairNow();
};
这是我的CarMechanic.cpp文件:
#include "CarMechanic.h"
#include <iostream>
CarMechanic::CarMechanic(string name)
{}
CarMechanic::~CarMechanic() {} //destructor does nothing
void CarMechanic::repairNow()
{
brokenDown = false; //Car is not broken
std::cout << "The repairNow function has set car to not broken." <<
std::endl;
}
void CarMechanic::repairCar(Car carToFix)
{
repairNow();
}
现在我的main.cpp文件试图使用我创建的类汽车的前一个对象:
#include <iostream>
#include "Car.h"
#include "CarMechanic.h"
int main()
{
Car myCarObject1;
Car speedster(10);
CarMechanic repairMan("Mike");
repairMan.repairCar(MyCarObject1);
所以我觉得最后一行应该接受MyCarObject1作为Mike将修复的汽车对象,但它没有这样做.Visual Basic告诉我myCarObject1是未定义的,Speedster也是如此。如何获取repairCar函数以接收汽车对象并进行修复?
如果这有任何帮助,这是我的其他文件:
Car.h:
#ifndef CAR_H
#define CAR_H
class Car
{ // defining the Car class
private: // restricted access, can only access from inside the class.
unsigned fuelLeft; // class member variable.
bool brokenDown = false;
public: // open access, can access from outside the class (e.g. main).
Car(); // default constructor, has no input parameters.
Car(unsigned); // another constructor, has unsigned as a parameter.
~Car(); // destructor, called when car objects are destroyed.
short unsigned int fuelSpace;
void drive(); // class member function.
bool hasFuel(); // class member function.
void refuel();
void breakdownNow();
void repairNow();
bool isBrokenDown();
};
#endif // CAR_H
Car.cpp:
#include <iostream>
#include "Car.h"
Car::Car() { // defining default constructor.
fuelLeft = 5; // typical to initialise class member variables.
fuelSpace = fuelLeft; //store the amount of fuel each car has
}
Car::Car(unsigned startingFuel) { // defining another constructor.
fuelLeft = startingFuel; // typical to initialise class member variables.
fuelSpace = startingFuel; //store the amount of fuel each car has
}
Car::~Car() { } // destructor does nothing.
void Car::drive() { // defining a class member function (method).
if (brokenDown == true) //check if car is broken first
{
std::cout << "Can't drive car, it has broken down!" << std::endl;
}
else if (fuelLeft > 0)
{
fuelLeft--;
std::cout << "Drives car 1km" << std::endl;
}
else
{
std::cout << "Can't drive car, ran out of fuel!" << std::endl;
}
}
bool Car::hasFuel() { // defining a class member function (method).
if (fuelLeft > 0 && brokenDown != true)
return true;
return false;
}
void Car::refuel() {
fuelLeft = fuelSpace;
}
void Car::breakdownNow()
{
brokenDown = true; //Car is broken
std::cout << "The beakdownNow function has set car to broken." << std::endl;
}
void Car::repairNow()
{
brokenDown = false; //Car is not broken
std::cout << "The repairNow function has set car to not broken." <<
std::endl;
}
bool Car::isBrokenDown()
{
if (brokenDown == true)
{
std::cout << "The car is broken." << std::endl;
return true;
}
else
{
std::cout << "The car isn't broken." << std::endl;
return false;
}
}