I have a class Result which takes two other classes UNIT and Date as object parameters, so that I can store the values in them for use in those classes. I will put down what Ive got so far, and a few of the errors I get are as follows.
error: prototype for 'int Result::GetUnit() const' does not match any in class 'Result'|
lab2\Result.h|17|error: candidate is: Result Result::GetUnit() const|
lab2\Result.cpp|66|error: prototype for 'int Result::GetDate()' does not match any in class 'Result'|
lab2\Result.h|18|error: candidate is: Result Result::GetDate() const|
lab2\Result.cpp|73|error: 'SetResult' was not declared in this scope|
error: 'SetResult' was not declared in this scope|
For the error not declared in this scope, after researching I understand that I need to define the functions before the first call is made. I have done that in the .cpp file but I still get the error. What am I doing wrong?
Result.h:
#ifndef RESULT_H
#define RESULT_H
#include <iostream>
#include <string>
#include "UNIT.h"
#include "Date.h"
//const unsigned ResultSize = 10;
using namespace std;
class Result
{
public:
Result(){};
Result(UNIT unitobj1, unsigned marks1, Date dateobj1);
Result GetUnit() const;
Result GetDate() const;
void SetDate(Date dateobj1);
void SetUnit(UNIT unitonj1);
void SetMarks( unsigned marks1 );
unsigned GetMarks() const;
void SetCredits( unsigned cred );
unsigned GetCredits() const;
string GetID() const;
void SetID(string idd);
void SetResult(istream & input);
void GetResult(ostream & os);//unsigned GetUnit() const;
private:
UNIT unitobj;
Date dateobj;
string id;
int credits;
unsigned marks;
};
inline unsigned Result::GetCredits() const
{
return credits;
}
ostream & operator <<( ostream & os, const Result & S);
istream & operator >>( istream & input, Result & S);
#endif // RESULT_H
Result.cpp:
#include "Result.h"
#include "UNIT.h"
#include "Date.h"
Result::Result(UNIT unitobj1, unsigned marks1, Date dateobj1)
{
unitobj = unitobj1;
marks = marks1;
dateobj = dateobj1;
}
void Result::SetResult(istream &input){
UNIT unitobj1;
unsigned marks1;
Date date1;
input >> unitobj1 >> marks1 >> date1;
SetUnit(unitobj1);
SetMarks(marks1);
SetDate(date1);
}
void Result::GetResult(ostream &os){
os << GetUnit() < " Marks: " << GetMarks() << '\n' << GetDate() << '\n';
}
void Result::SetUnit(UNIT unitobj1){
unitobj = unitobj1;
}
void Result::SetMarks(unsigned marks1){
marks = marks1;
}
void Result::SetDate(Date dateobj1){
dateobj = dateobj1;
}
Result::GetUnit() const{
return unitobj;
}
inline unsigned Result::GetMarks() const{
return marks;
}
Result::GetDate(){
return dateobj;
}
istream & operator >>( istream & input, Result & S)
{
SetResult(input);
return input;
}
ostream & operator <<( ostream & os, const Result & S)
{
GetResult(os);
return os;
}
Date.h:
#if !defined(_DATE_H)
#define _DATE_H
#include <iostream>
#include <string>
using namespace std;
class Date {
public:
Date();
Date(unsigned day1, string month1, unsigned year1);
void SetDay(unsigned day1);
void SetMonth(string month1);
void SetYear(unsigned year1);
unsigned GetDay() const;
string GetMonth() const;
unsigned GetYear() const;
void SetDate(istream &input);
void GetDate(ostream & os);
private:
unsigned day;
string month;
unsigned year;
};
ostream & operator <<(ostream & os, const Date & D);
istream & operator >>(istream & input, Date & D);
#endif //_DATE_H
Date.cpp:
//
//
// Generated by StarUML(tm) C++ Add-In
#include "Date.h"
Date::Date(unsigned day1, string month1, unsigned year1) {
day = day1;
month = month1;
year = year1;
}
void Date::SetDay(unsigned day1) {
day = day1;
}
void Date::SetMonth(string month1) {
month = month1;
}
void Date::SetYear(unsigned year1) {
year = year1;
}
inline unsigned Date::GetDay() const {
return day;
}
string Date::GetMonth() const {
return month;
}
inline unsigned Date::GetYear() const {
return year;
}
void Date::SetDate(istream &input){
unsigned day1;
string month1;
unsigned year1;
input >> day1 >> month1 >> year1;
SetDay(day1);
SetMonth(month1);
SetYear(year1);
}
void Date::GetDate(ostream &os){
os << " Date: " << GetDay() << " " << GetMonth() << " " << GetYear();
}
istream & operator >>( istream & input, Date & D) {
SetDate(input);
return input;
}
ostream & operator <<( ostream & os, const Date & D) {
GetDate(os);
return os;
}
UNIT.h:
#ifndef UNIT_H
#define UNIT_H
#include <iostream>
#include <string> // C string library
using namespace std;
const unsigned UnitNameSize = 10;
class UNIT
{
public:
UNIT();
UNIT( string nam, string idd, unsigned cred);
void SetName(string nam);
string GetName() const;
void SetMarks(unsigned marks1);
unsigned GetMarks();
void SetCredits(unsigned cred);
unsigned GetCredits() const;
string GetID() const;
void SetID(string idd);
void SetUnit(istream & input);
void GetUnit(ostream & os);
private:
string name;
string id;
unsigned marks;
int credits;
};
ostream & operator <<( ostream & os, const UNIT & U);
istream & operator >>( istream & input, UNIT & U);
#endif // UNIT_H
UNIT.cpp:
#include "UNIT.h"
#include <string>
UNIT::UNIT()
{
name[0] = '\0';
}
void UNIT::SetName(string nam)
{
name = nam;
}
string UNIT::GetName() const
{
return name;
}
void UNIT::SetID(string idd)
{
id = idd;
}
string UNIT::GetID() const
{
return id;
}
void UNIT::SetCredits(unsigned cred){
credits = cred;
}
inline unsigned UNIT::GetCredits() const{
return credits;
}
UNIT::UNIT( string nam, string idd,
unsigned cred)
{
name.replace(0, 10, nam );
id = idd;
credits = cred;
}
void UNIT::SetUnit(istream &input){
string nam;
string idd;
unsigned cred;
getline(input,nam, '\n');
getline(input,idd,'\n');
input >> cred;
SetName(nam);
SetID(idd);
SetCredits(cred);
}
void UNIT::GetUnit(ostream & os){
os << " Unit ID: " << GetID() << '\n'
<< " Unit Name: " << GetName() << '\n'
<< " Credits: " << GetCredits() << '\n';
}
istream & operator >>( istream & input, UNIT & U)
{
SetUnit(input);
return input;
}
ostream & operator <<( ostream & os, const UNIT & U)
{
GetUnit(os);
return os;
}
Note: I know that I can just make the overloaded operators methods of the class and declare them as friends, but I am trying to achieve this by using set and get methods. Any help would be very appreciated!
So, this is pretty straightforward, look at Result::GetUnit
Result::GetUnit() const {
return unitobj;
}
This method is missing a return type. Now look at unitobj
UNIT unitobj;
So it's clear that the return type should be UNIT, so the above method should be defined as
UINT Result::GetUnit() const {
return unitobj;
}
Now look at how you declared this method in your class
class Result
{
...
Result GetUnit() const;
Here you gave it a return type of Result where we've already seen that the return type should be UINT, so change the above to
class Result
{
...
UNIT GetUnit() const;
Result::GetDate has similar problems, here the return type should be Date but again you specified it as Result.
For the SetResult error you simply need to specify that you are calling the SetResult method on a Result object. The compiler thinks you are calling a global SetResult function (which doesn't exist)
Like this
istream & operator >>( istream & input, Result & S)
{
S.SetResult(input);
return input;
}
S.SetResult(input); tells the compiler that you want to call the SetResult method using the Result object refered to by S.
You can tell this by reading the error message carefully, it said 'SetResult' was not declared in this scope, not 'Result::SetResult' was not declared in this scope. As you said you have declared Result::SetResult, but your code wasn't calling that, it was trying to call a different function called SetResult and the compiler correctly reported that no such function was declared.
Related
I have looked over many questions available on SO, but I could not find something that actually answered my question.
I was wondering how to implement the istream& operator >> method to create an array of Books.
I just need help with the istream method, not the whole program.
#include "Warehouse.h"
#include "Book.h"
#include <iostream>
#include<string>
using namespace std;
static const int MAX_BOOKS = 35;
clss Warehouse {
/**
* #param is the input stream
* #param warehouse the warehouse object reference
* #return the input stream
*/
friend istream& operator >> (istream& is, Warehouse& warehouse);
/**
* #param os the output stream
* #param warehouse the warehouse object reference
* #return the output stream
*/
friend ostream& operator << (ostream& os, const Warehouse& warehouse);
public:
static const int MAX_BOOKS = 35;
Warehouse();
/**
* #param isbn the ISBN number to search for
* #param book reference to the matched book object, if found
* #return true if found.
*/
bool find (string isbn, Book& book) const;
/**
* Prints the inventory of the Warehouse (i.e. list all the books)
*/
void list () const;
private: /* extra credit */
void sort_();
private:
Book books[Warehouse::MAX_BOOKS];
int bookCount;
};
#endif /* WAREHOUSE_H */
This is the cpp file for the Book:
#include "Book.h"
#include <iostream>
#include <string>
using namespace std;
static const int MAX_BOOKS = 35;
static const int MAX_AUTHORS = 20;
string title_;
string authors_[Book::MAX_AUTHORS];
int authorCount_;
string publisher_;
short yearPublish_;
bool hardcover_;
float price_;
string isbn_;
long copies_;
istream& operator >> (istream& is, Book& book){
is >> book.title_;
is.ignore();
is >> book.authorCount_;
for (int j=0;j<book.authorCount_;j++){
is >> book.authors_[j];
is.ignore();
}
is >> book.publisher_;
is.ignore();
is >> book.yearPublish_;
is.ignore();
is >> book.hardcover_;
is.ignore();
is >> book.price_;
is.ignore();
is >> isbn_;
is.ignore();
is >> copies_;
is.ignore();
return is;
}
ostream& operator << (ostream& os, const Book& book){
os<< book.title_ << endl;
for (int j=0; j<book.authorCount_; j++){
os<< book.authors_[j] << endl;
}
os<< book.publisher_ << endl;
os<< book.yearPublish_;
os<<book.hardcover_;
os<< book.price_ << endl;
os<< book.isbn_ << endl;
os<< book.copies_ << endl;
return os;
}
Book :: Book() {};
Book :: Book (string title, string authors[], int authorCount, string publisher, short yearPublish, bool hardcover, float price, string isbn, long copies){
title_ = title;
authorCount_ = authorCount;
for (int i=0;i<authorCount_;i++){
authors_[i] = authors[i];
}
publisher_ = publisher;
yearPublish_ = yearPublish;
hardcover_ = hardcover;
price_ = price;
isbn_ = isbn;
copies_= copies;
}
void Book::setTitle(string title){
title_=title;
}
string Book::getTitle()const{
return title_;
}
void Book:: setAuthorCount(short authorCount){
authorCount_ = authorCount;
}
void Book::setAuthors(string authors[]){
authors_[MAX_AUTHORS]=authors[MAX_AUTHORS];
}
string Book::getAuthors() const{
return authors_[MAX_AUTHORS-1];
}
void Book::setPublisher(string publisher){
publisher_ = publisher;
}
string Book::getPublisher() const{
return publisher_;
}
void Book::setYearPublish(short yearPublish){
yearPublish_ = yearPublish;
}
short Book:: getYearPublish() const{
return yearPublish_;
}
void Book::setHardcover(bool hardcover){
hardcover_ = hardcover;
}
bool Book::getHardcover() const{
return hardcover_;
}
void Book:: setIsbn(string isbn){
isbn_ = isbn;
}
string Book::getIsbn() const{
return isbn_;
}
void Book:: setPrice(float price){
price_ = price;
}
float Book:: getPrice() const{
return price_;
}
void Book:: setCopies(long copies){
copies_ = copies;
}
long Book::getCopies()const{
return copies_;
}
I am working on my school project that has a base class and a derived class with some other classes.
Product.h
#ifndef AMA_PRODUCT_H
#define AMA_PRODUCT_H
#include <iostream>
#include <fstream>
namespace AMA
{
const int max_sku_length = 7;
const int max_unit_length = 10;
const int max_name_length = 75;
const double TRate = 0.13;
class Product
{
private:
char m_type;
char m_sku[max_sku_length +1];
char m_unit[max_unit_length + 1];
char* m_name;
int m_Cquantity;
int m_Nquantity;
double m_price;
bool m_status;
protected:
void sku(const char* setSku) { strncpy(m_sku, setSku, max_sku_length); }
const char* name() const { return m_name; }
bool taxed() const { return m_status; }
const char* sku() const { return m_sku;}
void name(const char*);
const char* unit() const;
double price() const;
void message(const char*);
bool isClear() const;
public:
double cost() const;
bool operator==(const char* src) { return strcmp(m_sku, src) == 0; }
bool isEmpty() const { return ((m_sku[0] == '\0') && (m_name == nullptr) && (m_price == 0) && (m_Cquantity == 0)); }
int qtyNeeded() const { return m_Nquantity; }
int quantity() const { return m_Cquantity; }
int operator+=(int src) { return m_Cquantity += src; }
Product();
Product(const char* sku, const char* name, const char* unit, int qty = 0,
bool taxed = true, double price = 0.0, int qtyNeeded = 0);
Product(const Product&);
Product& operator=(const Product&);
~Product();
virtual std::fstream& store(std::fstream& file, bool addNewLine = true)const = 0;
virtual std::fstream& load(std::fstream& file) = 0;
virtual std::ostream& write(std::ostream& os, bool linear)const = 0;
virtual std::istream& read(std::istream& is) = 0;
double total_cost() const;
void quantity(int);
bool operator>(const char*) const;
bool operator>(const Product&) const;
};
std::ostream& operator<<(std::ostream&, const Product&);
std::istream& operator>>(std::istream&, Product&);
double operator+=(double&, const Product&);
}
#endif
MyProduct.h
#ifndef AMA_MY_PRODUCT_H
#define AMA_MY_PRODUCT_H
#include <fstream>
#include "Product.h"
#include "ErrorState.h"
namespace AMA {
class MyProduct : public Product {
public:
MyProduct();
MyProduct(const char* sku, const char* name, const char* unit, int qty = 0,
bool isTaxed = true, double price = 0.0, int qtyNeeded = 0);
const char* sku() const;
const char* name() const;
const char* unit() const;
bool taxed() const;
double price() const;
double cost() const;
};
class Test {
MyProduct product; // Error
const char* filename;
public:
Test(const char* file);
Test(const char* file, const char* theSku, const char* theName);
std::fstream& store(std::fstream& file, bool addNewLine = true) const;
std::fstream& load(std::fstream& file);
std::ostream& write(std::ostream& os, bool linear) const;
std::istream& read(std::istream& is);
int operator+=(int value);
bool operator==(const char* sku) const;
friend std::ostream& operator<<(std::ostream& os, const Test& test);
friend double operator+=(double& d, const Test& test);
friend std::istream& operator>>(std::istream& is, Test& test);
};
}
#endif
MyProduct.cpp
#include <iomanip>
#include <fstream>
#include <cstring>
#include "MyProduct.h"
#ifdef TAB
#undef TAB
#endif
#define TAB '\t'
using namespace std;
namespace AMA {
MyProduct::MyProduct() : Product("", "", "") {}
MyProduct::MyProduct(const char* sku, const char* name, const char* unit, int qty,
bool isTaxed, double price, int qtyNeeded) :
Product(sku, name, unit, qty, isTaxed, price, qtyNeeded) {}
const char* MyProduct::sku() const { return Product::sku(); }
const char* MyProduct::name() const { return Product::name(); }
const char* MyProduct::unit() const { return Product::unit(); }
bool MyProduct::taxed() const { return Product::taxed(); }
double MyProduct::price() const { return Product::price(); }
double MyProduct::cost() const { return Product::cost(); }
Test::Test(const char* file) : filename(file) { }
Test::Test(const char* file, const char* theSku, const char* theName) :
product(theSku, theName, ""), filename(file) { }
std::fstream& Test::store(std::fstream& file, bool addNewLine) const {
if (!product.isEmpty()) {
file.open(filename, ios::out | ios::app);
file << product.sku() << TAB << product.name() << TAB << product.unit() << TAB <<
(product.taxed() ? 1 : 0) << TAB << product.price() << TAB << product.quantity() << TAB <<
product.qtyNeeded() << endl;
file.clear();
file.close();
}
return file;
}
std::fstream& Test::load(std::fstream& file) {
char sku_[max_sku_length + 1];
char name[max_name_length + 1];
char unit[max_unit_length + 1];
int quantity, qtyNeeded;
double price_;
char taxed_;
file.open(filename, ios::in);
file >> sku_;
file >> name;
file >> unit;
file >> taxed_;
file >> price_;
file >> quantity;
file >> qtyNeeded;
file.clear();
file.close();
product = MyProduct(sku_, name, unit, quantity, taxed_ != 0, price_, qtyNeeded); //ERROR
return file;
}
std::ostream& Test::write(std::ostream& os, bool linear) const {
return product.isEmpty() ? os : (os << product.sku() << ": " << product.name() << ", quantity: "
<< product.quantity() << ", quantity needed:" << product.qtyNeeded()
<< ", Cost: " << fixed << setprecision(2) << product.cost());
}
std::istream& Test::read(std::istream& is) {
char sku_[max_sku_length + 1];
char name[max_name_length + 1];
char unit[max_unit_length + 1];
int quantity, qtyNeeded;
double price_;
char taxed_;
cout << " Sku: ";
is >> sku_;
cout << " Name (no spaces): ";
is >> name;
cout << " Unit: ";
is >> unit;
cout << " Taxed? (y/n): ";
is >> taxed_;
cout << " Price: ";
is >> price_;
cout << " Quantity On hand: ";
is >> quantity;
cout << " Quantity Needed: ";
is >> qtyNeeded;
product = MyProduct(sku_, name, unit, quantity, taxed_ != 0, price_, qtyNeeded); //ERROR
return is;
}
int Test::operator+=(int value) {
product.quantity(product += value);
return product.quantity();
}
bool Test::operator==(const char* sku) const {
return !std::strcmp(product.sku(), sku);
}
std::ostream& operator<<(std::ostream& os, const Test& test) {
return test.product.write(os, true);
}
double operator+=(double& d, const Test& test) {
return d += test.product.total_cost();
}
std::istream& operator>>(std::istream& is, Test& test) {
return test.product.read(is);
}
}
I am getting errors that I don't really know how to fix
object of abstract class type "AMA::MyProduct" is not allowed
'AMA::MyProduct': cannot instantiate abstract class
a cast to abstract class "AMA::MyProduct" is not allowed
Can someone help me out please?
The errors are self explanatory.
AMA::MyProduct is an abstract class, and such it cannot be instantiated directly.
A class is abstract when it has at least 1 abstract method (virtual and = 0 keywords on it). An abstract method MUST be overridden in a derived class, and you MUST instantiate that derived class, not the abstract class.
AMA::MyProduct is abstract because it does not override the 4 abstract methods it inherits from AMA::Product:
virtual std::fstream& store(std::fstream& file, bool addNewLine = true)const = 0;
virtual std::fstream& load(std::fstream& file) = 0;
virtual std::ostream& write(std::ostream& os, bool linear)const = 0;
virtual std::istream& read(std::istream& is) = 0;
You implemented them in AMA::Test instead of in AMA::MyProduct.
I've been pulling my hair out over a certain error that seems to be plaguing my program. I've attempted to search online for cases similar to mine but I can't seem to find a way to apply the other solutions to this problem. My issue is as follows: When I initially open the program it immediately stops responding and crashes. Debugging led me to find the error in question is "0xC0000005: Access violation writing location 0xCCCCCCCC". It seems to be tied to me assigning two attributes (sku_ and name_ ) as '\0'. If I change these values to anything else such as "" or even "\0" the program runs in visual studio, but will fail to compile elsewhere. Could someone help me understand where I am going wrong?
Product.h
#ifndef SICT_Product_H__
#define SICT_Product_H__
#include "general.h"
#include "Streamable.h"
#include <cstring>
namespace sict {
class Product : public Streamable {
char sku_ [MAX_SKU_LEN + 1];
char* name_;
double price_;
bool taxed_;
int quantity_;
int qtyNeeded_;
public:
//Constructors
Product();
Product(const char* sku, const char* name1, bool taxed = true, double price = 0, int qtyNeeded =0);
Product(Product& g);
~Product();
//Putter Functions
void sku(const char* sku) { strcpy(sku_,sku); };
void price(double price) {price_ = price;};
void name(const char* name);
void taxed(bool taxed) { taxed_ = taxed; };
void quantity(int quantity) { quantity_ = quantity; };
void qtyNeeded(int qtyNeeded) { qtyNeeded_ = qtyNeeded; };
//Getter functions
const char* sku() const { return sku_; };
double price() const { return price_; };
const char* name() const { return name_; };
bool taxed() const { return taxed_; };
int quantity() const { return quantity_; };
int qtyNeeded() const { return qtyNeeded_; };
double cost() const;
bool isEmpty() const;
Product& operator=(const Product& );
bool operator==(const char* );
int operator+=(int );
int operator-=(int );
};
double operator+=(double& , const Product& );
std::ostream& operator<<(std::ostream& os, const Product& );
std::istream& operator>>(std::istream& is, Product& );
}
#endif
Product.cpp
#include <iostream>
#include <cstring>
#include "Product.h"
namespace sict {
Product::Product() {
sku_[0] = '\0';
name_[0] = '\0';
price_ = 0;
quantity_ = 0;
qtyNeeded_ = 0;
}
Product::Product(const char* sku, const char* name1, bool taxed1, double price1, int qtyNeeded1) {
strncpy(sku_, sku, MAX_SKU_LEN);
name(name1);
quantity_ = 0;
taxed(taxed1);
price(price1);
qtyNeeded(qtyNeeded1);
}
double Product::cost() const {
if (taxed_ == true) {
return (price_ * TAX) + price_;
}
else
return price_;
}
bool Product::isEmpty() const{
if (sku_ == nullptr && name_ == nullptr && quantity_ == 0 && price_ == 0 && qtyNeeded_ == 0) {
return true;
}
else
return false;
}
Product::Product(Product& ex) {
sku(ex.sku_);
price(ex.price_);
name(ex.name_);
taxed(ex.taxed_);
quantity(ex.quantity_);
qtyNeeded(ex.qtyNeeded_);
}
Product& Product::operator=(const Product& g) {
sku(g.sku_);
price(g.price_);
name(g.name_);
taxed(g.taxed_);
quantity(g.quantity_);
qtyNeeded(g.qtyNeeded_);
return *this;
}
Product::~Product() {
delete [] name_;
}
void Product::name(const char* name) {
name_ = new char [strlen(name) + 1];
strcpy(name_, name);
}
bool Product::operator==(const char* right) {
if (sku_ == right) {
return true;
}
else
return false;
}
int Product::operator+=(int g) {
quantity_ = quantity_ + g;
return quantity_;
}
int Product::operator-=(int g) {
quantity_ = quantity_ - g;
return quantity_;
}
double operator+=(double& p, const Product& right) {
p = p + (right.cost() * right.quantity());
return p;
}
std::ostream& operator<<(std::ostream& os, const Product& g) {
return g.write(os, true);
}
std::istream& operator>>(std::istream& is, Product& g) {
return g.read(is);
}
}
The General.h file referenced in the header is just a list of constant values such as the "TAX" and "MAX_SKU_LEN" values.
The Streamable header contains pure virtual functions. I will list it here in case it is needed.
Streamable.h
#ifndef SICT__Streamable_H_
#define SICT__Streamable_H_
#include <iostream>
#include <fstream>
#include "Product.h"
namespace sict {
class Streamable {
public:
virtual std::fstream& store(std::fstream& file, bool addNewLine = true)const = 0;
virtual std::fstream& load(std::fstream& file) = 0;
virtual std::ostream& write(std::ostream& os, bool linear)const = 0;
virtual std::istream& read(std::istream& is) = 0;
};
}
#endif
Thank you very much in advance.
Product::Product() {
sku_[0] = '\0';
name_[0] = '\0'; // <---- writing via unitialized pointer
price_ = 0;
quantity_ = 0;
qtyNeeded_ = 0;
}
There's one possible source of your problems. You have defined a char pointer (a dynamic array or a C-string, that is) name_ in your class but you never allocate any memory for it in your constructor, before attempting to record a value via the pointer. Naturally, you get a write access violation.
Before assigning the value to char at index [0] you need to first allocate space for at least one element in your string, e.g. by doing name_ = new char [1]. Alternatively, you may choose to initialize the pointer itself to NULL (or nullptr) and use that to indicate that name_ has not yet been set.
My package compiles, the get functions work except for the doubles it prints a random number. Any ideas?
#ifndef Package_H
#define Package_H
#include <string>
using namespace std;
//The class Package is the base class for derived classes TwoDayPackage and OverNightPackage
class Package //begins class Package
{
public:
Package(const string &, const string &, const string &, const string &, const string &, const string &, const string &, const string &, const string &, const string &, double = 0.0, double = 0.0); //constructor
//set and get functions for sender
void setSenderName(const string &);
string getSenderName() const;
void setSenderAddress(const string &);
string getSenderAddress() const;
void setSenderCity(const string &);
string getSenderCity() const;
void setSenderState(const string &);
string getSenderState() const;
void setSenderZip(const string &);
string getSenderZip() const;
//set and get functions for recipient
void setRecipientName(const string &);
string getRecipientName() const;
void setRecipientAddress(const string &);
string getRecipientAddress() const;
void setRecipientCity(const string &);
string getRecipientCity() const;
void setRecipientState(const string &);
string getRecipientState() const;
void setRecipientZip(const string &);
string getRecipientZip() const;
void setWeight(double);
double getWeight() const;
void setShip(double);
double getShip() const;
double calculateCost() const;
void print() const;
private:
string senderName;
string senderAddress;
string senderCity;
string senderState;
string senderZip;
string recipientName;
string recipientAddress;
string recipientCity;
string recipientState;
string recipientZip;
double weight;
double shipCost;
};
#endif
.
//next page
#include <iostream>
using namespace std;
#include "Package.h"
Package::Package(const string & sname, const string & saddress, const string & scity, const string & sstate, const string & szip, const string & rname, const string & raddress, const string & rcity, const string & rstate, const string & rzip, double weight, double shipCost)
{
senderName = sname;
senderAddress = saddress;
senderCity = scity;
senderState = sstate;
senderZip = szip;
recipientName = rname;
recipientAddress = raddress;
recipientCity = rcity;
recipientState = rstate;
recipientZip = rzip;
setWeight(weight);
setShip(shipCost);
}
void Package::setSenderName(const string & sname)
{
senderName = sname;
}
string Package::getSenderName() const
{
return senderName;
}
void Package::setSenderAddress(const string & saddress)
{
senderAddress = saddress;
}
string Package::getSenderAddress() const
{
return senderAddress;
}
void Package::setSenderCity(const string & scity)
{
senderCity = scity;
}
string Package::getSenderCity() const
{
return senderCity;
}
void Package::setSenderState(const string & sstate)
{
senderState = sstate;
}
string Package::getSenderState() const
{
return senderState;
}
void Package::setSenderZip(const string & szip)
{
senderZip = szip;
}
string Package::getSenderZip() const
{
return senderZip;
}
void Package::setRecipientName(const string & rname)
{
recipientName = rname;
}
string Package::getRecipientName() const
{
return recipientName;
}
void Package::setRecipientAddress(const string & raddress)
{
recipientAddress = raddress;
}
string Package::getRecipientAddress() const
{
return recipientAddress;
}
void Package::setRecipientCity(const string & rcity)
{
recipientCity = rcity;
}
string Package::getRecipientCity() const
{
return recipientCity;
}
void Package::setRecipientState(const string & rstate)
{
recipientState = rstate;
}
string Package::getRecipientState() const
{
return recipientState;
}
void Package::setRecipientZip(const string & rzip)
{
recipientZip = rzip;
}
string Package::getRecipientZip() const
{
return recipientZip;
}
void Package::setWeight(double weight)
{
weight = (weight < 0.0 ) ? 0.0 : weight;
}
double Package::getWeight() const
{
return weight;
}
void Package::setShip(double shipCost)
{
shipCost = ( shipCost < 0.0) ? 0.0 : shipCost;
}
double Package::getShip() const
{
return shipCost;
}
double Package::calculateCost() const
{
return weight * shipCost;
}
void Package::print() const
{
cout<<"Sender:"<<endl
<<senderName<<endl
<<senderAddress<<endl
<<senderCity<<", "<<senderState<<" "<<senderZip<<endl
<<endl
<<"Recepient:"<<endl
<<recipientName<<endl
<<recipientAddress<<endl
<<recipientCity<<", "<<recipientState<<" "<<recipientZip<<endl
<<endl
<<"Weight of package: "<<weight<<" oz."<<endl
<<"Type of delivery: Regular Delivery"<<endl
<<"Cost of package: $"<<calculateCost()<<endl;
}
//The class TwoDayPackage is the first derived class from class Package
//The class OverNightPackage is the second derived class from class Package
.
//main function
#include <iostream>
#include <iomanip>
#include <vector>
using namespace std;
using std::setprecision;
#include "Package.h"
/*#include"overnighttest.h"
#include"twoday2.h"*/
//Test File
int main()
{
Package message("chris beyer","1 westwood circle","edison","nj","08820","mike b","1 westwood cirle","edison","nj","08820",10.00,1.50);
/*OverNightPackage box("John Doe", "789 Fire Street", "Hell", "MI", "48169", "Jane Doe", "987 Leg Sun Crossing", "Intercourse", "PA", "17534", 10.00, 1.50, .85);
TwoDayPackage parcel("John Doe", "789 Fire Street", "Hell", "MI", "48169", "Jane Doe", "987 Leg Sun Crossing", "Intercourse", "PA", "17534", 15.00, 1.05, 5.00);*/
cout << fixed << setprecision(2);
cout<<"Package delivery services program"<<endl
<<endl
<<"Cost per ounce for a package: $.50/ounce"<<endl
<<"Additional cost for two day delivery: $2.00/ounce"<<endl
<<"Additional cost for overnight delivery: $5.00/ounce"<<endl<<endl;
vector<Package*> myPackages;
Package *messagePtr=&message;
/*TwoDayPackage *TDpPtr=&TDp;
OvernightPackage *OpPtr=&Op;*/
myPackages.push_back(messagePtr);
/*myPackages.push_back(TDpPtr);
myPackages.push_back(OpPtr);*/
double total=0;
for(int i=0;i<myPackages.size();i++)
{
cout<<"Package #"<<i+1<<":"<<endl<<endl;
(*myPackages[i]).print();
cout<<endl;
total+=(*myPackages[i]).calculateCost();
}
cout<<"Total cost for all the packages: $"<<total<<endl;
system("pause");
return 0;
}
In your setters, you're not setting the instance variables. For instance: weight = (weight < 0.0) ? 0.0 : weight just changes the temporary variable used for the argument. You can either change the name of the parameter, change the name of the instance variable (recommended), or use the syntax this->weight = ...
struct myVals {
int val1;
int val2;
};
I have static functions
static myVals GetMyVals(void)
{
// Do some calcaulation.
myVals val;
val.val1 = < calculatoin done in previous value is assigned here>;
val.val2 = < calculatoin done in previous value is assigned here>;
return val;
}
bool static GetStringFromMyVals( const myVals& val, char* pBuffer, int sizeOfBuffer, int count)
{
// Do some calcuation.
char cVal[25];
// use some calucations and logic to convert val to string and store to cVal;
strncpy(pBuffer, cVal, count);
return true;
}
My requirement here is that i should have above two functions to be called in order and print the string of "myvals" using C++ output operator (<<).
How can we achieve this? Does i require new class to wrap this up. Any inputs are help ful. Thanks
pseudocode:
operator << () { // operator << is not declared completely
char abc[30];
myvals var1 = GetMyVald();
GetStringFromMyVals(var1, abc, 30, 30);
// print the string here.
}
The signature for this operator is as follows:
std::ostream & operator<<(std::ostream & stream, const myVals & item);
An implementation could look like this:
std::ostream & operator<<(std::ostream & stream, const myVals & item) {
stream << item.val1 << " - " << item.val2;
return stream;
}