Basically my issue is with compositions. I understand the principle, but I'm struggling with the execution in one of the tests.
From the code of Computer and Monitor below, I have to create a final class Complect which will have its own name , the name of the computer, the name of the monitor, and a price which will be made up from the price() functions.
Computer.h
#ifndef COMPUTER_H
#define COMPUTER_H
#include <string>
class Computer{
public:
Computer(std::string name, int ram, double price);
std::string name() const;
int ram() const;
double price() const;
void printComputer() const;
void setComputer(std::string name, int ram, double price);
private:
std::string its_name;
int ram_gb;
double cost_price;
};
#endif // COMPUTER_H
Computer.cpp
#include "Computer.h"
#include <iostream>
Computer::Computer(std::string name, int ram, double price)
: its_name(name), ram_gb(ram), cost_price(price){
}
std::string Computer::name() const {
return its_name;
}
int Computer::ram() const {
return ram_gb;
}
double Computer::price() const {
return cost_price;
}
void Computer::printComputer() const{
std::cout << "Computer name = " <<name() <<"\n"
<< "Computer RAM = " <<ram() <<" GB\n"
<< "Computer Price = " << price() <<" EUR \n";
}
Monitor.h
#ifndef MONITOR_H
#define MONITOR_H
#include <string>
class Monitor{
public:
Monitor(std::string name, std::string type, double price);
std::string name() const;
std::string type() const;
double price() const;
//print computer
void printMonitor() const;
//set computer
void setMonitor(std::string name, std::string type, double price);
private:
std::string its_name;
std::string type_set;
double cost_price;
};
#endif // MONITOR_H
Monitor.cpp
#include "Monitor.h"
#include <iostream>
Monitor::Monitor(std::string name, std::string type, double price) : its_name(name), type_set(type), cost_price(price){
}
std::string Monitor::name() const {
return its_name;
}
std::string Monitor::type() const{
return type_set;
}
double Monitor::price() const {
return cost_price;
}
void Monitor::printMonitor() const{
std::cout << "Monitor name = " <<name() <<"\n"
<< "Monitor type = " <<type() <<"\n"
<< "Monitor price = " << price() <<" EUR \n";
}
Here is the class that I have made:
Complect.h
#ifndef COMPLECT_H
#define COMPLECT_H
#include <string>
class Complect{
public:
Complect(std::string name, std::string computername, std::string monitorname, double price);
std::string name() const;
std::string computername() const;
std::string monitorname() const;
double price() const;
void printComplect();
void setComplect(std::string name, std::string computername, std::string monitorname, double price);
private:
std::string complect_name;
std::string computername_final;
std::string monitorname_final;
double cost_price;
};
#endif // COMPLECT_H
Complect.cpp
#include "Complect.h"
#include "Monitor.h"
#include "Computer.h"
#include <iostream>
Complect::Complect(std::string name, std::string computername, std::string monitorname, double price) :
complect_name(name), computername_final(computername), monitorname_final(monitorname), cost_price(price){
}
std::string Complect::name() const{
return complect_name;
}
std::string Complect::computername() const{
return computername_final;
}
std::string Complect::monitorname() const{
return monitorname_final;
}
double Complect::price() const{
return cost_price;
}
void Complect::printComplect(){
std::cout << "Complect name = " << name() <<"\n"
<< "Computer name = " <<computername() <<"\n"
<<"Monitor name = " <<monitorname() <<"\n"
<<"Complect price = " <<price() <<" EUR \n";
}
Here is how I use the classes in Main.cpp
#include <iostream>
#include "Computer.h"
#include "Monitor.h"
#include "Complect.h"
int main(){
Computer asus("Asus One", 8, 545.95) ;
asus.printComputer() ;
std::cout << "\n";
Monitor iiyama("Iiyama Blackhawk 27inch", "LED", 299.99);
iiyama.printMonitor();
std::cout <<"\n";
Complect numberOne ("Number one complect", asus.name(), iiyama.name(), iiyama.price() + asus.price());
numberOne.printComplect();
std::cout <<"\n";
system ("pause");
return 0;
}
The end result is what it should be, so this code works.
But the issue with this is that it's incorrectly structured.
In the main.cpp file you will see that the Complect object is being created. But I currently provide all of the informations of that object at construction in main.cpp file.
Sorry the codes a bit messy, but im trying to wrap my head around this and struggling at the moment... How to make the class in complect.cpp file provide itself all its information ?
Currently you do not use composition but copy the attributes from the two other classes and you do a lot of work in the call of the constructor from main
A first little change from your code is to get instances of Computer and Monitor in parameter in the constructor:
Complect(std::string name, const Computer &, const Monitor &);
of course the final price is also computed inside that constructor, in main you just create a Complect with its name and its parts :
Complect numberOne ("Number one complect", asus, iiyama);
Now main fortunately does not have to know how the price is computed, else imagine if the formula change and you have to update all the calls of the constructor :-(. The way the price is computed is only the responsibility of Complect.
Complect numberOne ("Number one complect", asus, iiyama);
More than the constructor setComplect must be updated to receive the price of the monitor and computer separated for the same reason.
void setComplect(std::string name, std::string computername, std::string monitorname, double computerprice, double monitorprice);
or probably better to replace it by the methods
void setname(std::string name);
void setcomputer(std::string computername, double computerprice);
void setmonitor(std::string monitorname, double monitorprice);
But to duplicate all the attributes of Computer and Monitor in Complect is not practical, and you do not have composition to the instances. A first possibility is to save a copy of them :
class Complect{
...
private:
Computer computer;
Monitor monitor;
// does not need attribute "double cost_price;"
...
};
Complect::Complect(std::string name, const Computer & c, const Monitor & m)
: complect_name(name), computer(c), monitor(m) {
}
std::string Complect::computername() const{
return computer.name();
}
std::string Complect::monitorname() const{
return monitor.name();
}
double Complect::price() const{
return computer.price() + monitor.price();
}
void Complect::printComplect(){
std::cout << "Complect name = " << name() <<"\n"
<< "Computer name = " << computer.name() <<"\n"
<<"Monitor name = " << monitor.name() <<"\n"
<<"Complect price = " << price() <<" EUR \n";
}
The advantage of that solution is you are not impacted if the initial instances of Monitor and Computer disappear. The disadvantage is for instance the price is not updated if the price of one of the cloned part changes except by calling setXXX
An other way is to not clone the Monitor and Conputer, but you cannot have just that :
class Complect{
...
private:
Computer & computer;
Monitor & monitor;
// does not need attribute "double cost_price;"
...
};
Complect::Complect(std::string name, const Computer & c, const Monitor & m)
: complect_name(name), computer(c), monitor(m) {
}
because that supposes the instances of Monitor and Computer still exist while the corresponding instance of Complect exists
Fortunately C++ offers interesting features to manage that
Related
I have a set of a custom class and when I'm trying to insert an object of that class, the terminal gives me an error:
#ifndef EMPLOYEE_HH
#define EMPLOYEE_HH
#include <string>
#include <iostream>
#include <set>
#include <iterator>
using namespace std ;
class Employee {
public:
// Constructor
Employee(const char* name, double salary) : _name(name), _salary(salary) {}
// Accessors
const char* name() const { return _name.c_str() ; }
double salary() const { return _salary ; }
// Print functions
void businessCard(ostream& os = cout) const {
os << " +------------------+ " << endl
<< " | ACME Corporation | " << endl
<< " +------------------+ " << endl
<< " " << name() << endl ;
}
private:
string _name ;
double _salary ;
} ;
class Manager : public Employee{
public:
Manager(const char* name, double salary) : Employee(name, salary) {};
void addSubordinate(Employee& empl){
_subs.insert(empl);
}
const set<Employee*>& listOfSubordinates() const{
return _subs;
}
void businessCard() const{
Employee::businessCard();
set <Employee*>::iterator it=_subs.begin();
cout <<"Managed employees: " <<endl;
while(it!=_subs.end()){
cout <<*it++ <<endl;
}
}
private:
set <Employee*> _subs;
};
#endif
The addSubordinate() routine:
void addSubordinate(Employee& empl){
_subs.insert(empl);
}
returns this error:
no instance of overloaded function "std::set<_Key, _Compare, _Alloc>::insert [with _Key=Employee *, _Compare=std::less<Employee *>, _Alloc=std::allocator<Employee *>]" matches the argument list
I have tried to overload the operator < as other people suggested in response to similar questions, but that doesn't seem to solve the issue.
Your set accepts a pointer to an Employee, but you are trying to insert the object itself.
What you can do is
void addSubordinate(Employee& empl){
_subs.insert(&empl); // This will store the address to the object
}
or accept a pointer itself
void addSubordinate(Employee* empl){
_subs.insert(empl);
}
You need to define the < operator in the Employee class. The items in a set are always sorted and since you don't have a comparator it won't let you insert an instance of that class.
Try something like this:
bool operator<(const Employee& emp){
return salary() < emp.salary();
}
When putting freeSeats to 0, my code still says that a person has avalibale seats in his/hers car.
I have created two classes. One for Car and one for Person. The Car class has a function to see if there are free seats in the car. A person-object can have a car. When checking if the person has avalibale seats, my code responds "Yes" even though I give input "0". Why?
#pragma once
#include <iostream>
//Here is class Car declaration
class Car {
private:
unsigned int freeSeats;
public:
bool hasFreeSeats() const;
void reserveFreeSeat();
Car( unsigned int freeSeats);
};
//Here is function definition
#include "Car.h"
bool Car::hasFreeSeats() const {
if (freeSeats > 0)
return true;
return false;
}
void Car::reserveFreeSeat() {
--freeSeats;
}
Car::Car(unsigned int freeSeas) :
freeSeats{ freeSeats }
{
}
//Here is class Person declaration
class Person {
private:
std::string name;
std::string email;
Car *car; //pointer to a car
public:
Person(std::string name, std::string email, Car *car = nullptr);
std::string getName() const;
std::string getEmail() const;
void setEmail();
bool hasAvalibaleSeats() const;
friend std::ostream& operator << (std::ostream& os, const Person& p);
};
//Here is function definition
Person::Person(std::string name, std::string email, Car *car) :
name{ name }, email{ email }, car{ car }
{
}
std::string Person::getName() const {
return name;
}
std::string Person::getEmail() const {
return email;
}
void Person::setEmail() {
std::string newEmail;
std::cout << "What is the e-mail adress?";
std::cin >> newEmail;
email = newEmail;
std::cout << "E-mail has been set." << std::endl;
}
bool Person::hasAvalibaleSeats() const {
if (car != nullptr) { //check if there is a car
return car->hasFreeSeats();
}
return false;
}
std::ostream& operator << (std::ostream& os, const Person& p) {
std::string seats = "No";
if (p.hasAvalibaleSeats())
seats = "Yes";
return os << "Name: " << p.name << "\nE-mail: " << p.email << "\nHas free seats: " << seats << std::endl;
}
//From main im calling
#include "Car.h"
#include "Person.h"
int main() {
Car ferrari{ 2 };
Car bugatti{ 3 };
Car jeep{0};
Person one{ "Aleksander","aleks#aleks.com", &ferrari };
Person two{ "Sara","sara#sara.com", &bugatti };
Person three{ "Daniel", "daniel#daniel.com", &jeep };
Person four{ "Chris", "chris#chris.com" };
std::cout << one << std::endl;
std::cout << two << std::endl;
std::cout << three << std::endl;
std::cout << four << std::endl;
system("pause");
return 0;
}
I get
Name: Aleksander
E-mail: aleks#aleks.com
Has free seats: Yes
Name: Sara
E-mail: sara#sara.com
Has free seats: Yes
Name: Daniel
E-mail: daniel#daniel.com
Has free seats: Yes
Name: Chris
E-mail: chris#chris.com
Has free seats: No
But I want Daniel has free seats to be "No"
There's a typo here:
Car::Car(unsigned int freeSeas) :
freeSeats{ freeSeats }
{}
You wrote freeSeas instead of freeSeats. Due to that, the freeSeas parameter is unused and freeSeats{ freeSeats } does nothing as freeSeats is refering to the member variable, not the parameter.
Debugging is way easier when you enable compiler warnings. Compiler is your friend, and will help you immensely if you are willing to hear it.
For example, gcc gave me the following warnings when compiling your code:
prog.cc: In constructor 'Car::Car(unsigned int)':
prog.cc:37:23: warning: unused parameter 'freeSeas' [-Wunused-parameter]
Car::Car(unsigned int freeSeas) :
~~~~~~~~~~~~~^~~~~~~~
prog.cc: In constructor 'Car::Car(unsigned int)':
prog.cc:38:16: warning: '*<unknown>.Car::freeSeats' is used uninitialized in this function [-Wuninitialized]
freeSeats{ freeSeats }
^~~~~~~~~
I don't have to understand everything, but it tells me 2 things:
There is unused argument (why? it is used to initialize...)
Variable is initialized with uninitialized value (why?)
It made me look closer at this constructor and then you can see the typo.
My program is supposed to print a "to" and "from" address sourced from the EndPoint toString method but I can't quite figure out how to implement it. Here is my code. How do I get the toString method in the Package::Package constructor to print the contents of the EndPoint's toString method?
// ShippingProgram.cpp :
//
#include "stdafx.h"
#include <iomanip>
#include <stdexcept>
#include <sstream>
#include <iostream>
#include "Package.h" // Package class definition
using namespace std;
// constructor
EndPoint::EndPoint(const string& nameInfo, const string& addressInfo, const string& cityInfo, const string& stateInfo, int zipInfo) {
name = nameInfo;
address = addressInfo;
city = cityInfo;
state = stateInfo;
zip = zipInfo;
}
string EndPoint::toString() const {
ostringstream output;
output << fixed << setprecision(2); // two digits of precision
output << name << "\n" << address << "\n" <<
city << ", " << state << " " << zip;
return output.str();
}
Package::Package(EndPoint senderInfo, EndPoint receiverInfo, double weightInfo, double costPerOzInfo) {
weight = weightInfo; // should validate
costPerOz = costPerOzInfo;
}
void Package::calculateCost(double)
{
}
double Package::calculateCost() const {
return weight * costPerOz;
}
string Package::toString() const {
ostringstream output;
output << fixed << setprecision(2); // two digits of precision
output << "\nFrom:\n" << senderInfo.toString() << "\n\nTo:\n" << receiver <<
"\n\nWeight: " << weight << endl <<
"\nShipping cost:\n" << calculateCost();
return output.str();
}
Main Method:
#include <iostream>
#include <iomanip>
#include "stdafx.h"
//#include "TwoDayPackage.h"
//#include "OvernightPackage.h"
#include "Package.h"
using namespace std;
int main()
{
// Three address records.
EndPoint homer{ "Homer Simpson", "742 Evergreen Terrace", "Springfield",
"FL", 32401 };
EndPoint donald{ "Donald Duck", "1313 Webfoot Walk", "Duckburg",
"CA", 95501};
EndPoint kermit{ "Kermit Frog", "On the Swamp", "Leland", "MS", 38756 };
// This calls the base class constructor (regular fee).
Package regular{ homer, donald, 25.0, 0.20 };
// Defines output precision for floating point numbers (iomanip).
// cout << fixed << setprecision(2);
// Prints package parameters.
cout << "Regular package processed." << endl;
cout << regular.toString();
cout << "Shipping Cost: $" << regular.calculateCost() << endl << endl;
cout << homer.toString();
// First derived class (two-day fee added).
/* TwoDayPackage twoday{ donald, kermit, 17.5, 0.20, 2.0 };
cout << "Two-day package processed." << endl;
cout << twoday.toString();
cout << "Shipping Cost: $" << twoday.calculateCost() << endl << endl;
// Second derived class (overnight fee added).
OvernightPackage overnight{ kermit, homer, 14.2, 0.20, 0.50 };
cout << "Overnight package processed." << endl;
cout << overnight.toString();
cout << "Shipping Cost: $" << overnight.calculateCost() << endl << endl;
*/
}
This project requires that I create a shipping program with inheritance. It must include a "EndPoint" class that is private and contains the sender and receiver info and a "Package" class that compiles everything and puts it to string.
My Errors are with how in the world I get my Package constructor to be able to contain the information from my EndPoint class. Since the main method is formatted where the Package class must be (EndPoint, EndPoint, Weight, Cost) but it doesn't compile like that. I guess I just don't understand how to send the EndPoint info to the Package objects.
Here are my errors:
No instance of constructor "Package::Package" matches the argument list argument types are: (EndPoint, EndPoint, double, double)
Error C2440 'initializing': cannot convert from 'initializer list' to 'Package'
Error C3861 'setprecision': identifier not found
Package.h
#pragma once
#ifndef PACKAGE_H
#define PACKAGE_H
#include <string>
#include <iostream>
using namespace std;
class EndPoint {
public:
EndPoint(const std::string&, const std::string&, const std::string&, const std::string&, int = 0.0);
void setName(const std::string&);
std::string getName() const;
void setAddress(const std::string&);
std::string getAddresss() const;
void setCity(const std::string&);
std::string getCity() const;
void setState(const std::string&);
std::string getState() const;
void setZip(int);
int getZip() const;
string toString() const;
protected:
std::string name;
std::string address;
std::string city;
std::string state;
int zip;
};
class Package {
public:
string toString() const;
Package(const std::string&, const std::string&, double = 0.0, double = 0.0);
void setSender(const std::string&);
std::string getSender() const;
void setReceiver(const std::string&);
std::string getReceiver() const;
void setWeight(double);
double getWeight() const;
void setCostPerOz(double);
double getCostPerOz() const;
void calculateCost(double);
double calculateCost() const;
double calculateCost(double weight, double costPerOz)
{
double shipping;
shipping = weight * costPerOz;
cout << "The Base Cost = " << shipping << endl << endl;
return shipping;
}
protected:
std::string sender;
std::string receiver;
double weight; // gross weekly sales
double costPerOz; // commission percentage
};
#endif
Package.cpp
#include "stdafx.h"
#include <iomanip>
#include <stdexcept>
#include <sstream>
#include "Package.h" // Package class definition
using namespace std;
// constructor
EndPoint::EndPoint(const string& nameInfo, const string& addressInfo, const string& cityInfo, const string& stateInfo, int zipInfo) {
name = nameInfo;
address = addressInfo;
city = cityInfo;
state = stateInfo;
zip = zipInfo;
}
void EndPoint::setName(const string& nameInfo) {
name = nameInfo;
}
string EndPoint::getName() const { return name; }
void EndPoint::setAddress(const string& addressInfo) {
address = addressInfo;
}
string EndPoint::getAddresss() const { return address; }
void EndPoint::setCity(const string& cityInfo) {
city = cityInfo;
}
string EndPoint::getCity() const { return city; }
void EndPoint::setState(const string& stateInfo) {
state = stateInfo;
}
string EndPoint::getState() const { return state; }
void EndPoint::setZip(int zipInfo) {
zip = zipInfo;
}
int EndPoint::getZip() const {
return zip;
}
string EndPoint::toString() const {
ostringstream output;
output << fixed << setprecision(2); // two digits of precision
output << name << "\n" << address << "\n" <<
city << ", " << state << " " << zip;
return output.str();
}
string EndPoint::getState() const { return state; }
Package::Package(const string& senderInfo, const string& receiverInfo, double weightInfo, double costPerOzInfo) {
sender = senderInfo; // should validate
receiver = receiverInfo; // should validate
weight = weightInfo; // should validate
costPerOz = costPerOzInfo;
}
void Package::setSender(const string& senderInfo) {
sender = senderInfo; // should validate
}
string Package::getSender() const { return sender; }
void Package::setReceiver(const string& receiverInfo) {
receiver = receiverInfo; // should validate
}
string Package::getReceiver() const { return receiver; }
void Package::setWeight(double weightInfo) {
if (weightInfo < 0.0) {
throw invalid_argument("The package weight must be >= 0.0");
}
weight = weightInfo;
}
double Package::getWeight() const { return weight; }
void Package::setCostPerOz(double costPerOzInfo) {
costPerOz = costPerOzInfo;
}
double Package::getCostPerOz() const {
return costPerOz;
}
double Package::calculateCost() const {
return weight * costPerOz;
}
string Package::toString() const {
ostringstream output;
output << fixed << setprecision(2); // two digits of precision
output << "From:\n" << sender << "\n\nTo:\n" << receiver <<
"\n\nWeight: " << weight << endl <<
"\nShipping cost: " << calculateCost();
return output.str();
}
Main.cpp
#include <iostream>
#include <iomanip>
#include "stdafx.h"
//#include "TwoDayPackage.h"
//#include "OvernightPackage.h"
#include "Package.h"
using namespace std;
int main()
{
// Three address records.
EndPoint homer{ "Homer Simpson", "742 Evergreen Terrace", "Springfield",
"FL", 32401 };
EndPoint donald{ "Donald Duck", "1313 Webfoot Walk", "Duckburg",
"CA", 95501};
EndPoint kermit{ "Kermit Frog", "On the Swamp", "Leland", "MS", 38756 };
// This calls the base class constructor (regular fee).
Package regular{ homer, donald, 25.0, 0.20 };
// Defines output precision for floating point numbers (iomanip).
cout << fixed << setprecision(2);
// Prints package parameters.
cout << "Regular package processed." << endl;
cout << regular.toString();
cout << "Shipping Cost: $" << regular.calculateCost() << endl << endl;
// First derived class (two-day fee added).
/* TwoDayPackage twoday{ donald, kermit, 17.5, 0.20, 2.0 };
cout << "Two-day package processed." << endl;
cout << twoday.toString();
cout << "Shipping Cost: $" << twoday.calculateCost() << endl << endl;
// Second derived class (overnight fee added).
OvernightPackage overnight{ kermit, homer, 14.2, 0.20, 0.50 };
cout << "Overnight package processed." << endl;
cout << overnight.toString();
cout << "Shipping Cost: $" << overnight.calculateCost() << endl << endl;
*/
}
I have commented out blocks of code here as I am trying to just get the first part to work before diving into the rest.
Edit:
Thank you all for the advice! I have made some edits and taken out a ton of extra code (getters and setter. I learned with java...) and I have gotten the program to compile and work as intended save for a small but important issue.
No instance of constructor "Package::Package" matches the argument
list argument types are: (EndPoint, EndPoint, double, double)
in your code:
Package regular{ homer, donald, 25.0, 0.20 }; you are passing wrong variables to the constructor's parameters which is an Endpoint object for first and second parameter
what you have is:
Package(const std::string&, const std::string&, double = 0.0, double = 0.0);
which accepts a std::string object for first and second parameter.
cannot convert from 'initializer list' to 'Package
fixing problem 1 will fix this
i dont know why you get 3rd one since you have iomanip in your main
I'm reading a book on C++ and was writing out some code to practice using the interface and implementation of a class. I've searched for solutions to my issue for a while to no avail.
I have a class with an enumeration inside of it. While trying to instantiate an object of that class, I am having trouble accessing the enum types outside of the class. I have tried using Book::Horror, Biblo::Horror, Biblo::Book::Horror, Horror, and even things like Biblo::Book::Genre::Horror. Can't seem to get it to let me access the types of the enum for the instantiation of my object in the main.cpp file.
Any help is appreciated! The more complex uses of C++ are still new to me. Here is my source:
book.h
#include <iostream>
#include <string>
using namespace std;
namespace Biblo{
class Book{
public:
enum Genre{
No_Genre, Horror, Comedy, Romance, Mystery
};
// The rest of this header is working fine I think, just this enum
class Invalid{}; // Used for throwing errors
Book(int n_ISBN, int n_copyrightYear, string n_title, string n_author, Genre n_genre);
Book();
// Accessors (non-modifying)
int getISBN() const { return ISBN; }
int getCopyrightYear() const { return copyrightYear; }
string getTitle() const { return title; }
string getAuthor() const { return author; }
string getGenre() const;
// Mutators
void changeAuthor(string newAuthor);
private:
int ISBN;
int copyrightYear;
string title;
string author;
Genre genre;
}; // End Book
// Helper Functions go here
bool operator==(const Book& a, const Book& b);
bool operator!=(const Book& a, const Book& b);
} // End Biblo
and main.cpp
#include <iostream>
#include "book.h"
using namespace std;
int main()
{
Biblo::Book book(100, 2012, "The Walrus", "The Eggman", Book::Horror); // THIS LINE GIVES ERROR
cout << "ISBN: " << book.getISBN() << endl;
cout << "Copyright: " << book.getCopyrightYear() << endl;
cout << "Title: " << book.getTitle() << endl;
cout << "Author: " << book.getAuthor() << endl;
cout << "Genre: " << book.getGenre() << endl;
return 0;
}
Edit: here is the book.cpp file
#include <iostream>
#include "book.h"
#include <string>
namespace Biblo{
Book::Book(int n_ISBN, int n_copyrightYear, string n_title, string n_author, Genre n_genre)
:ISBN(n_ISBN), copyrightYear(n_copyrightYear), title(n_title), author(n_author), genre(n_genre)
{
// constructor
}
Book::Book()
:ISBN(0), copyrightYear(0), title(""), author(""), genre(Genre::No_Genre)
{
// Default constructor
}
// Accessors
string Book::getGenre() const
{
if (Book.genre == Genre::No_Genre)
return "No Genre!";
if (Book.genre == Genre::Horror)
return "Horror";
if (Book.genre == Genre::Comedy)
return "Comedy";
if (Book.genre == Genre::Romance)
return "Romance";
if (Book.genre == Genre::Mystery)
return "Mystery";
}
// Mutators
void Book::changeAuthor(string newAuthor)
{
author = newAuthor;
}
// Helper Functions
bool operator==(const Book& a, const Book& b)
{
if (a.getISBN() != b.getISBN())
return false;
if (a.getCopyrightYear() != b.getCopyrightYear())
return false;
if (a.getTitle() != b.getTitle())
return false;
if (a.getAuthor() != b.getAuthor())
return false;
if (a.getGenre() != b.getGenre())
return false;
return true;
}
bool operator!=(const Book& a, const Book& b)
{
return !(a==b);
}
} // End Biblo
It seems you tried everything but the thing you needed! The enum is nested inside the Book class which is within the Biblo namespace. The code you are looking for is:
int main()
{
Biblo::Book book(100, 2012, "The Walrus", "The Eggman", Biblo::Book::Horror);
return 0;
}
You need to include the enum class. eg.:
Biblio::Book::Genre::Horror
Bunch of things that are going wrong really.
As others mentioned your enum is stashed one level deeper than you think it is, and your complaint about fixing it then producing an undefined reference is probably because at that point you bump into the fact there's nothing much initialized, and if you got past that you are returning items somewhat poorly when it comes to the enumerator.
If you use the right name space, quickly put in an actual implementation for the constructor, and get the most immediate return for your enum (an int might work) it should work, and probably look like this:
#include <iostream>
#include <string>
using namespace std;
namespace Biblo{
class Book{
public:
enum Genre{
No_Genre, Horror, Comedy, Romance, Mystery
};
// The rest of this header is working fine I think, just this enum
class Invalid{}; // Used for throwing errors
Book(int n_ISBN, int n_copyrightYear, string n_title, string n_author, Genre n_genre);
Book();
// Accessors (non-modifying)
int getISBN() const { return ISBN; }
int getCopyrightYear() const { return copyrightYear; }
string getTitle() const { return title; }
string getAuthor() const { return author; }
int getGenre() const { return genre; }
// Mutators
void changeAuthor(string newAuthor);
private:
int ISBN;
int copyrightYear;
string title;
string author;
Genre genre;
}; // End Book
Book::Book(int n_ISBN, int n_copyrightYear, string n_title, string n_author, Genre n_genre){
ISBN = n_ISBN; copyrightYear = n_copyrightYear; title = n_title; author = n_author;
};
}
using namespace std;
int main()
{
Biblo::Book book(100, 2012, "The Walrus", "The Eggman", Biblo::Book::Horror); // THIS LINE GIVES ERROR
cout << "ISBN: " << book.getISBN() << endl;
cout << "Copyright: " << book.getCopyrightYear() << endl;
cout << "Title: " << book.getTitle() << endl;
cout << "Author: " << book.getAuthor() << endl;
cout << "Genre: " << book.getGenre() << endl;
return 0;
}
Are you sue the book you're following isn't discussing details such as initializer lists or something else for constructors concurrent to, or previous to, the subjects you're looking at?
The code looked somewhat incomplete.
Edited in line here on SO, so bear with the poor formatting and the merged h/cpp look :)
i have a base class product and a child class multiBuyProduct
class Product
{
public:
Product(std::string name, Amount price);
}
class MultiBuyProduct :public Product
{
public:
MultiBuyProduct(std::string aName, Amount price, int minDiscountedQuantity, int discountedPercent);
now obviously some of the constructor variables are the same but in my main class i assume if i want the functionality of multiBuyProduct i need to call it? or can i call product and pass values for the multiBuyProduct constructor into a parameter that is expecting a product?
Below shows a method where this is used
void ShoppingCart::add(Product p, int quantity, bool end)
{
}
Above is the method i want to sent the parameters to but i dont know if i need to change the code for this method to accept a MultiBuyProduct or ...??
EDIT: sorry forgot to mention 'Amount' is a class that takes two integers
Amount amount(0,1);
You should change ShoppingCart::add something like below, to achieve polymorphic behavior:
void ShoppingCart::add(Product *p, int quantity, bool end)
{
p->doSomething();
// ...
Product *product = p;
product->doSomething();
// ...
}
Usage:
ShoppingCart shoppingCart;
MultiBuyProduct multiBuyProduct("Paper", Amount(0,1), 1, 1);
shoppingCart.add(&multiBuyProduct, 1, true);
You will probably want to add a virtual method in Product, say, Amount Product::calculatePrice(int quantity), and override it in MultiBuyProduct so that it performs the correct calculation based on minDiscountedQuantity. Then, call this method from add(). Furthermore, you need to pass a reference (Product&) or a pointer (Product*) to add() in order for virtual method invocation to work.
You should change a few things.
You need a type of pointer or reference for polymorphic behaviour.
You need to call the base class constructor.
You need to override your virtual methods.
You should watch out for proper memory management once you rely on pointers.
You should try to be const correct.
Consider this example below:
#include <string>
#include <iostream>
#include <vector>
#include <memory>
#include <sstream>
class Product
{
private:
double Price;
std::string Name;
public:
Product(const std::string& name, double price)
: Price(price),
Name(name)
{
std::cout << "Created " << name << " for $" << price << std::endl;
}
virtual std::string GetName() const
{
return Name;
}
virtual double GetPrice() const
{
return Price;
}
};
class MultiProduct :public Product
{
private:
int Quantity;
public:
MultiProduct(const std::string& name, double price, int quantity)
: Product(name, price),
Quantity(quantity)
{
std::cout << "Created " << quantity << "x " << name << " for $" << price << " each." << std::endl;
}
virtual double GetPrice() const
{
return Product::GetPrice() * Quantity;
}
virtual std::string GetName() const
{
std::stringstream s;
s << Product::GetName();
s << " x";
s << Quantity;
return s.str();
}
};
class ShoppingCart
{
private:
std::vector< std::shared_ptr<Product> > Cart;
public:
void Add( std::shared_ptr<Product> product )
{
Cart.push_back( product );
}
void PrintInvoice() const
{
std::cout << "Printing Invoice:" << std::endl;
for( auto it = Cart.begin() ; it != Cart.end() ; ++it )
{
std::cout << (*it)->GetName() << ": " << (*it)->GetPrice() << std::endl;;
}
}
};
int main()
{
ShoppingCart cart;
cart.Add( std::shared_ptr<Product>( new Product( "cheese", 1.23 ) ) );
cart.Add( std::shared_ptr<Product>( new MultiProduct( "bread", 2.33, 3 ) ) );
cart.Add( std::shared_ptr<Product>( new Product( "butter", 3.21 ) ) );
cart.Add( std::shared_ptr<Product>( new MultiProduct( "milk", 0.55, 5 ) ) );
cart.Add( std::shared_ptr<Product>( new Product( "honey", 5.55 ) ) );
cart.PrintInvoice();
std::cout << "Press any key to continue" << std::endl;
std::cin.get();
return 0;
}