Create objects

This is a discussion on Create objects within the C++ Programming forums, part of the General Programming Boards category; Code: //base class building definition. #ifndef BUILDING_H #define BUILDING_H #include <iostream.h> class building { public: building(); //default constructor building(int r, ...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    34

    Angry Create objects

    Code:
    //base class building definition.
    
    #ifndef BUILDING_H
    #define BUILDING_H
    
    #include <iostream.h>
    
    class building 
    {
    
    public:
    
    	building();				//default constructor
    	building(int r, int fl, int fla, char h);				//overloaded construtor
    	~building();											//destructor
    	void printoscreen();									//prints to screen
    
    	void setrooms (int numrooms);							//set no. rooms 
    	int getrooms();											//get no. rooms
    
    	void setfloors (int numfloors);							//set no. floors
    	int getfloors();										//get no.of floors
    
    	void setarea (int floorarea);							//set floorarea
    	int getarea();											//get floorarea
    
    	void setheat (char heatype);							//set heatertype
    	int getheat();											//get heatertype
    
    
    private:
    
    	int rooms;		
    	int floors;		
    	int area;	
    	char heatype;	
    
    };
    
    #endif
    
    // member function definition for class building
    
    
    
    
    #include "building.h"
    
    building::building()
    {
    	// NULL constructor
    	 rooms=0;
    	 floors=0;
    	 area=0;
    	 heatype='o';
    }
    //constructor function to initialize private data
    
    building::building(int r, int f, int fla, char ht)
    {
    
    	rooms = r;
    	floors = f;
    	area = fla;
    	heatype = ht;
    }
    
    building::~building()   // call destructor
    {
    
    	cout<<"\nThe building object has been destroyed";
    
    }
    
    void building::printoscreen(){
    
    // print data to screen
    
    cout<<"\nThe number of rooms = :"<<getrooms();
    cout<<"\nThe number of floors = :"<<getfloors();
    cout<<"\nThe floor area of the building is : "<<getarea()<<" square feet";
    
    if(getheat() == 'o')
    cout<<"\nHeating type is Oil.";
    else
    cout<<"\nHeating type is Electric.";
    
    }
    
    //mutator functions
    void building::setrooms(int numrooms){
    // set the number of rooms
    	rooms =numrooms;
    
    }
    
    void building::setfloors(int numfloors){
    	floors =numfloors;
    }
    void building::setarea(int farea)
    {
    	// set floorarea
    	area = farea;
    }
    void building::setheat(char heatr)
    {
    	// set heater
    	heatype = heatr;
    }
    
    
    
    // accessor functions
    int building::getrooms()
    {
    	return rooms;
    }
    int building::getfloors()
    {
    	return floors;
    }
    int building::getarea()
    {
    	return area;
    }
    int building::getheat()
    {
    	return heatype;
    }
    
    // member function definition for class building
    
    
    
    
    #include "building.h"
    
    building::building()
    {
    	// NULL constructor
    	 rooms=0;
    	 floors=0;
    	 area=0;
    	 heatype='o';
    }
    //constructor function to initialize private data
    
    building::building(int r, int f, int fla, char ht)
    {
    
    	rooms = r;
    	floors = f;
    	area = fla;
    	heatype = ht;
    }
    
    building::~building()   // call destructor
    {
    
    	cout<<"\nThe building object has been destroyed";
    
    }
    
    void building::printoscreen(){
    
    // print data to screen
    
    cout<<"\nThe number of rooms = :"<<getrooms();
    cout<<"\nThe number of floors = :"<<getfloors();
    cout<<"\nThe floor area of the building is : "<<getarea()<<" square feet";
    
    if(getheat() == 'o')
    cout<<"\nHeating type is Oil.";
    else
    cout<<"\nHeating type is Electric.";
    
    }
    
    //mutator functions
    void building::setrooms(int numrooms){
    // set the number of rooms
    	rooms =numrooms;
    
    }
    
    void building::setfloors(int numfloors){
    	floors =numfloors;
    }
    void building::setarea(int farea)
    {
    	// set floorarea
    	area = farea;
    }
    void building::setheat(char heatr)
    {
    	// set heater
    	heatype = heatr;
    }
    
    
    
    // accessor functions
    int building::getrooms()
    {
    	return rooms;
    }
    int building::getfloors()
    {
    	return floors;
    }
    int building::getarea()
    {
    	return area;
    }
    int building::getheat()
    {
    	return heatype;
    }
    // driver for building
    #include "building.h"
    #include "business.h"
    #include "school.h"
    #include "home.h"
    
    
    int main(void)
    {
    
    building b(20, 3, 18000, 'o')	;	//building parameter is 20 rooms, 3 floors, 
    								//18000sqfeet and oil heat
    b.printoscreen();
    			
    
    
    
    school s(75, 2, 55000, 'o', 10, 40, 12);		//derived object s of class school
    											//building set to 75 rooms, 2 floors,
    s.printoscreen();
    												//55000sqfeet and has oil heat
    //derived business class parameters
    
    business bus(20, 1, 21000, 'o', 4, 12);
    
    bus.printoscreen();
    
    //derived class home parameters
    home h(11, 3, 12000, 'e', 2, 5);
    h.printoscreen();
    
    return 0;
    }

    Code tags added by Hammer

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Please read this then edit your post accordingly.

    [edit]
    And...errrmm... what's your question again?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    34
    Class building is the base class
    All other classes are derived from Building

    All I'm trying to to is to and print the derived classes with the values assigned in main thru overloaded constructors

    private functions in building are rooms, floors, area and heatype

    The derived classes ,addition in this example, school also has as members washrooms, classrooms and offices,

    The HTML editor does not seem to want to cooperate.

    I appreciate any help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about cout an stack object?
    By joenching in forum C++ Programming
    Replies: 8
    Last Post: 05-08-2005, 10:10 PM
  2. pointers in arrays... Declaring objects in a loop.
    By Guest852 in forum C++ Programming
    Replies: 10
    Last Post: 04-05-2005, 06:57 AM
  3. Odd 3D Invis Objects?
    By Zeusbwr in forum Game Programming
    Replies: 4
    Last Post: 12-07-2004, 06:01 PM
  4. creating objects in c++
    By sachitha in forum C++ Programming
    Replies: 2
    Last Post: 09-19-2004, 12:19 PM
  5. Data Structures for game objects
    By curlious in forum C++ Programming
    Replies: 2
    Last Post: 09-06-2004, 01:39 PM

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