Language C++ The objective of the Final Project is to write a program which inco
ID: 3813783 • Letter: L
Question
Language C++
The objective of the Final Project is to write a program which incorporates several advanced concepts and corresponding C++ constructs, such as
Inheritance
Polymorphism
Containers and STL
Templates
Generic Algorithms
The Final Project application will use a collection of several types on elements placed in a class hierarchy, so we can leverage polymorphism to manage the items at a common, base-class denominator level, while also utilizing specialized aspects, unique to each type. In order to accomplish this, the container will have to store the base class pointers, as handles to sub-classed specialized items. The collection will be organized in a STL container and will use a generic algorithm to efficiently sort and search the container. The project will also incorporate a custom function template to capture an algorithm common for both sub-class types, but applied to a specific type.
The functionality, encapsulated in methods and invoked through the menu, will include the following,
Adding an item to the container
Sorting the container based on an items’ base class attribute (identifier, serial number, tag, etc.)
Searching for an item in the container. Prompt the user to select or enter an identifier.
Removing a located item from the container
Displaying the sorted list of items, detailing their attributes
The example provided in class modeled a “Bag of Pets” in the way described above. Other ideas of application that can be designed using a container of items include:
A Pet Hospital, where adding and removing an item would mean admitting and releasing a pet
A Rental Store, where inventory items can be rented and returned, and which would carry two related types of items (books and videos, or cars and trucks, skis and snowboards, etc.)
A Temp Agency, where you hire or release temporary employees
The “items” will have several attributes in common (a unique Serial Number/ID/tag, a name, a description, etc.) inherited from a base class, and also additional attributes and behaviors specific to item types.
PetsInBag.cpp
PetsInBag.h
PetsInBagApp.cpp
Explanation / Answer
C++ Standards
C++ is standardized as ISO/IEC 14882. Currently, there are two versions:
C++98 (ISO/IEC 14882:1998): 1st standard version of C++.
C++03 (ISO/IEC 14882:2003): minor "bug-fix" to C++98 with no change to the language. Commonly refer to as C++98/C++03 or
First C++ standard.
C++11 (ISO/IEC 14882:2011): 2nd standard version of C++. Formerly called C++0x, as it was expected to finalize in 200x.
It adds some new features to the language; more significantly, it greatly extends the C++ standard library and standard
template library (STL).
C++ Features
C++ is C. C++ supports (almost) all the features of C. Like C, C++ allows programmers to manage the memory directly, so as
to develop efficient programs.
C++ is OO. C++ enhances the procedural-oriented C language with the object-oriented extension. The OO extension
facilitates design, reuse and maintenance for complex software.
Template C++. C++ introduces generic programming, via the so-called template. You can apply the same algorithm to
different data types.
STL. C++ provides a huge set of reusable standard libraries, in particular, the Standard Template Library (STL).
C++ Strength and Pitfall
C++ is a powerful language for high-performance applications, including writing operating systems and their subsystems,
games and animation. C++ is also a complex and difficult programming language, which is really not meant for dummies.
For example, to effectively use the C++ Standard Template Library (STL), you need to understand these difficult concepts:
pointers, references, operator overloading and template, on top of the object-oriented programming concepts such as
classes and objects, inheritance and polymorphism; and the traditional constructs such as decision and loop. C++ is
performance centric. The C++ compiler does not issue warning/error message for many obvious programming mistakes,
undefined and unspecified behaviors, such as array index out of range, using an uninitialized variable, etc, due to the
focus on performance and efficiency rather than the ease of use - it assumes that those who choose to program in C++ are
not dummies.
2. Basic Syntaxes
2.1 Revision
Below is a simple C++ program that illustrates the important programming constructs (sequential flow, while-loop, and
if-else) and input/output. Read "Introduction To C++ Programming for Novices and First-time Programmers" if you need help
in understanding this program.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/*
* Sum the odd and even numbers, respectively, from 1 to a given upperbound.
* Also compute the absolute difference.
* (SumOddEven.cpp)
*/
#include <iostream> // Needed to use IO functions
using namespace std;
int main() {
int sumOdd = 0; // For accumulating odd numbers, init to 0
int sumEven = 0; // For accumulating even numbers, init to 0
int upperbound; // Sum from 1 to this upperbound
int absDiff; // The absolute difference between the two sums
// Prompt user for an upperbound
cout << "Enter the upperbound: ";
cin >> upperbound;
// Use a while-loop to repeatedly add 1, 2, 3,..., to the upperbound
int number = 1;
while (number <= upperbound) {
if (number % 2 == 0) { // Even number
sumEven += number; // Add number into sumEven
} else { // Odd number
sumOdd += number; // Add number into sumOdd
}
++number; // increment number by 1
}
// Compute the absolute difference between the two sums
if (sumOdd > sumEven) {
absDiff = sumOdd - sumEven;
} else {
absDiff = sumEven - sumOdd;
}
// Print the results
cout << "The sum of odd numbers is " << sumOdd << endl;
cout << "The sum of even numbers is " << sumEven << endl;
cout << "The absolute difference is " << absDiff << endl;
return 0;
}
Enter the upperbound: 1000
The sum of odd numbers is 250000
The sum of even numbers is 250500
The absolute difference is 500
Program Notes
using namespace std;
The names cout and endl belong to the std namespace. They can be referenced via fully qualified name std::cout and
std::endl, or simply as cout and endl with a "using namespace std;" statement. For simplicity, I shall use the latter
approach in this section. I will discuss the significance later.
return 0;
The return value of 0 indicates normal termination; while non-zero (typically 1) indicates abnormal termination.
C++ compiler will automatically insert a "return 0;" at the end of the the main() function, thus, it statement can be
omitted.
Instead of using numeric value of zero and non-zero, you can also use EXIT_SUCESS or EXIT_FAILURE, which is defined in
the cstdlib header (i.e., you need to "#include <cstdlib>".
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.