| |||
All the files for the shape demo on one page!
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: shapes_combined Module: C233
All the files for the shape demo on one page! Although it's much easier
to write a short program all in one file, as the program grows you'll need to split it into elements to be maintained on different cycles, tested one at a time, and shared on an "as needed" basis between lots of programs. This example file contains the source of a whole series of files, here as a single page on the web so that you can cut and paste each of them without having to keep following link after link to get to the next one! // ---------------- circle.h ----------------- #ifndef CIRCLE #include "shape.h" class Circle: public Shape { public: Circle(float radius); float getarea() ; }; #define CIRCLE #endif // ---------------- rectangle.h ----------------- #ifndef RECTANGLE #include "shape.h" class Rectangle: public Shape { public: Rectangle(float width, float height); float getarea() ; }; #define RECTANGLE #endif // ---------------- shape.h ----------------- #ifndef SHAPE class Shape { public: float width; float height; // bool cached; Later ;-) virtual float getarea() {} ; }; #define SHAPE #endif // ---------------- square.h ----------------- #ifndef SQUARE #include "rectangle.h" class Square: public Rectangle { public: Square(float size); void addframe(const float amount); } ; #define SQUARE #endif // ---------------- circle.cpp ----------------- #include "circle.h" Circle::Circle (float radius) { this->width = this->height = radius * 2.0; } float Circle::getarea() { return width * width * 3.14159265 / 4.0; } // ---------------- rectangle.cpp ----------------- #include "rectangle.h" Rectangle::Rectangle(float width, float height) { this->width = width; this->height = height; } float Rectangle::getarea() { return width * height ; } // ---------------- square.cpp ----------------- #include "square.h" Square::Square (float size) : Rectangle(size, size) { } void Square::addframe(const float wid) { width += wid; height += wid; } // ---------------- shape.cpp ----------------- #include "shape.h" // Yeah, right! Base class has no code at present in this demo // ---------------- shape_main.cpp ----------------- #include <iostream> using namespace std; #include "circle.h" #include "rectangle.h" #include "square.h" int main (int argc, char **argv) { Shape * papers[5]; // Put the objects on the heap papers[0] = new Circle(15.0); papers[1] = new Rectangle(34.0,2.0); papers[2] = new Circle(2.3); papers[3] = new Square(3.33); papers[4] = new Rectangle(5.61,7.92); ((Square *)papers[3])->addframe(2.5); for (int k=0; k<5; k++) { Shape * current = papers[k]; cout << "Area is " << current->getarea() << endl; } } // ---------------- makefile ----------------- HEADS=shape.h circle.h square.h rectangle.h hereitis: shape_main.o circle.o shape.o square.o rectangle.o $(HEADS) g++ -o hereitis shape_main.o circle.o shape.o square.o rectangle.o shape_main.o: shape_main.cpp $(HEADS) g++ -c shape_main.cpp shape.o: shape.cpp $(HEADS) g++ -c shape.cpp square.o: square.cpp $(HEADS) g++ -c square.cpp circle.o: circle.cpp $(HEADS) g++ -c circle.cpp rectangle.o: rectangle.cpp $(HEADS) g++ -c rectangle.cpp clean: @echo "Cleaning up ..." @rm -rf *.o @rm hereitis test test: hereitis ./hereitis @touch test // ----------- And some test runs of all of that! ----------- munchkin:c5 grahamellis$ make clean Cleaning up ... munchkin:c5 grahamellis$ make g++ -c shape_main.cpp g++ -c circle.cpp g++ -c shape.cpp g++ -c square.cpp g++ -c rectangle.cpp g++ -o hereitis shape_main.o circle.o shape.o square.o rectangle.o munchkin:c5 grahamellis$ make test ./hereitis Area is 706.858 Area is 68 Area is 16.619 Area is 33.9889 Area is 44.4312 munchkin:c5 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. Web site author
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.
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.php • PAGE BUILT: Sun Oct 11 14:50:09 2020 • BUILD SYSTEM: JelliaJamb |