gpt4 book ai didi

C++程序无需声明构造函数即可运行,但需要声明一个

转载 作者:太空宇宙 更新时间:2023-11-04 16:20:36 25 4
gpt4 key购买 nike

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

#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;
}

感谢大家的帮助。

最佳答案

在我看来,这可能会与“声明”一词混淆。你的老师说声明构造函数,然后给出示例 DormRoom specialA(1, numberInRoom1);。我相信他/她在技术上意味着“声明构造函数”。构造函数本身已在头文件中声明,除非您从 DormRoom 继承,否则您绝对不需要默认构造函数。

关于C++程序无需声明构造函数即可运行,但需要声明一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17195062/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com