My code is supposed to read 16 lines from a text file, and passing them into an array of 4 Objects, each with 4 attributes. The problem im encountering is that while everything seems to work fine on passing the text details to the array, the 1st array element of the last object in the array is not the one supposed to be! I am really stuck!
My code is :
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
int CDsize;
class CD {
public:
// constructors
CD();
CD(string arist, string title, int year, double price);
// getter methods
string getArtist() const { return this->artist; }
string getTitle() const { return this->title; }
int getYear() const { return this->year; }
double getPrice() const { return this->price; }
// setter methods - inline functions
void setArtist(const string artist) { this->artist = artist; }
void setTitle(const string title) { this->title = title; }
void setYear(const int year) { this->year = year; }
void setPrice(const double price) { this->price = price; }
// option methods
private:
string artist;
string title;
int year;
double price;
};
/*Text menu option 1/*
void printallcds(CD MAX_CDS[])
{
int d;
for (d=0; d<=CDsize; d++)
{
cout << "CD Number : " << d << "/ Artist : " << MAX_CDS[d].getArtist() << "/
Title : " << cout << MAX_CDS[d].getTitle() << "/ Year of Release : " <<
MAX_CDS[d].getYear() << "/ Price : " <<
cout << MAX_CDS[d].getPrice() << endl;
}
}*/
int main() {
CD MAX_CDS[3];
int CDsize = (sizeof(MAX_CDS) / sizeof(MAX_CDS[0]));
ifstream CDfile("mystock.txt");
string data;
int yeardata;
double pricedata;
int i;
for (i = 0; i < 4; i++) {
getline(CDfile, data);
MAX_CDS[i].setArtist(data);
getline(CDfile, data);
MAX_CDS[i].setTitle(data);
getline(CDfile, data);
stringstream yearstream(data);
yearstream >> yeardata;
MAX_CDS[i].setYear(yeardata);
getline(CDfile, data);
stringstream pricestream(data);
pricestream >> pricedata;
MAX_CDS[i].setPrice(pricedata);
}
CDfile.close();
// testing
cout << MAX_CDS[3].getArtist() << endl; // error !!!
cout << MAX_CDS[3].getTitle() << endl;
cout << MAX_CDS[3].getYear() << endl;
cout << MAX_CDS[3].getPrice() << endl;
return 0;
}
// constructors implementation
CD::CD() {}
CD::CD(string artist, string title, int year, double price) {
this->artist = artist;
this->title = title;
this->year = year;
this->price = price;
}
}
You only have space for 3 items in MAX_CDS (see CD MAX_CDS[3];), yet you're referencing the 4th item in your error.
MAX_CDS[3] // Actually represents the 4th item
Counting starts from 0 in C++.
So, to reference the 3rd item, in your case the last item, use MAX_CDS[2] or MAX_CDS[CDSize-1].
cout << MAX_CDS[CDSize-1].getArtist() << endl;
cout << MAX_CDS[CDSize-1].getTitle() << endl;
cout << MAX_CDS[CDSize-1].getYear() << endl;
cout << MAX_CDS[CDSize-1].getPrice() << endl;
Rereading your question, you probably want more items!
CD MAX_CDS[4]; // Now you have 4 items available: indexed 0, 1, 2, and 3
Related
The code compiles it does not read staff.txt. Not sure how to fix in the code. the error must be in reading the staff.txt of the code. Does anyone have an idea? output is in the image provided. Background on the question:
You are tasked to create 2 ADTs: customers and staff. Each customer will have a first and last name, an indication if the customer is enrolled in the “preferred” program, the car they are driving, and the amount of money owed to the company. On the other hand, each staff member will have a first and last name, the car they are driving, and their salary. In a real-world problem, there would obviously be more records associated with each staff member and customer.
ODU rentals would like you to automatically process txt files and print the information to the console screen. When you perform this process, ensure that your member variables are private. In addition, be sure to use getter and setter functions to manipulate and print your data. You may print your data in any format as long as it is organized. An example of correct output can be seen below.
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <cstdlib>
using namespace std;
class Customer {
string fname;
string lname;
int enroll;
string car;
float amount;
public:
//Defining the customer ADT
Customer();
void setFirstName(string fnm);
void setLastName(string lnm);
void setEnroll(int e);
void setCarName(string cnm);
void setAmount(float amt);
string getFirstName();
string getLastName();
int getEnroll();
string getCarName();
float getAmount();
void printData();
};
Customer::Customer() {
fname = lname = car = "";
enroll = 0;
amount = 0;
}
void Customer::setFirstName(string fnm) {
fname = fnm;
}
void Customer::setLastName(string lnm) {
lname = lnm;
}
void Customer::setEnroll(int e) {
enroll = e;
}
void Customer::setCarName(string cnm) {
car = cnm;
}
void Customer::setAmount(float amt) {
amount = amt;
}
string Customer::getFirstName() {
return(fname);
}
string Customer::getLastName() {
return(lname);
}
int Customer::getEnroll() {
return(enroll);
}
string Customer::getCarName() {
return(car);
}
float Customer::getAmount() {
return(amount);
}
void Customer:: printData() {
cout << left << setw(25) << getFirstName()+" "+getLastName() << left <<setw(15) << getEnroll();
cout << left << setw(15) << getCarName() << left << setw(15) << fixed << setprecision(2) << getAmount() << endl;
}
class Staff {
string fname;
string lname;
string car;
float salary;
public:
//Defining staff ADT
Staff();
void setFirstName(string fnm);
void setLastName(string lnm);
void setCarName(string cnm);
void setSalary(float sal);
string getFirstName();
string getLastName();
string getCarName();
float getSalary();
void printData();
};
Staff::Staff() {
fname = lname = car = "";
salary = 0;
}
void Staff::setFirstName(string fnm) {
fname = fnm;
}
void Staff:: setLastName(string lnm) {
lname = lnm;
}
void Staff::setCarName(string cnm) {
car = cnm;
}
void Staff::setSalary(float sal) {
salary = sal;
}
string Staff::getFirstName() {
return(fname);
}
string Staff::getLastName() {
return(lname);
}
string Staff::getCarName() {
return(car);
}
float Staff::getSalary() {
return(salary);
}
void Staff:: printData() {
cout << left << setw(25) << getFirstName()+" "+getLastName() << left << setw(15);
cout << getCarName() << left << setw(15) << fixed << setprecision(2) << getSalary() <<endl;
}
//Main function
//Read txt files
int main() {
string fnm, lnm, carnm;
float amt, sal;
int e, i=0, recno;
ifstream fstaff("staff.txt");
if(!fstaff) {
cout << "File can not be opened" << endl;
exit(1);
}
fstaff >> recno;
Staff s[recno];
cout << left<<setw(25) << "Name" << left << setw(15) << "Car" << left << setw(15) << "Salary" <<endl;
cout << "____________________________________________________________________________" << endl;
for(i=0; i<recno; i++) {
fstaff>>fnm>>lnm>>carnm>>sal;
s[i].setFirstName(fnm);
s[i].setLastName(lnm);
s[i].setCarName(carnm);
s[i].setSalary(sal);
s[i].printData();
}
fstaff.close();
ifstream fcust("customers.txt");
if(!fcust) {
cout << "File can not be opened"<<endl;
exit(1);
}
fcust >> recno;
Customer c[recno];
cout << endl << endl;
cout << left << setw(25) << "Name" << left << setw(15) << "Preferred" << left << setw(15) << "Car" << left << setw(15) << "Bill" << endl;
cout <<"____________________________________________________________________________"<<endl;
for(i=0; i<recno; i++) {
fcust >> fnm >> lnm >> e >> carnm >>amt;
c[i].setFirstName(fnm);
c[i].setLastName(lnm);
c[i].setEnroll(e);
c[i].setCarName(carnm);
c[i].setAmount(amt);
c[i].printData();
}
fcust.close();
return 1;
}
Code Output
I am unsure about why I am getting this error message when I hover over purchaseArray in my string Customer::save() method in the customer.cpp file:
identifier is undefined
And this error message when I hover over getline in the void Customer::parse(string line) method in the customer.cpp file:
no instance of overloaded function "getline" matches the argument list argument types are: (std::stringstream, int, char)
Code in the Customer.cpp file:
#include "Customer.h"
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include<utility>
using namespace std;
//default constructor
Customer::Customer() {
}
//Full constructor
Customer::Customer(int customerID, string title, string name, int numOfPurchases, int purchase1, int purchase2, int purchase3, string type)
{
this->customerID = customerID;
this->title = title;
this->name = name;
this->numOfPurchases = numOfPurchases;
purchases = new int[3];
purchases[0] = purchase1;
purchases[1] = purchase2;
purchases[2] = purchase3;
this->type = type;
}
Customer::Customer(const Customer& source) //copy constructor
{
cout << "copy constructor called" << endl;
this->customerID = source.customerID;
this->title = source.title;
this->name = source.name;
this->numOfPurchases = source.numOfPurchases;
this->purchases = new int[3];
purchases[0] = source.purchases[0];
purchases[1] = source.purchases[1];
purchases[2] = source.purchases[2];
this->type = source.type;
}
//overloaded assignment operator=
Customer& Customer::operator= (Customer& otherCustomer)
{
cout << "Overloaded assignment operator= called" << endl;
//self-assignment guard
if (this == &otherCustomer)
return *this; //refernce to the same object
// copy data from the source (rhs) to this object (the destination)
name = otherCustomer.name;
//must make a new scores object to store a copy of the other student
if (purchases != nullptr)
delete[] purchases;
purchases = new int[3];
for (int i = 0; i < 3; i++) {
purchases[i] = otherCustomer.purchases[i];
}
//return this existing object so we can chain this operator
return *this;
}
string Customer::save()
{
stringstream out;
out << this->customerID << ";";
out << this->title << ";";
out << this->name << ";";
out << this->numOfPurchases << ";";
int* purchases = 0;
purchases = purchases | (purchaseArray[0] << 24);
purchases = purchases | (purchaseArray[1] << 16);
purchases = purchases | (purchaseArray[2] << 8);
out << this->type << ";";
out.flush();
return out.str();
}
void Customer::parse(string line)
{
stringstream in(line);
string customerIDLine;
getline(in, customerIDLine, ';');
customerID = stoi(customerIDLine);
getline(in, title, ';');
getline(in, name, ';');
int numOfPurchases;
getline(in, numOfPurchases, ';');
int s = stoi(numOfPurchases);
purchasesArray[0] = (s & (255 << 16)) >> 16;
purchasesArray[1] = (s & (255 << 8)) >> 8;
purchasesArray[2] = s & 255;
getline(in, type, ';');
}
Customer::~Customer() {
cout << "Destructor ~Customer called" << endl;
delete[] purchases;
}
// Overloaded insertion operator (Outputs Character object data as an output stream)
// Defined in header file as a "friend" function, as it is not a member function
//
ostream& operator<<(ostream& out, Customer& customer)
{
cout << "Customer details ( output by insertion operator<< )" << endl;
cout << "Customer ID: " << customer.customerID << endl;
cout << "Title: " << customer.title << endl;
cout << "Name: " << customer.name << endl;
cout << "Number of purchases: " << customer.numOfPurchases << endl;
cout << "Purchases: ";
for (int i = 0; i < 3; i++)
{
if (i > 0) cout << ",";
cout << customer.purchases[i];
}
cout << "Type: " << customer.type << endl;
return out;
}
istream& operator>> (istream& in, Customer& customer)
{
cout << "Enter Customer details ( using the extraction operator>> )" << endl;
cout << "Enter Customer ID: " << endl;
cin >> customer.customerID;
cout << "Enter Title: " << endl;
getline(cin, customer.title);
cout << "Enter Name: " << endl;
getline(cin, customer.name);
cout << "Enter Number of Purchases: ";
cin >> customer.numOfPurchases;
cout << "Enter Purchases: ";
cin >> customer.purchases[0];
cin >> customer.purchases[1];
cin >> customer.purchases[2];
cout << "Enter Type";
getline(cin, customer.type);
cout << endl;
return in;
}
int Customer::getCustomerID()
{
return customerID;
}
string Customer::getTitle()
{
return title;
}
string Customer::getName()
{
return name;
}
int Customer::getNumOfPurchases()
{
return numOfPurchases;
}
int* Customer::getPurchases()
{
return purchases;
}
string Customer::getType()
{
return type;
}
void Customer::setCustomerID(int customerID)
{
if (customerID < 1) {
cout << "Customer ID has to be equal to 1 or more" << endl; //Changed all the "throw invalid_argument" messages to cout as they were causing an issue with my main.cpp file and an abort message kept appearing every time I ran my main.cpp file.
}
this->customerID = customerID;
}
void Customer::setTitle(string title)
{
if (title.length() < 2) {
cout << "Title has to be more than or equal to 2 characters" << endl;
}
this->title = title;
}
void Customer::setName(string name)
{
if (name.length() < 4) {
cout << "Length of name should be more than or equal to 4 characters" << endl;
}
this->name = name;
}
//Got help ith this on stack overflow as I was using "&&" instead of using "||" for the if statement
void Customer::setNumOfPurchases(int numOfPurchases)
{
if(numOfPurchases <0 || numOfPurchases > 10000){
cout << "Number of purchases should be between 0 to 10000" << endl;
}
this->numOfPurchases = numOfPurchases;
}
void Customer::setPurchases(int purchase1, int purchase2, int purchase3)
{
if (purchase1 < 0 || purchase2 < 0 || purchase3 < 0) {
cout << "Purchases must be more than or equal to zero" << endl;
}
}
//Got help from stack overflow on comparing strings as I originally didnt use "type.compare"
void Customer::setType(string type) {
if (type.compare("New") !=0 || type.compare("Either") !=0) {
cout << "Type of purchase has to be New or Either" << endl;
}
}
Code in the Customer.h file:
#pragma once
#include<iostream>
using namespace std;
#include<string>
class Customer
{
private:
int customerID;
string title;
string name;
int numOfPurchases;
int* purchases;
string type;
public:
Customer(); // default constructor
Customer(int customerID, string title, string name, int numOfPurchases, int purchase1, int purchase2, int purchase3, string type);
//copy overload assignment
Customer& operator=(Customer& otherCustomer);
Customer(const Customer& source);
string save();
void parse(string line);
~Customer(); //destructor
//Getters and Setters
void setCustomerID(int customerID);
void setTitle(string title);
void setName(string name);
void setNumOfPurchases(int numOfPurchases);
void setPurchases(int purchase1, int purchase2, int purchase3);
void setType(string type);
int getCustomerID();
string getTitle();
string getName();
int getNumOfPurchases();
int* getPurchases();
string getType();
void printCustomer() {
cout << customerID << "," << title << "," << name << "," << numOfPurchases << "," << purchases << "," << type << endl;
}
friend std::ostream& operator<<(std::ostream& out, Customer& customer); // overloaded operator<<
friend istream& operator>> (istream& in, Customer& customer); // overloaded operator >>
};
Code from the Main.cpp file:
// Repeat_Assessment_C++_AislingSmith.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
#include "Customer.h"
using namespace std;
void OutputFileStream();
void parseLine(const string& str);
void InputFileStream();
void save(vector<Customer> customers);
void load(vector<Customer>& customers);
void addCustomer(vector<Customer>& vect);
//void printActions();
void OutputFileStream()
{
cout << "Creating and writing to file: Customer.txt" << endl;
ofstream outStream("customers.txt"); // write mode (overwrites existing data)
if (outStream.good())
{
int customerID = 150033;
outStream << "This is a line of text.\n";
outStream << "This is another line of text.\n";
outStream << "This is a line of text.\n";
int numOfPurchases = 4;
int purchases = 0;
outStream << customerID << "Mr" << "Jack" << "New" << numOfPurchases << purchases << endl;
outStream.close(); // close file
cout << "File written.\n" << endl;
}
else
cout << "Unable to open file";
}
void parseLine(const string& str) {
stringstream strStream(str); //create string stream from the string
// int customerID;
string title;
string name;
string type;
//int numOfPurchases;
//int purchases;
string s;
int customerID = 150033;
getline(strStream, s, ';');
customerID = stoi(s);
getline(strStream, title, ';');
getline(strStream, name, ';');
getline(strStream, type, ';');
int numOfPurchases = 4;
getline(strStream, s, ';');
numOfPurchases = stoi(s);
int purchases = 0;
getline(strStream, s, ';');
purchases = stoi(s);
int* purchasesArray = new int[3];
purchasesArray[0] = (purchases & (255 << 16)) >> 16;
purchasesArray[1] = (purchases & (255 << 8)) >> 8;
purchasesArray[2] = purchases & 255;
for (int i = 0; i < 3; i++)
{
int purchasesArray[3];
}
cout << " CustomerID: " << customerID << "Title:" << title << " Name: " << name << " Type:" << type << " Number of Purchases: " << numOfPurchases << "Purchases: " << purchases << endl;
}
void InputFileStream() {
cout << "Reading from a semi-colon delimited txt file" << endl;
string line;
ifstream inStream("customers.txt"); //opens file as an input file stream
if (inStream.good()) //if the file is opened successfully and not empty
{
while (getline(inStream, line)) //reads line until false return
{
parseLine(line);
}
inStream.close();
}
else
cout << "unable to open file or the file is empty!";
}
void save(vector<Customer> customers)
{
ofstream out("customers.txt");
if(out)
{
for (Customer& c : customers)
{
out << c.save();
}
out.flush();
out.close();
}
else
{
cout << "Error Writing to File" << endl;
}
}
void load(vector<Customer>& customers)
{
ifstream in("customers.txt");
if (in) {
string line;
while (!in.eof())
{
getline(in, line);
if (line != "")
{
Customer c;
c.parse(line);
customers.push_back(c);
}
}
}
}
void addCustomer(vector<Customer>& customers) {
Customer customer;
cin >> customer;
customers.push_back(customer);
}
int main()
{
InputFileStream();
vector<Customer> customers;
Customer c;
Customer cust1;
cust1.setCustomerID(150032);
cust1.setTitle("Mr");
cust1.setName("Joey");
cust1.setNumOfPurchases(3);
cust1.setPurchases(366, 352, 334);
cust1.setType("New");
cout << cust1.getCustomerID() << endl;
cout << cust1.getTitle() << endl;
cout << cust1.getName() << endl;
cout << cust1.getNumOfPurchases() << endl;
cout << cust1.getPurchases() << endl;
cout << cust1.getType() << endl;
return 0;
}
First Problem
The first error is pretty clear, purchaseArray is nowhere defined (at least not in the code you show here) and you probably meant just purchases or this->purchases? Allthough I got no clue what you tried to achieve with the int *purchases there.
Second Problem
The second one occurs, because the way you try to read an integer from a stream is flawed.
It's about this piece of code from Customer::parse
int numOfPurchases; // declare int
getline(in, numOfPurchases, ';'); // error, second parameter must be std::string!
int s = stoi(numOfPurchases); // numOfPurchases is already an int
You probably wanted this:
std::string numOfPurchasesBuf; // temporary buffer
getline(in, numOfPurchasesBuf, ';'); // read data to string
int s = stoi(numOfPurchasesBuf); // convert to int
and since all of those lines read some member variable you probably want to set this one as well:
this->numOfPurchases = s;
As the title suggest, I am trying to sort a vector of objects by the GPA. My main issue is putting a method where I define the function for my comparison. The vector itself holds objects of five fields, which includes: string name, string ssn, string year, float credit, float gpa. I know theirs a way to use the sort operator. I am new to C++ so I am not very knowledgeable in this language. I appreciate the help! Here is the code:
#include <iostream>
#include <string>
#include <iterator>
#include <iomanip>
#include <fstream>
#include <vector>
#include <sstream>
#include <algorithm>
#include <cstdlib>
#include <list>
using namespace std;
class Student {
//declare local variables
protected:
string name; //people with names longer than 21 characters will just
have to make do
string ssn; // Social Secturity Number.
float gpa; //Most up to date gpa for the student
float credits; //Number of student's credit hours
//build public methods
public:
//Default Constructor
Student() {}
//Student constructor. Besides the character arrays, everything else is passed by reference.
Student(const string n, const string s, string sGPA, string sCredits) {
name = n;
ssn = s;
gpa = (float)atof(sGPA.c_str());
credits = (float)atof(sCredits.c_str());
}
string getName() {
return name;
}
string getSSN() {
return ssn;
}
float getGPA() {
return gpa;
}
float getCredit() {
return credits;
}
//a function that is expected to be implemented and overridden by subclasses
virtual void print() const {
cout << '\n' << endl;
cout << "Student's name: " << name << endl;
cout << "Student SSN: " << ssn << endl;
cout << "Student's current GPA: " << gpa << endl;
cout << "Student's credit hours: " << credits << endl;
}
// a pure virtual function for implementation later. Makes whole class Abstract
virtual float tuition() const = 0;
};
class Undergrad : public Student {
//declare local variables
protected:
float undergrad_rate = 380.0;
string year;
//build public methods
public:
//Default Constructor
Undergrad() {}
//Undergrad Constructor
Undergrad(const string n, const string s, string uGPA, string uCredits, string y) :
Student(n, s, uGPA, uCredits), year(y) {}
//Display the contents of undergrad
void print() const {
Student::print();
cout << "Undergrad Rate: " << undergrad_rate << endl;
cout << "Year: " << year << endl;
}
//Display undergrad's current year
string get_year() {
return year;
}
//Display the undergrad's current rate
float get_rate() {
return undergrad_rate;
}
//Set a undergrad's current year
void set_year(string y) {
year = y;
}
//Display the cost for an undergrad to attend university
float tuition() const {
return 1000000;
}
};
int main() {
ifstream ip("data.txt");
if (!ip.is_open()) std::cout << "ERROR: File not found" << '/n';
string name;
string ssn;
string year;
string credit;
string gpa;
list<Undergrad> myList;
list<Undergrad>::iterator i;
//Undergrad g(name, ssn, year, credit, gpa);
while (ip.good()) {
getline(ip, name, ',');
getline(ip, ssn, ',');
getline(ip, gpa, ',');
getline(ip, credit, ',');
getline(ip, year, '\n');
// float number = stoi(gpa);
//float number1 = stoi(credit);
Undergrad g(name, ssn, year, credit, gpa);
myList.push_back(g);
}
ip.close();
//This deletes the last object in the list and stores it in a temp object. It assigns that object to the beginning of the list.
Undergrad temp = myList.back();
myList.pop_back();
myList.insert(myList.begin(), temp);
/* for (int i = 0; i < myList.size(); i++) {
cout << "Name: " << myList[i].getName() << endl;
cout << "SSN: " << myList[i].getSSN() << endl;
cout << "Year: " << file[i].get_year() << endl;
cout << "Credit: " << file[i].getCredit() << endl;
cout << "GPA " << file[i].getGPA() << endl;
cout << " " << endl;
}
*/
/*for (Undergrad &x : myList) { //Goes through my list and displays its contents
x.print(); //This isn't bringing up errors.
}
*/
//This code copy the contents of the list to a vector.
std::vector<Undergrad> vect{ std::make_move_iterator(std::begin(myList)),
std::make_move_iterator(std::end(myList)) };
std::sort(vect.begin(), vect.end(), CompareGPA);
for (Undergrad &x : vect) {
x.print();
}
system("pause");
return 0;
}
Furthermore this is the code Im trying to implement to get the comparision
bool CompareGPA(const Student& left, const Student& right) {
return left.gpa > right.gpa;
}
Using lambda you can implement like this:Use a property int get_gpa() const to access the protected member.
std::sort(vect.begin(), vect.end(), [] (const Student& left, const Student& right)
{
return left.get_gpa() > right.get_gpa();
});
Here how to implement the property ( a member function to return the protected variable) :
int get_gpa() const
{
return gpa;
}
So I was able to fix it, however, the operator doesn't seem to be comparing them both since I always get false. There seems to be an error with the pLoan where it is not comparing both of them.
My code is
#include <string>
#include <iostream>
#include <sstream>
using namespace std;
//Vehicle Class
class Vehicle {
public:
Vehicle();
void setPrice(double a);
void setMpg(int a);
double getPrice() const;
int getMpg() const;
void printVehicle() const;
Vehicle(double price, int mpg);
private:
double price;
int mpg;
};
//Loan Class
class Loan {
public:
void setBank(string a);
void setLoan(double a);
string getBank() const;
double getLoan() const;
void printLoan() const;
Loan(string bank = "", double loan = 0);
private:
string bank;
double loan;
};
//Car Class
class Car : public Vehicle {
public:
Car(double price = 0, int mpg = 0, string bank = "", double loan = 0, string name = "", int element = 0);
void setName(string a);
void setLoan(string b, double l, int element);
string getName() const;
void printFull() const;
void setNbrOfLoans(int a);
int getNbrOfLoans() const;
Loan* getpLoan() const;
~Car();
private:
string name;
Loan* pLoan;
int nbrOfLoans;
};
bool operator==(const Car &car1, const Car &car2) {
Loan* pLoan1 = car1.getpLoan();
Loan* pLoan2 = car2.getpLoan();
return ((car1.getPrice() == car2.getPrice()) && (car1.getMpg() == car2.getMpg()) && (car1.getName() == car2.getName())
&& (car1.getNbrOfLoans() == car2.getNbrOfLoans()) &&
(pLoan1[0].getBank() == pLoan2[0].getBank()) && (pLoan1[0].getLoan() == pLoan2[0].getLoan()));
}
//Main
int main() {
Car car1(24800, 22, "Citi", 21600, "Mustang", 1);
Car* pCar1 = &car1;
pCar1->setLoan("Citi", 21600, 0);
pCar1->printFull();
pCar1->setNbrOfLoans(1);
Car car2;
Car* pCar2 = &car2;
cout << boolalpha;
cout << "Enter the price of the car: ";
double price;
cin >> price;
pCar2->setPrice(price);
cout << "Enter the mpg: ";
int mpg;
cin >> mpg;
pCar2->setMpg(mpg);
cout << "Enter the name of the car: ";
string name;
cin >> name;
pCar2->setName(name);
string bank;
double loan;
int index;
cout << "Enter the amount of loans you obtained: ";
cin >> index;
pCar2->setNbrOfLoans(index);
for (int i = 0; i < index; i++)
{
cout << "Enter the name of bank " << i + 1 << ": ";
cin >> bank;
cout << "Enter the amount of loan " << i + 1 << ": ";
cin >> loan;
pCar2->setLoan(bank, loan, i);
}
if (pCar1 == pCar2)
cout << "Cars are the same. ";
else
cout << "Cars are not the same. ";
cout << endl;
pCar2->printFull();
return 0;
}
////////////////////////////////////////////////////////////////////////////////////////
//Vehicle class function definitions
////////////////////////////////////////////////////////////////////////////////////////
Vehicle::Vehicle() {
price = 0;
mpg = 0;
}
Vehicle::Vehicle(double price, int mpg) {
price = price;
mpg = mpg;
}
void Vehicle::setPrice(double a) {
price = a;
}
void Vehicle::setMpg(int a) {
mpg = a;
}
double Vehicle::getPrice() const {
return price;
}
int Vehicle::getMpg() const {
return mpg;
}
void Vehicle::printVehicle() const {
cout << "Price: " << price << endl;
cout << "MPG: " << mpg << endl;
}
////////////////////////////////////////////////////////////////////////////////////////
//Loan Class function definitions
///////////////////////////////////////////////////////////////////////////////////////
Loan::Loan(string bank, double loan) {
Loan::bank = bank;
Loan::loan = loan;
}
void Loan::setBank(string a) {
bank = a;
}
void Loan::setLoan(double a) {
loan = a;
}
string Loan::getBank() const {
return bank;
}
double Loan::getLoan() const {
return loan;
}
void Loan::printLoan() const {
cout << "Bank: " << bank << endl;
cout << "Loan: " << loan << endl;
}
////////////////////////////////////////////////////////////////////////////////////
//Car Class function definitions
////////////////////////////////////////////////////////////////////////////////////
Car::Car(double price, int mpg, string bank, double loan, string name, int element) : Vehicle(price, mpg)
{
nbrOfLoans = element;
Car::name = name;
setMpg(mpg);
setPrice(price);
pLoan = new Loan[nbrOfLoans];
}
Loan* Car::getpLoan() const{
return pLoan;
}
void Car::setName(string a) {
name = a;
}
void Car::setLoan(string b, double l, int element) {
pLoan[element].setBank(b);
pLoan[element].setLoan(l);
}
string Car::getName() const {
return name;
}
int Car::getNbrOfLoans() const {
return nbrOfLoans;
}
void Car::setNbrOfLoans(int a) {
nbrOfLoans = a;
if (pLoan != NULL)
delete[] pLoan;
pLoan = new Loan[nbrOfLoans];
}
void Car::printFull() const {
cout << endl << "Car name: " << name << endl;
cout << "Price: " << getPrice() << endl;
cout << "MPG: " << getMpg() << endl;
for (int i = 0; i < nbrOfLoans; i++)
{
cout << "Loan #" << i + 1 << "." << endl;
cout << "Bank: " << pLoan[i].getBank();
cout << endl;
cout << "Loan amount: " << pLoan[i].getLoan();
cout << endl;
}
}
Car::~Car() {
delete[] pLoan;
}
Output:
IS always cars are not the same even when they are
Your main code is not calling your operator ==:
if (pCar1 == pCar2)
cout << "Cars are the same. ";
else
cout << "Cars are not the same. ";
here you're comparing the two pointers. To compare the two pointed-to objects you need
if (*pCar1 == *pCar2) ...
One more error:
pCar1->setLoan("Citi", 21600, 0);
pCar1->printFull();
pCar1->setNbrOfLoans(1);
setNbrOfLoans must be located before setLoan:
pCar1->setNbrOfLoans(1);
pCar1->setLoan("Citi", 21600, 0);
pCar1->printFull();
Im trying to create a percent savings program for a supermarket with 3 inherited classes (PersonData -> CustomerData -> PreferredCustomer). The entire program has to follow this UML diagram:
I am having problems with the third class; specifically initializing values in the first constructor of the class. Im getting an error message in visual studio: "redefinition of formal parameter Pa" and "redefinition of formal parameter "dl".
This is the constructor with the errors:
PreferredCustomer(string aLname, string aFname, string aAddress,
string aCity, string aState, int aZip, string aPhone, int cn,
bool ml, double Pa, double dl) // constructor 1
{
double Pa;
double dl;
}
The parameters in this third child constructor have been overloaded from the parent constructor. Arguments cn, ml, Pa, dl have all been added into this parameter.
I'm not sure why i am getting these errors? Also, should i be creating a virtual function or using pointers? Thank you for any help.
My program source code (for reference):
#include <iostream>
#include <string>
using namespace std;
class PersonData // PersonData parent class defined
{
private:
string lastName;
string firstName;
string address;
string city;
string state;
string phone;
int zip;
public:
PersonData() // Default Constructor initialization
{
lastName = " ";
firstName = " ";
address = " ";
city = " ";
state = " ";
zip = 0;
phone = " ";
}
PersonData(string aLname, string aFname, string aAddress, string aCity, string aState, int aZip, string aPhone) // Constructor 1
{
lastName = aLname;
firstName = aFname;
address = aAddress;
city = aCity;
state = aState;
zip = aZip;
phone = aPhone;
}
// Accesor Functions
string getLastName() const
{
return lastName;
}
string getFirstName() const
{
return firstName;
}
string getAddress() const
{
return address;
}
string getCity() const
{
return city;
}
string getState() const
{
return state;
}
int getZip() const
{
return zip;
}
string getPhone() const
{
return phone;
}
// Mutator Functions
void setLastName(string aLname)
{
lastName = aLname;
}
void setFirstName(string aFname)
{
firstName = aFname;
}
void setAddress(string aAddress)
{
address = aAddress;
}
void setCity(string aCity)
{
city = aCity;
}
void setState(string aState)
{
state = aState;
}
void setZip(int aZip)
{
zip = aZip;
}
void setPhone(string aPhone)
{
phone = aPhone;
}
};
class CustomerData :public PersonData // CustomerData child class of PersonData base class
{
private:
int customerNumber;
bool mailingList;
public:
CustomerData() // Default constructor
{
customerNumber = 0;
mailingList = 0;
}
CustomerData(int cNum, bool mailL) // Constructor 1
{
setCustomerNumber(cNum);
setMailingList(mailL);
}
// Accessor Functions for child class
int getCustomerNumber() const
{
return customerNumber;
}
bool getMailingList() const
{
if (mailingList != 0)
{
cout << "On Mailing List!: ";
return mailingList;
}
else (mailingList == 0);
{
cout << "Not on mailing list!: ";
return mailingList;
}
}
// Mutator Functions for child class
void setCustomerNumber(int cNum)
{
customerNumber = cNum;
}
void setMailingList(bool mailL)
{
mailingList = mailL;
}
};
class PreferredCustomer :public CustomerData // child class of CustomerData child class
{
private:
double purchasesAmount;
double discountLevel;
public:
PreferredCustomer() // default constructor
{
purchasesAmount = 0;
discountLevel = 0;
}
PreferredCustomer(string aLname, string aFname, string aAddress,
string aCity, string aState, int aZip, string aPhone, int cn,
bool ml, double Pa, double dl) // constructor 1
{
double Pa;
double dl;
}
// Mutator Functions
void setPurchasesAmount(double Pa)
{
purchasesAmount = Pa;
}
// Accessor Functions
double getPurchasesAmount() const
{
return purchasesAmount;
}
double getDiscountLevel() const
{
return discountLevel;
}
void addPurchase(double P) const
{
}
};
int main()
{
CustomerData Cdata; // for access of child class functions
PersonData Pdata; // for access of parent class functions
PreferredCustomer PCdata; // for access of preferred customer class functions
string temp1; // Temporary variable for string values
int temp2, // Temporary variable for integer values
max = 100, // For-loop maximum loop value
i; // i variable
bool temp3; // Temporary variable for bool values
for (i = 1; i <= max; i++)
{
// Input Data
cout << "Please input first Name: ";
getline(cin, temp1);
Pdata.setFirstName(temp1);
cout << "Please input last Name: ";
getline(cin, temp1);
Pdata.setLastName(temp1);
cout << "Please input address: ";
getline(cin, temp1);
Pdata.setAddress(temp1);
cout << "Please input city: ";
getline(cin, temp1);
Pdata.setCity(temp1);
cout << "Please input state: ";
getline(cin, temp1);
Pdata.setState(temp1);
cout << "Please input Zip code: ";
cin >> temp2;
Pdata.setZip(temp2);
cin.ignore(); // discards unread char from cin
cout << "Please input Phone Number: ";
getline(cin, temp1);
Pdata.setPhone(temp1);
cout << "Enter 1 to be included in mail list," << endl;
cout << "Enter 0 to not be included in mail list: ";
cin >> temp3;
Cdata.setMailingList(temp3);
Cdata.setCustomerNumber(i); // set customer number
cout << endl << endl;
cout << "Name: " << Pdata.getFirstName() << ", " << Pdata.getLastName() << " \n";
cout << "Customer Number: " << Cdata.getCustomerNumber() << "\n";
cout << "Address: " << Pdata.getAddress() << "\n";
cout << "City: " << Pdata.getCity() << "\n";
cout << "State: " << Pdata.getState() << "\n";
cout << "Phone: " << Pdata.getPhone() << "\n";
cout << "Zip: " << Pdata.getZip() << "\n";
cout << Cdata.getMailingList() << "\n";
cout << endl << endl;
cin.ignore(); // discards unread char from cin
}
char c;
cin >> c;
return 0;
}
A constructor's purpose is to take the parameters the are passed in and construct the object from the class declaration and initialize members as appropriate. It not only has to initialize the class but also all the base classes as well, this can be done by passing parameters to the base constructor(s).
In
PreferredCustomer(string aLname, string aFname, string aAddress,
string aCity, string aState, int aZip, string aPhone, int cn,
bool ml, double Pa, double dl) // constructor 1
{
double Pa;
double dl;
}
You aren't initializing anything
You are declaring two local parameters that match (and conflict with) the parameters passed in
You don't call the base's constructors (how most of the work is done)
PreferredCustomer(...) :CustomerData(...)
CustomerData(...) :PersonData(...)
You don't initialize this class's members
purchasesAmount = pa;
discountLevel = dl;