1

この割り当てでは、car という名前のクラスを作成し、car オブジェクトを作成して、それぞれ 5mph で 5 倍加速し、ブレーキをかけるように求めています。これで完了ですが、main.cpp から関数とクラスの定義を分離したいのですが、これを行う方法を完全に理解していないと思います。

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

// Car class declaration.

class Car 
{
    private:
    // member variables
    int yearModel;
    string make;
    int speed;

    public:
    Car(int carYearModel, string carMake, int carSpeed)
    { yearModel = carYearModel;
      make = carMake;
      speed = carSpeed; }

    void accelerate(int mph)
    { speed += mph; }
    void brake(int mph)
    { speed -= mph; }

    int getSpeed () const
    { return speed; }
};

int main()
    {
    int count;
    Car honda(2005, "Accord", 0);

    cout << "The starting speed is "  
     << honda.getSpeed() << endl << endl;

    cout << "We will now accelerate by 5 mph, 5 times.\n";
    system ("pause");

    for ( count = 0; count < 5; count++)
    {
        honda.accelerate(5);
        cout << "Speed is now: " << honda.getSpeed() 
             << " mph." << endl;
    }

    system ("pause");

    cout << endl << "Now we will brake by 5 mph, 5 times. \n";
    system ("pause");

    for ( count = 0; count < 5; count++)
    {
        honda.brake(5);
        cout << "Speed is now: " << honda.getSpeed() 
             << " mph." << endl;
    }

    system ("pause");

    return 0;
    }
4

1 に答える 1

1
  • 定義から宣言を分割します。
  • 定義を cpp ファイルに移動
  • 宣言を hpp ファイルに移動する

最も重要なこと

  • using namespaceヘッダーファイルでは決して使用しないでください

車.hpp:

#ifndef CAR_INCLUDED_H
#define CAR_INCLUDED_H
#include <string>

class Car 
{
    private:
        int yearModel;
        std::string make;
        int speed;

    public:
        Car(int carYearModel, std::string carMake, int carSpeed);
        void accelerate(int mph);
        void brake(int mph);
        int getSpeed () const;
};

#endif

Car.cpp:

#include "Car.hpp"

Car::Car(int carYearModel, std::string carMake, int carSpeed)
{
    yearModel = carYearModel;
    make = carMake;
    speed = carSpeed;
}

void Car::accelerate(int mph)
{
    speed += mph;
}
void Car::brake(int mph)
{
    speed -= mph;
}

int Car::getSpeed() const
{
    return speed;
}

main.cpp:

#include "Car.hpp"

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
    int count;
    Car honda(2005, "Accord", 0);

    cout << "The starting speed is "  
        << honda.getSpeed() << endl << endl;

    cout << "We will now accelerate by 5 mph, 5 times.\n";
    system ("pause");

    for ( count = 0; count < 5; count++)
    {
        honda.accelerate(5);
        cout << "Speed is now: " << honda.getSpeed() 
            << " mph." << endl;
    }

    system ("pause");

    cout << endl << "Now we will brake by 5 mph, 5 times. \n";
    system ("pause");

    for ( count = 0; count < 5; count++)
    {
        honda.brake(5);
        cout << "Speed is now: " << honda.getSpeed() 
            << " mph." << endl;
    }

    system ("pause");

    return 0;
}
于 2013-08-31T21:19:36.517 に答える