【问题标题】:How to provide an Implementation in c++? [closed]如何在 C++ 中提供实现? [关闭]
【发布时间】:2014-11-03 23:26:26
【问题描述】:

往年考试2出现以下问题。

class SchoolBus
{
public:
    SchoolBus(int seats, int seatedStudents);
    bool addStudents(int students);
    bool removeStudents(int students);
    int getStudents() const;

private:
    int seats;
    int seatedStudents;
};

2) 提供 SchoolBus 构造函数的实现。

SchoolBus(int seats, int seatedStudents) {
   this->seats = seats;
   this->seatedStudents = seatedStudents;
}

我没有得到 #2。我知道这就是答案,但是您将如何在代码中编写并编译它?我想实际编译它,看看它是如何工作的。

【问题讨论】:

  • 你试过编译代码吗?

标签: c++ class implementation


【解决方案1】:

在#2 中,代码称为构造函数,因此我将使用我更熟悉的术语来编写构造函数:

SchoolBus::SchoolBus(int seating_capacity, int students_in_bus)
: seats(seating_capacity),
  seatedStudents(students_in_bus)
{
}

要对其进行编码,我可能会将其放入一个名为“school_bus.cpp”的文件中:

#include "school_bus.hpp"

SchoolBus::SchoolBus(int seating_capacity, int students_in_bus)
: seats(seating_capacity),
  seatedStudents(students_in_bus)
{
}

我会将类声明放在一个名为“school_bus.hpp”的头文件中:

class SchoolBus
{
public:
    SchoolBus(int seats, int seatedStudents);
    bool addStudents(int students);
    bool removeStudents(int students);
    int getStudents() const;

private:
    int seats;
    int seatedStudents;
};

要编译,我可能会使用g++

  g++ -g -o school_bus.o -c school_bus.cpp

为了测试它,我会创建一个“main”函数:

#include "school_bus.hpp"
int main(void)
{
  static SchoolBus yellow_bus(25, 36);
  return 0;
}

这可能需要构建和链接:

g++ -g -o school_bus.exe main.cpp school_bus.o

然后我可以使用调试器:

gdb ./school_bus.exe

【讨论】:

    【解决方案2】:

    构造函数实现没有正确限定。它应该以类名作为前缀。

    这个:

    SchoolBus(int seats, int seatedStudents) {
       this->seats = seats;
       this->seatedStudents = seatedStudents;
    }
    

    应该变成:

    SchoolBus::SchoolBus(int seats, int seatedStudents) {
       this->seats = seats;
       this->seatedStudents = seatedStudents;
    }
    

    那么它应该可以工作了。

    【讨论】:

      【解决方案3】:

      要么像这样完整地声明:

      class SchoolBus
      {
      public:
          SchoolBus(int seats, int seatedStudents)
          {
              this->seats = seats;
              this->seatedStudents = seatedStudents;
          }
          bool addStudents(int students);
          bool removeStudents(int students);
          int getStudents() const;
      
      private:
          int seats;
          int seatedStudents;
      };
      

      或将两者分开

      //in schoolbus.h
      #pragma once
      class SchoolBus
      {
      public:
          SchoolBus(int seats, int seatedStudents);
          bool addStudents(int students);
          bool removeStudents(int students);
          int getStudents() const;
      
      private:
          int seats;
          int seatedStudents;
      };
      
      //in schoolbus.cpp
      #include "schoolbus.h"
      
      SchoolBus::SchoolBus(int seats, int seatedStudents) {
         this->seats = seats;
         this->seatedStudents = seatedStudents;
      }
      

      还值得注意的是,通常这会在初始化列表中完成,如下所示:

      SchoolBus::SchoolBus(int pSeats, int pSeatedStudents) :
        seats(pSeats),
        seatedStudents(pSeatedStudents)
      {
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-03-26
        • 1970-01-01
        • 1970-01-01
        • 2017-06-13
        • 1970-01-01
        • 2012-08-09
        • 1970-01-01
        相关资源
        最近更新 更多