Jacob
Jacob

Reputation: 21

C++ program works without declaring constructor but need to declare one

I am writing this code for a homework assignment. Below is the code that I did and it works the way that it is. I am told that I must declare the constructors. Below the code is one of the header files that we must use. So why is it that the program works if I did not declare the constructor. I have ask the teacher two days ago and she still has not responded back to me.

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

this is the header file for 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

update: this is the outline we are to use for this program

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

Thanks for the help everyone.

Upvotes: 0

Views: 204

Answers (2)

Kris
Kris

Reputation: 1556

You have a declared constructor of DormRoom in your dormroom.h header file. You don't need to declare in any other place. In main function you only create new objects, using a constructor declared before.

By the way, it is a good practice to not to add an implementation of functions, classes etc int header files. You should divide into two separate .cpp and .h files. In header file should be only declarations and in source file (.cpp) should be only an implementation.

Upvotes: 2

wlyles
wlyles

Reputation: 2316

It looks to me like this might be confusion with the word "declare." Your teacher says declare the constructors, then gives the example DormRoom specialA(1, numberInRoom1);. I believe he/she technically means "declare with the constructor". The constructor itself has already been declared in the header file, and you don't absolutely need a default constructor unless your inheriting from DormRoom.

Upvotes: 3

Related Questions