Write definition of a class that has the following properties.
Question
Write definition of a class that has the following properties.
The name of the class is Sales.
The class Sales has the following member variables:
id (string)
year (int)
name (string),
amountSold (int),
The class Sales has the following member functions:
Constructors:
default constructor
overloaded constructor
Setters:
setId – sets the id
setYear – sets the year
setName – sets the name
setAmount – sets the amount sold
Getters:
getId – returns the id
getYear – returns the year
getName – returns the name
getAmount – returns the amount sold
Other functions:
display – outputs the data in the member variables, the commission percent and amount earned as shown below (see sample output).
detAmountEarned – determines the amount earned based upon their sales.
detCommissionPercent – determines the commission percent based on the following table:
SalesCommissionBelow $10003%$1001 – $50004.5%$5001 – $10,0005.25%over $10,0006%
Write definitions of all the member functions of class Sales.
Finish writing the test driver for this class
Sample Output:
Testing the first object:
ID: 20192785
Year: 2017
Name: Jane North
Amount Sold: $10000
Commission: 5.25%
Amount Earned: $525
Base Code:
Sales.h #ifndef SALES_H #define SALES_H #include <string> using std:: string; class Sales { private: /* Write you code here */ public: // constructors /* Write you code here */ // setters /* Write you code here */ // getters /* Write you code here */ // other functions /* Write you code here */ }; #endif Sales.cpp: #include "Sales.h" #include <iostream> #include <iomanip> #include <string> using namespace std; /~~~~~~ This is the default constructor; it sets everything to 0 or "". */ Sales::Sales() { /* Write you code here */ } /~~~~~~ This is an overloaded constructor. It sets the variables according to the parameters. */ Sales::Sales( /* Write you code here */) { /* Write you code here */ } /~~~~~~ This function displays the member variables, the commision percent and amount earned in a neat format. */ void Sales::display() const { /* Write you code here */ } /~~~~~~ This function determines the commision percent based on the following table: Sales Commission $0 - 1000 3% 1001 - 5000 4.5% 5001 - 10,000 5.25% over 10,000 6% */ double Sales::detCommissionPercent() const { double commission = 0; if ( amountSold > 0) { /* Write you code here */ } return commission; } /~~~~~~ This function determines the amount earned: it calls the detCommisionPercent) function. */ double Sales::detAmountEarned() const { double amountEarned = 0; /* Write you code here */ return amountEarned; } Main.cpp: /* Test Driver for the Sales class */ #include <iostream> #include <string> #include <iomanip> #include "Sales.h" using namespace std; int main() { // Create the first objec Sales one; // Call setters to assign values to one's data members one.setId("20192785"); one.setYear(2017); one.setName("Jane North"); one.setAmount(10000); // Display one's data calling getters cout << "Testing the first object:" << endl; cout << " ID: " << /* Write you code here */ << endl; cout << " Year: " << /* Write you code here */ << endl; cout << " Name: " << /* Write you code here */ << endl; cout << " mount Sold: $" << /* Write yor code here */<< endl; cout << " Commission: " << /* Write you code here */ << endl; cout << "Amount Earned: $" << /* Write you code here */<< endl; // Create the second object using the default constructor Sales two; // Display two's data members calling the display() function cout << "Testing the second object:" << endl; two.display(); // Create the third object using the overloaded construct Sales three("19278520", 2012, "Tim South", 12000); // Display three's data calling the display() function cout << "Testing the third object:" << endl; three.display(); return 0; }
Summary
Here main three files are created naming as Sales.h, Sales.cpp and main.cpp. Sales.h is here the header file for class sales. In the main.cpp there is a main method. Where every function is declared. There is a constructor and function in every header file.
Explanation
Here three different program has been written. Where each class has its own different header file. Name of the header file is Sales.h, Sales.cpp and main.cpp. There are a class Sales in the sales.h header file. So the data member of the class is name, year, id and getnd amountSold with the access identifier private. And also there are constructors, functions, setter and getter. After that in the next program, there is a new header file called Sales.cpp which includes the old header file sales.h. Next we define a definition of constructors and functions based on the user specification. After that there is a new class with a new header file where both the previous header files are added.
Sales.h
#ifndef SALES_H #define SALES_H #include <string> using std:: string; class Sales { private: /* Write you code here */ string id,name; int year,amountSold; public: // constructors Sales(); Sales(string i,int y,string n,int a); /* Write you code here */ // setters void setName(string n){ name=n; } void setId(string i){ id=i; } void setAmount(int a){ amountSold=a; } void setYear(int y){ year=y; } /* Write you code here */ // getters string getName(){ return name; } string getId(){ return id; } int getAmount(){ return amountSold; } int getYear(){ return year; } /* Write you code here */ // other functions /* Write you code here */ double detCommissionPercent() const; double detAmountEarned() const; void display() const; }; #endif
Sales.cpp
#include "Sales.h" #include <iostream> #include <iomanip> #include <string> using namespace std; /* This is the default constructor; it sets everything to 0 or "". */ Sales::Sales(){ /* Write you code here */ name="Pravallika"; id="3240"; amountSold=10000; year=2020; } /* This is an overloaded constructor. It sets the variables according to the parameters. */ Sales::Sales(string i,int y,string n, int a){ /* Write you code here */ name=n; id=i; amountSold=a; year=y; } /* This function displays the member variables, the commision percent and amount earned in a neat format. */ void Sales::display() const{ /* Write you code here */ cout<<"ID: "<<id<<endl; cout<<"Year: "<<year<<endl; cout<<"Name: "<<name<<endl; cout<<"Amount Sold: $"<<amountSold<<endl; cout<<"Commission: "<<detCommissionPercent()<<"%"<<endl; cout<<"Amount Earned: $"<<detAmountEarned()<<endl<<endl; } /* This function determines the commision percent based on the following table: Sales Commission $0 - 1000 3% 1001 - 5000 4.5% 5001 - 10,000 5.25% over 10,000 6% */ double Sales::detCommissionPercent() const { double commission = 0; if(amountSold>=0 && amountSold<=1000) commission=3; if(amountSold>=1001 && amountSold<=5000) commission=4.5; if(amountSold>=5001 && amountSold<=10000) commission=5.25; if(amountSold>=10001) commission=6; return commission; } /* This function determines the amount earned: it calls the detCommisionPercent) function. */ double Sales::detAmountEarned() const { double amountEarned = 0; /* Write you code here */ amountEarned= (amountSold*detCommissionPercent())/100; return amountEarned; }
Main.cpp
/* Test Driver for the Sales class */ #include "Sales.h" #include "Sales.cpp" #include <iostream> #include <string> #include <iomanip> using namespace std; int main() { // Create the first object Sales one; // Call setters to assign values to one's data members one.setId("20192785"); one.setYear(2017); one.setName("Jane North"); one.setAmount(10000); // Display one's data calling getters cout << "Testing the first object:" << endl; cout << " ID: " <<one.getId()<< endl; cout << " Year: " << one.getYear()<< endl; cout << " Name: " <<one.getName()<< endl; cout << " Amount Sold: $" <<one.getAmount()<<endl; cout << " Commission: " <<one.detCommissionPercent()<< endl; cout << "Amount Earned: $" <<one.detAmountEarned()<< endl; // Create the second object using the default constructor Sales two; // Display two's data members calling the display() function cout << "Testing the second object:" << endl; two.display(); // Create the third object using the overloaded constructor Sales three("19278520", 2012, "Tim South", 12000); // Display three's data calling the display() function cout << "Testing the third object:" << endl; three.display(); return 0; }