Can anybody comment on this code for me? I'm not new to OOP, but I am new to C++ and I'm sure I didn't exactly do this correctly from a C++ programmer's point of view. I was very concerned about learning the proper use of header files. Did I do it correctly? I think next I'll add some menu driven stuff and create a collection of house objects.
logic.h
logic.cppCode:class Building { public: Building(); ~Building(); setRooms(int num); int getRooms(); setFloors(int num); int getFloors(); private: int rooms, floors; }; class House : public Building { public: House(); ~House(); setBedrooms(int num); int getBedrooms(); setBaths(int num); int getBaths(); private: int bedrooms, baths; };
main.cppCode:#include <iostream> #include "logic.h" using namespace std; /* Building functions */ Building::Building() { std::cout << "New building object created." << endl; } Building::~Building() { std::cout << "Building object being destroyed." << endl; } Building::setFloors(int num) { floors = num; } Building::setRooms(int num) { rooms = num; } int Building::getFloors() { return floors; } int Building::getRooms() { return rooms; } /* House functions */ House::House() { std::cout << "New house object created." << endl; } House::~House() { std::cout << "House object being destroyed." << endl; } House::setBaths(int num) { baths = num; } House::setBedrooms(int num) { bedrooms = num; } int House::getBaths() { return baths; } int House::getBedrooms() { return bedrooms; }
Code:#include <iostream> #include "logic.h" using namespace std; int main() { House h; h.setBaths(12); h.setBedrooms(40); h.setFloors(5); h.setRooms(30); std::cout << "Current house" << endl; std::cout << "Baths: " << h.getBaths() << endl; std::cout << "Bedrooms: " << h.getBedrooms() << endl; std::cout << "Floors: " << h.getFloors() << endl; std::cout << "Rooms: " << h.getRooms() << endl; return 0; }



LinkBack URL
About LinkBacks


