Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This is coded in C C++ in terminal..For this assignment, you will need to write three source code files as well as two header files.

This is coded in C C++ in terminal..For this assignment, you will need to write three source code files as well as two header files. Each of these files is relatively short, but many inexperienced programmers are overwhelmed by the idea of writing a program as multiple files. "Where do I start?!!" is a common refrain. This assignment sheet attempts to walk you through the steps of writing a multi-file program. The steps outlined below should not be thought of as a purely linear process, but rather an iterative one - For example, work a little on Step 1, then a little on Step 2, then test what you've written (Step 3). Step 1: Write the Seller class declaration The Seller class represents information about a salesperson. The code for the Seller class will be placed in two separate files, which is the norm for non-template C++ classes. The header file for a class contains the class declaration, including declarations of any data members and prototypes for the methods of the class. The name of the header file should be of the form ClassName.h (for example, Seller.h for the header file of the Seller class). A skeleton for the Seller.h file is given below. As shown, a header file should begin and end with header guards to prevent it from accidentally being #included more than once in the same source code file (which would produce duplicate symbol definition errors). The symbol name used in the header guards can be any valid C++ name that is not already in use in your program or the C/C++ libraries. Using a name of the format CLASSNAME_H (like SELLER_H in the code below) is recommended to avoid naming conflicts. #ifndef SELLER_H #define SELLER_H //***************************************************************** // FILE: Seller.h // AUTHOR: your name // LOGON ID: your DogTag // DUE DATE: due date of assignment // // PURPOSE: Contains the declaration for the Seller class. //***************************************************************** class Seller { // Data members and method prototypes for the Seller class go here . . . }; #endif Data Members The Seller class should have the following two private data members: A name (a character array with room for 30 characters PLUS the null character) A sales total (a double variable) Note: Make sure you code your data members in THE EXACT ORDER LISTED ABOVE and with THE EXACT SAME DATA TYPES. If you use float instead of double or only make the name array 30 characters long instead of 31, your final program will not work correctly. Method Prototypes The Seller class declaration should (eventually) contain public prototypes for all of the methods in the Seller.cpp source code file described in Step 2 below. Step 2: Write the Seller class implementation The source code file for a class contains the method definitions for the class. The name of the source code file should be of the form ClassName.cpp or ClassName.cc (for example, Seller.cpp for the source code file of the Seller class). The Seller class implementation should (eventually) contain definitions for all of the methods described below. Make sure to #include "Seller.h" at the top of this file. Seller default constructor This "default" constructor for the Seller class takes no parameters. Like all C++ constructors, it does not have a return data type. This method should set the name data member to a "null string". This can be done by copying a null string literal ("") into the character array using strcpy() or by setting the first element of the array to a null character ('\0'). The sales total data member should be set to 0. Alternate Seller constructor Write another constructor for the Seller class that takes two parameters: 1) a character array that contains a new name, and 2) a double variable that contains a new sales total. DO NOT GIVE THESE PARAMETERS THE SAME NAMES AS YOUR DATA MEMBERS.. Like all C++ constructors, this constructor does not have a return data type. Use strcpy() to copy the new name parameter into the name data member. Assign the new sales total parameter to the sales total data member. getName() This accessor method takes no parameters. It should return the name data member. In C++, the usual return data type specified when you are returning the name of a character array is char* or "pointer to a character" (since returning an array's name will convert the name into a pointer to the first element of the array, which in this case is data type char. getSalesTotal() This accessor method takes no parameters. It will have a return data type of double. It should return the sales total data member. setSalesTotal() This accessor method takes one parameter, a a double variable that contains a new sales total. DO NOT GIVE THIS PARAMETER THE SAME NAME AS YOUR DATA MEMBER.. The method returns nothing. It should assign the new sales total parameter to the sales total data member. print() This method takes no parameters and returns nothing. The method should print the name and sales total data members on the console using cout. Use setw() to line the printed values up in columns (a width of 30 for the name and 9 for the sales total will match the sample output). The name should be left-justified; the sales total should be right justified and printed using fixed-point notation with two places after the decimal point. Step 3: Test and debug the Seller class As you write your declaration and implementation of the Seller class, you should begin testing the code you've written. Create a basic main program called assign2.cpp that tests your class. This is not the final version of assign2.cpp that you will eventually submit. In fact, you'll end up deleting most (or all) of it by the time you're done with the assignment. An example test program is given below. You do not have to have written all of the methods for the Seller class before you begin testing it. Simply comment out the parts of your test program that call methods you haven't written yet. Write one method definition, add its prototype to the class declaration, uncomment the corresponding test code in your test program, and then compile and link your program. If you get syntax errors, fix them before you attempt to write additional code. A larger amount of code that does not compile is not useful - it just makes debugging harder! The goal here is to constantly maintain a working program. #include #include "Seller.h" using std::cout; using std::endl; int main() { // Test default constructor Seller seller1; // Test alternate constructor Seller seller2("Jones, Martin", 1234.56); // Test print() method and whether constructors // properly initialized objects cout << "Printing seller1 "; seller1.print(); cout << endl << endl; cout << "Printing seller2 "; seller2.print(); cout << endl << endl; // Test accessor methods cout << seller2.getName() << endl; cout << seller2.getSalesTotal() << endl; seller2.setSalesTotal(6543.21); cout << seller2.getSalesTotal() << endl; return 0; } Once your Seller class has been thoroughly tested and debugged, it's time to write the second class for this assignment. Step 4: Write the SalesDB class declaration The SalesDB class represents a database of Seller objects. Like the Seller class, the code for this class will be placed in two separate files. Place the class declaration in a header file called SalesDB.h. Like the file Seller.h you wrote in Step 1, this file should begin and end with header guards to prevent it from accidentally being #included more than once in the same source code file. After the header guard at the top of the file but before the class definition, make sure to #include "Seller.h". Data Members The SalesDB class should have the following two private data members: An array of 30 Seller objects An integer that specifies the number of sellers actually stored in the array Note: Once again, make sure you code your data members in THE EXACT ORDER LISTED ABOVE and with THE EXACT SAME DATA TYPES. Method Prototypes The SalesDB class declaration should (eventually) contain public prototypes for all of the methods in the SalesDB.cpp source code file described in Step 5 below. Step 5: Write the SalesDB class implementation The SalesDB class implementation should (eventually) contain definitions for all of the methods described below. Make sure to #include "SalesDB.h" at the top of this file. SalesDB default constructor This "default" constructor for the SalesDB class takes no parameters. Like all C++ constructors, it does not have a return data type. This constructor is called to create an empty database, so this method should set the number of sellers data member to 0. Alternate SalesDB constructor Write a second constructor for the SalesDB class that takes one parameter: a pointer to a constant character (which will point to an array of characters that contains the name of an existing database file). Like all C++ constructors, this constructor does not have a return data type. This constructor should do the following: 1. Declare an input file stream variable (the code below assumes it is named inFile). 2. Open the file stream for binary input. Check to make sure the file was opened successfully as usual. 3. Read the database file into your SalesDB object. You can do this with a single statement: 4. inFile.read((char*) this, sizeof(SalesDB)); Close the file stream. print() This method takes no parameters and returns nothing. This method should first print a descriptive header line (e.g., "Sales Database Listing"). It should then loop through the array of Seller objects and print each of the elements that contain seller data, one per line. Here we see some of the power of object-oriented programming: since each element of the array is an object, we can call a method for that object. We've already written a print() method for the Seller class, so printing an element of the array is as easy as calling print() for the array element. The syntax for calling a method using an array element that is an object is pretty straightforward: arrayName[subscript].methodName(arguments); Step 6: Write the main program Since most of the logic of the program is embedded in the two classes you wrote, the main() routine logic is extremely simple. Create a SalesDB object using the alternate constructor you wrote. Pass the filename string "salesdb" as an argument to the constructor. Call the print() method for the SalesDB object. Other Points Make sure to document your program according to the standards listed in the Course Notes book. In particular, each class method or function should have a documentation box describing its purpose, the input parameters (if any), and the return value (if any). There should also be a documentation box for the program as a whole.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions