Inheritance

This is a discussion on Inheritance within the C++ Programming forums, part of the General Programming Boards category; I am building a simple program, using classes I have to create a main building class and make some derived ...

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    4

    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.

  2. #2
    Registered User Dohojar's Avatar
    Join Date
    Feb 2002
    Posts
    115
    Can you post the errors you are getting.
    Dohojar Moajbuj
    Time is the greatest teacher, too bad it kills all its students

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    127
    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
    Nana C++ Library is a GUI framework that designed to be C++ style, cross-platform and easy-to-use.

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    4
    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?

  5. #5
    Registered User
    Join Date
    Aug 2003
    Posts
    127
    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
    Nana C++ Library is a GUI framework that designed to be C++ style, cross-platform and easy-to-use.

  6. #6
    Registered User
    Join Date
    Aug 2003
    Posts
    4
    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?

  7. #7
    Registered User
    Join Date
    Aug 2003
    Posts
    127
    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.
    Nana C++ Library is a GUI framework that designed to be C++ style, cross-platform and easy-to-use.

  8. #8
    Registered User
    Join Date
    Aug 2003
    Posts
    4
    Great, that clears alot up for me. Thanks for the help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 06-08-2009, 03:03 PM
  2. Multiple Inheritance - Size of Classes?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 11-26-2004, 08:04 AM
  3. inheritance and performance
    By kuhnmi in forum C++ Programming
    Replies: 5
    Last Post: 08-04-2004, 12:46 PM
  4. Inheritance and Polymorphism
    By bench386 in forum C++ Programming
    Replies: 2
    Last Post: 03-18-2004, 09:19 PM
  5. Inheritance vs Composition
    By Panopticon in forum C++ Programming
    Replies: 11
    Last Post: 01-20-2003, 03:41 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21