I am building a simple program, using classes I have to create a main building class and make some derived classes based on the building.
I have created the base class, and have tried to create one derived class using some examples found on the internet but it seems to be giving me a bunch of compile errors. This is my latest attempt.
Code:
//Building.h
#ifndef BUILDING_H
#define BUILDING_H
//Main building class which will be used as template to inherit other classes.
class Building {
public:
Building() { Rooms = 0; Floors = 0; Area = 0; Heating = 'e'; } //Constructor
~Building(); //Destructor
//Sets the number of rooms for the building
void setRooms( int );
//Sets the number of floors for the building
void setFloors( int );
//Sets the area for the building
void setArea( int );
//Sets the heating type for the building
void setHeating( char );
//Accessor function to retreive current value
void getValue( int );
//Prints the value of Building
void Print();
protected:
int Rooms;
int Floors;
int Area;
char Heating;
};
#endif //BUILDING_H
member functions.
Code:
//Building.cpp
#include "Building.h"
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
///Destructor for building class
Building::~Building() {
cout << "Building object destroyed!" << endl;
}
///sets the value of int Rooms,floors and area in the building class.
///function accepts 1 parameter, which will be validated
///if the value is greater then 1 and less then 1000 it's
///all good otherwise it will be a default of 0.
void Building::setRooms( int x ) {
Rooms = ( x >= 0 && x < 1000 ) ? x : 0;
}
void Building::setFloors( int x ) {
Floors = ( x >= 0 && x < 1000 ) ? x : 0;
}
void Building::setArea( int x ) {
Area = ( x >= 0 && x < 10000 ) ? x : 0;
}
void Building::setHeating( char h ) {
switch( h ) {
//case 'e'
case 'E':
Heating = h;
break;
//case 'o'
case 'O':
Heating = h;
break;
default:
cout << "Invalid value entered! Please make another selection." << endl;
Heating = 'e';
break;
}
}
void Building::getValue( int x ) {
switch ( x ) {
case 1:
cout << "The value of Rooms is: " << Rooms << endl;
break;
case 2:
cout << "The value of Floors is: " << Floors << endl;
break;
case 3:
cout << "The value of Area is: " << Area << endl;
break;
case 4:
cout << "The value of Heating is: " << Heating << endl;
break;
default:
cout << "Incorrect value entered. Please make another selection." << endl;
break;
}
}
void Building::Print() {
cout << "Building specifications: " << endl;
cout << "Rooms : " << Rooms << endl;
cout << "Floors : " << Floors << endl;
cout << "Area : " << Area << endl;
cout << "Heating: " << Heating << endl;
}
this is my derived class.
Code:
//School.h
#ifndef SCHOOL_H
#define SCHOOL_H
#include "Building.h"
class School : public Building {
public:
School( int, int, int, char, int, int, int );
~School();
void setWashrooms( int );
void setClassrooms( int );
void setOffices( int );
protected:
int Washrooms;
int Classrooms;
int Offices;
}
#endif //SCHOOL_H
it gives me problems compiling the functions for my derived class.
Code:
#include "School.h"
void School::setWashrooms( int x ) {
Washrooms = ( x >= 0 && x < 1000 ) ? x : 0;
}
void School::setClassrooms( int x ) {
Classrooms = ( x >= 0 && x < 1000 ) ? x : 0;
}
void School::setOffices( int x ) {
Offices = ( x >= 0 && x < 1000 ) ? x : 0;
}
Can you help me find out what im doing wrong. I've looked at a ton of tutorials on the net. Thanks.