-
Inheritance
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.
-
Can you post the errors you are getting.
-
It's here, you are missing a semicolon
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(); //this is a warning,you don't define the destructor
void setWashrooms( int );
void setClassrooms( int );
void setOffices( int );
protected:
int Washrooms;
int Classrooms;
int Offices;
} //here miss a ;
#endif //SCHOOL_H
-
Thanks. I'm going to have to read up more on destructors. I'm having alittle trouble using my derived class in my program. I'm getting (1) compile error
"c:\desktop\project\main.cpp(16) : error C2512: 'School' : no appropriate default constructor available"
I'm using this as my constructor for my class.
Code:
School( int, int, int, char, int, int, int ) : Building(){};
this is where my compiler (msvc++ 6.0) throws the error.
in my main.cpp. Where i do a simple:
Code:
School s; // This is the trouble line.
Am i constructing my class incorrectly?
-
yes,you must get a default constructor when you School s;
Code:
//School.h
#ifndef SCHOOL_H
#define SCHOOL_H
#include "Building.h"
class School : public Building {
public:
School(){}; //here,offer a default constructor
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
-
Excellent, thanks for the help man. So I didn't need to have the lame constructor that I had in place already? My derived class will use the base classes constructor, and then use it's own default constructor to finish up it's creation?
-
yes,School( int, int, int, char, int, int, int ); is a useless constructor and is not a default constructor
School s; this will call the default constructor
The compiler will not insert a default construct for the class school.because the constructor School(int, int, int, char, int, int, int )is existing.
now you must write a default constructor by yourself or you could
declare School( int a=1, int b=1, int c=1, char d=1, int e=1, int f=1, int g=1 ); to make the constructor into a default constructor
be careful,a class must have a default constructor existing.
-
Great, that clears alot up for me. Thanks for the help.:)