| |||||||||||
| |||||||||||
Overloading, Inheritance, Virtual, Polymorphism and more
OO in C++ - beyond the basics example from a Well House Consultants training course
More on OO in C++ - beyond the basics [link]
Source code: pert.cpp Module: C233
using namespace std;
/* Overloading, Inheritance, Virtual, Polymorphism and more Longer demo - lots of features in a short program!! You would not normally do anything like this much in such a short piece of code - this is a TRAINING EXAMPLE, folks! */ #include <iostream> // 1. Definition of the API (Application Programmer Interface) // and other private things in the object class Stay { public: ~Stay(); int getincome(); virtual int getincome(int) {} ; virtual int getup() {} ; virtual int getgroups() {} ; protected: int nightly; }; class Hotel : public Stay { public: Hotel(int, int); int getup(); int getgroups() ; int getincome(int); private: int roomcount; }; class Rental : public Stay { public: Rental(int); int getup(); int getgroups() ; int getincome(int); }; // 2. Internals / functionallity of the class Hotel // Destructor Stay::~Stay () { cout << "Cleared ..." << endl; } int Stay::getincome () { return nightly; } Hotel::Hotel(int roomcount, int baseprice) { nightly = roomcount * baseprice; this->roomcount = roomcount; } int Hotel::getup () { return nightly / roomcount; } // Overloaded - there are several getincomes! int Hotel::getincome (int ndel) { int perroom = nightly / roomcount; int rv; if (roomcount >= ndel) { rv = ndel * perroom; } else { rv = nightly; } return rv; } int Hotel::getgroups() { return roomcount; } Rental::Rental(int baseprice) { nightly = baseprice; } int Rental::getgroups() { return 1; } int Rental::getup () { return nightly; } int Rental::getincome (int ndel) { return nightly; } // 3. (Test) program that uses the class defined in the API int main(int argc, char** argv) { Stay * Melksham[4]; Hotel manor(5,72); Hotel beechfield(24,95); Hotel queensfeet(8,90); Rental ivyouse(140); // Scoping demo / force destructor to run as the stack // is released at the end of the block { Hotel buckhouse(302,60); } Melksham[0] = &manor; Melksham[1] = &beechfield; Melksham[2] = &ivyouse; Melksham[3] = &queensfeet; for (int k=0; k<4; k++) { cout << "Income during a course per night " << (*Melksham[k]).getincome() << endl; cout << "Income / course night / 3 delegates " << (*Melksham[k]).getincome(3) << endl; cout << "Guest Group Capacity " << (*Melksham[k]).getgroups() << endl; cout << "Unit cost " << (*Melksham[k]).getup() << endl; } } /* munchkin:c4 grahamellis$ g++ -o pert pert.cpp munchkin:c4 grahamellis$ ./pert Cleared ... Income during a course per night 360 Income / course night / 3 delegates 216 Guest Group Capacity 5 Unit cost 72 Income during a course per night 2280 Income / course night / 3 delegates 285 Guest Group Capacity 24 Unit cost 95 Income during a course per night 140 Income / course night / 3 delegates 140 Guest Group Capacity 1 Unit cost 140 Income during a course per night 720 Income / course night / 3 delegates 270 Guest Group Capacity 8 Unit cost 90 Cleared ... Cleared ... Cleared ... Cleared ... munchkin:c4 grahamellis$ */ Learn about this subject
This module and example are covered on the following public courses:
* Learning to program in C and C++ * C++ for C Programmers * C and C++ Programming * Learning to program in C and C++ * C and C++ Programming Also available on on site courses for larger groups Books covering this topic
Yes. We have over 700 books in our library. Books
covering C and C++ are listed here and when you've selected a
relevant book we'll link you on to Amazon to order.
Other Examples
This example comes from our "OO in C++ - beyond the basics" training module. You'll find a description of the topic and some
other closely related examples on the "OO in C++ - beyond the basics" module index page.
Full description of the source code
You can learn more about this example on the training courses listed on this page,
on which you'll be given a full set of training notes.
Many other training modules are available for download (for limited use) from our download centre under an Open Training Notes License. Other resources
• Our Solutions centre provides a number of longer technical articles.
• Our Opentalk forum archive provides a question and answer centre. • The Horse's mouth provides a daily tip or thought. • Further resources are available via the resources centre. • All of these resources can be searched through through our search engine • And there's a global index here. Purpose of this website
This is a sample program, class demonstration or answer from a
training course. It's main purpose
is to provide an after-course service to customers who have attended our
public private or
on site courses, but the examples are made
generally available under conditions described below.
Web site author
Conditions of use
Past attendees on our training courses are welcome to use individual
examples in the course of their programming, but must check
the examples they use to ensure that they are suitable for their
job. Remember that some of our examples show you how not to do
things - check in your notes. Well House Consultants take no responsibility
for the suitability of these example programs to customer's needs.
This program is copyright Well House Consultants Ltd. You are forbidden from using it for running your own training courses without our prior written permission. See our page on courseware provision for more details. Any of our images within this code may NOT be reused on a public URL without our prior permission. For Bona Fide personal use, we will often grant you permission provided that you provide a link back. Commercial use on a website will incur a license fee for each image used - details on request. |
| ||||||||||
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho PAGE: http://www.wellho.net/resources/ex.php4 • PAGE BUILT: Sun Oct 11 14:50:09 2020 • BUILD SYSTEM: JelliaJamb |