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