0

我正在为家庭作业编写此代码。下面是我所做的代码,它的工作方式就是这样。我被告知我必须声明构造函数。代码下方是我们必须使用的头文件之一。那么,如果我没有声明构造函数,为什么程序可以工作。我前两天问过老师,她仍然没有回复我。

#include "stdafx.h"

#include <iostream>
using namespace std;

#include "dormroom.h"
#include "textlib.h"
#include <iomanip>

int main( )
{

    int numberInRoom1;

    int numberInRoom2;

    int numberInRoom3;


    double totalFunds;


    cout << "Enter the number of students in room 1: ";
        cin >> numberInRoom1;

    cout << "Enter the number of students in room 2: ";
        cin >> numberInRoom2;

    cout << "Enter the number of students in room 3: ";
        cin >> numberInRoom3;

       // HERE IS WHERE IM AM TO DECLARE THE CONSTRUCTORS      

        // THESE ARE THE CONSTRUCTOR FOR EACH OF THE OBJECTS
    DormRoom specialA(1, numberInRoom1);


    DormRoom specialB(2, numberInRoom2);


    DormRoom specialC(3, numberInRoom3);

    totalFunds = specialA.getRoomCount() * specialA.getRoomCost() + specialB.getRoomCount() * specialB.getRoomCost() + specialC.getRoomCount() * specialC.getRoomCost();

    cout << "The total funds collected for the special rooms are $" << setw(6) << setreal(1,2) << totalFunds << endl;

    system("PAUSE");
    return 0;
}

这是 dormroom.h 的头文件

#ifndef DORM_ROOM_CLASS
#define DORM_ROOM_CLASS

// maintain data for a dormitory room
class DormRoom
{
   private:
      int roomNumber;      // room number
      int roomCount;       // number of students in the room
      double roomCost;     // cost of the room
   public:
      // constructor
      DormRoom(int roomNo, int number);

      // functions return value of attributes
      int getRoomNumber();
      int getRoomCount();
      double getRoomCost();

      // change number of room occupants and update cost
      void setRoomCount(int number);
};

// ***********************************************************
//      DormRoom class implementation
// ***********************************************************

// constructor.  initialize room number and number of
// occupants. the room cost is $2700 for a double and
// $3500 for a single
DormRoom::DormRoom(int roomNo, int number):
   roomNumber(roomNo), roomCount(number)
{
   if (roomCount == 2)
      roomCost = 2700.0;
   else
      roomCost = 3500.0;
}

// return room number
int DormRoom::getRoomNumber()
{
   return roomNumber;
}

// return number of room occupants
int DormRoom::getRoomCount()
{
   return roomCount;
}

// return the cost of the room
double DormRoom::getRoomCost()
{
   return roomCost;
}

// change the number of students in the room.  must recompute
// the room cost
void DormRoom::setRoomCount(int number)
{
   roomCount = number;

   if (roomCount == 2)
      roomCost = 2700.0;
   else
      roomCost = 3500.0;
}

#endif   // DORM_ROOM_CLASS

更新:这是我们将用于该程序的大纲

int main( )
{
    // COMMENT THE OBJECTS         (ABOVE THE CODE)
    // DECLARE THE OBJECTS         (EACH OBJECT SHOULD BE DECLARED ON A SEPARATE LINE FOR READABILITY)

    // OUTPUT A MESSAGE TO THE USER (cout) - REFER TO THE RUN SECTION IN THE PROGRAM DIRECTIONS
    // INPUT THE RESPONSE FROM THE USER (cin)

    // COMMENT THE CONSTRUCTORS    (ABOVE THE CODE)
    // DECLARE THE CONSTRUCTORS    (EACH CONSTRUCTOR SHOULD BE DECLARED ON A SEPARATE LINE FOR READABILITY)
    // DECLARE THE CONSTRUCTOR FOR EACH OF THE OBJECTS: specialA, specialB, and specialC
    // FOR EXAMPLE, DormRoom specialA(1, numberInRoom1);

    // COMMENT THE CALCULATION      (ABOVE THE CODE)
    // PERFORM THE CALCULATION FOR totalFunds 
    // NOTE: totalFunds SHOULD BE DELCARED AT THE TOP OF THE PROGRAM WITH THE OTHER OBJECTS.
    // FOR EXAMPLE, totalFunds = specialA.getRoomCount( ) * specialA.getRoomCost( ) + .....
    // CONTINUE WITH THE REMAINING OBJECTS: specialB and specialC

    // OUTPUT THE INFORMATION - REFER TO THE RUN SECTION IN THE PROGRAM DIRECTIONS. DO NOT FORGET THE $ AND
    // AND TO USE SETREAL FOR THE DECIMAL PLACE.

    return 0;
}

感谢大家的帮助。

4

2 回答 2

3

在我看来,这可能与“声明”一词混淆。你的老师说声明构造函数,然后给出例子DormRoom specialA(1, numberInRoom1);。我相信他/她在技术上的意思是“构造函数声明”。构造函数本身已经在头文件中声明,除非你继承自DormRoom.

于 2013-06-19T15:32:48.930 回答
2

你的dormroom.h头文件中有一个声明的 DormRoom 构造函数。您无需在其他任何地方申报。在 main 函数中,您只能使用之前声明的构造函数创建新对象。

顺便说一句,最好不要添加函数、类等 int 头文件的实现。您应该分成两个单独的 .cpp 和 .h 文件。在头文件中应该只有声明,在源文件(.cpp)中应该只是一个实现。

于 2013-06-19T15:33:19.927 回答