My code is as shown below. The problem is inside the int main() function, r.printItems() is not printing anything. What am I missing here?
main.cpp
#include <bits/stdc++.h>
#include "Customer.h"
#include "MenuCreator.h"
#include "FoodItem.h"
MenuCreator m1;
void createCustomer() {
Customer c1("mrg", "m#gmail.com", "9654357", "+91");
}
void createRestaurantItem(Restaurant &rest) {
rest.addItems(FoodItem("t1"));
rest.addItems(FoodItem("D1"));
}
void createMenu() {
m1.createMenu("sg");
Category c1;
c1.setName("Non-veg");
m1.addCategory(c1);
Restaurant r1;
r1.setName("ABC");
r1.setDescription("Test Restaurant");
createRestaurantItem(r1);
c1.addRestaurants(r1);
}
vector<Restaurant> getRestaurantsForCategory(string category) {
return m1.getRestaurantsForCategories(category);
}
int main() {
createCustomer();
createMenu();
for (auto r: getRestaurantsForCategory("Non-veg")) {
r.printItems();
}
return 0;
}
MenuCreator.h
#include <bits/stdc++.h>
#include "Menu.h"
using namespace std;
class MenuCreator {
public:
void createMenu(string name) {
Menu m1;
m1.setName(name);
menu = m1;
}
void addCategory(const Category &categ) {
categories.push_back(categ);
}
const Menu &getMenu() const {
return menu;
}
const vector<Category> &getCategories() const {
return categories;
}
void addRestaurantForCategory(string name, const Restaurant restaurant) {
for(auto categ: categories) {
if (categ.getName() == name) {
categ.addRestaurants(restaurant);
}
}
}
const vector<Restaurant> &getRestaurantsForCategories(string category) {
for(auto categ: categories) {
if(categ.getName() == category) return categ.getRestaurants();
}
}
private:
Menu menu;
vector<Category> categories;
};
Menu.h
#include<bits/stdc++.h>
#include "Category.h"
using namespace std;
class Menu {
public:
const string &getName() const {
return name;
}
void setName(const string &name) {
Menu::name = name;
}
private:
string name;
string description;
vector<Category> categories;
};
Category.h
using namespace std;
class Category {
public:
const string &getName() const {
return name;
}
void setName(const string &name) {
Category::name = name;
}
const string &getDescription() const {
return description;
}
void setDescription(const string &description) {
Category::description = description;
}
const vector<Restaurant> &getRestaurants() const {
return restaurants;
}
void setRestaurants(const vector<Restaurant> &restaurants) {
Category::restaurants = restaurants;
}
void addRestaurants(const Restaurant &rt) {
Category::restaurants.push_back(rt);
}
private:
string name;
string description;
vector<Restaurant> restaurants;
};
FoodItem.h
#include <bits/stdc++.h>
#include <vector>
#include "FoodItem.h"
using namespace std;
class Restaurant {
public:
Restaurant() {
this->id = gen_random(12);
}
virtual ~Restaurant() {
}
const string &getName() const {
return name;
}
void setName(const string &name) {
Restaurant::name = name;
}
const string &getDescription() const {
return description;
}
void setDescription(const string &description) {
Restaurant::description = description;
}
double getLat() const {
return lat;
}
void setLat(double lat) {
Restaurant::lat = lat;
}
double getLang() const {
return lang;
}
void setLang(double lang) {
Restaurant::lang = lang;
}
const string &getImageUrl() const {
return imageUrl;
}
void setImageUrl(const string &imageUrl) {
Restaurant::imageUrl = imageUrl;
}
const string &getVideoUrl() const {
return videoUrl;
}
void setVideoUrl(const string &videoUrl) {
Restaurant::videoUrl = videoUrl;
}
const vector<FoodItem> &getItems() const {
return items;
}
void setItems(const vector<FoodItem> &items) {
Restaurant::items = items;
}
void addItems(const FoodItem &item) {
this->items.push_back(item);
}
string gen_random(const int len) {
string tmp_s;
static const char alphanum[] =
"0123456789"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";
srand( (unsigned) time(NULL) * getpid());
tmp_s.reserve(len);
for (int i = 0; i < len; ++i)
tmp_s += alphanum[rand() % (sizeof(alphanum) - 1)];
return tmp_s;
}
const string &getId() const {
return id;
}
void printItems() {
for(auto it: items) {
cout<<"item: "<<it.getName()<<endl;
}
}
private:
string id;
string name;
string description;
double lat;
double lang;
string imageUrl;
string videoUrl;
string createdAt;
string updatedAt;
vector<FoodItem> items;
};
Restaurant.h
#include <bits/stdc++.h>
#include <vector>
#include "FoodItem.h"
using namespace std;
class Restaurant {
public:
Restaurant() {
this->id = gen_random(12);
}
virtual ~Restaurant() {
}
const string &getName() const {
return name;
}
void setName(const string &name) {
Restaurant::name = name;
}
const string &getDescription() const {
return description;
}
void setDescription(const string &description) {
Restaurant::description = description;
}
double getLat() const {
return lat;
}
void setLat(double lat) {
Restaurant::lat = lat;
}
double getLang() const {
return lang;
}
void setLang(double lang) {
Restaurant::lang = lang;
}
const string &getImageUrl() const {
return imageUrl;
}
void setImageUrl(const string &imageUrl) {
Restaurant::imageUrl = imageUrl;
}
const string &getVideoUrl() const {
return videoUrl;
}
void setVideoUrl(const string &videoUrl) {
Restaurant::videoUrl = videoUrl;
}
const vector<FoodItem> &getItems() const {
return items;
}
void setItems(const vector<FoodItem> &items) {
Restaurant::items = items;
}
void addItems(const FoodItem &item) {
this->items.push_back(item);
}
string gen_random(const int len) {
string tmp_s;
static const char alphanum[] =
"0123456789"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";
srand( (unsigned) time(NULL) * getpid());
tmp_s.reserve(len);
for (int i = 0; i < len; ++i)
tmp_s += alphanum[rand() % (sizeof(alphanum) - 1)];
return tmp_s;
}
const string &getId() const {
return id;
}
void printItems() {
for(auto it: items) {
cout<<"item: "<<it.getName()<<endl;
}
}
private:
string id;
string name;
string description;
double lat;
double lang;
string imageUrl;
string videoUrl;
string createdAt;
string updatedAt;
vector<FoodItem> items;
};
When createMenu() in main.cpp is setting up the menu, it creates a local Category object named c1 and adds it to the menu, which makes a copy of c1 when pushingnit into the MenuCreator::categories vector. No Restaurant objects had been added to c1 yet when that copy is created. So when a Restaurant is then added to c1 afterwards, the copy is not updated. That is why there are no Restaurant objects in the menu when MenuCreator::getRestaurantsForCategory() tries to return them.
Change createMenu() to fully initialize c1 before adding it to the menu, eg:
void createMenu() {
m1.createMenu("sg");
Category c1;
c1.setName("Non-veg");
Restaurant r1;
r1.setName("ABC");
r1.setDescription("Test Restaurant");
createRestaurantItem(r1);
c1.addRestaurants(r1);
m1.addCategory(c1); // <-- move down here
}
Also, on a side note, the return value of MenuCreator::getRestaurantsForCategories() is undefined if the specified category is not found. Since the return value is a reference to a vector, it needs to either return a static vector that is empty, or else throw an exception.
Related
I am trying to initialize 2 dynamic arrays which class student will have an array containing all the courses registered and class course will have an array containing all the students registered to the class. I defined class Course and Student like this:
#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;
class Course;
class Student {
std::string name; // person’s name
int id; // person’s age
Course* courses;
int course_register; // number of organization registered to
public:
Student();
Student(const Student& s) {
name = s.getName();
id = s.getAge();
course_register = s.getorg_register();
}
Student(std::string n, int i) {
id = i;
name = n;
}
int getAge() const { return id; }
string getName() const { return name; }
int getorg_register() const { return course_register; }
};
class Course {
std::string name; // name of the org
Student* students; // list of members
int size; // actual size of the org
int dim; // max size of the org
public:
Course()
{
dim = 100;
students = new Student[dim];
};
Course(const Course& course)
{
name = course.getName();
dim = course.getDim();
size = course.getSize();
students = new Student[dim];
for (int i = 0; i < size; i++) {
students[i] = course.getStudent(i);
}
}
~Course()
{
delete[] students;
}
Student getStudent(int i) {return students[i];}
Student* getStudents() {return students;}
int getSize(){return size;}
int getDim() {return dim;}
string getName() {return name;}
};
Student::Student() {
courses = new Course[5];
}
When I try to compile, I get an exception unhandled at runtime for constructor Student::Student(). Can someone explain to me why I get a runtime error? And how would you change it to make it work?
A Student should contain a list that refers to the Courses it is enrolled in, it should not create the Courses themselves.
A Course should contain a list that refers to the Students enrolled in it, it should not create the Students themselves.
Does that make sense? In this case, refers means "use a pointer". For instance, Student could have a std::vector<Course*>, and Course could have a std::vector<Student*>. Then you can have a method that enrolls a Student into a Course, where a pointer to the Student is added to the Course's list, and a pointer to the Course is added to the Student's list, eg:
Student.h
#ifndef StudentH
#define StudentH
#include <string>
#include <vector>
class Course;
class Student {
std::string name;
int id;
std::vector<Course*> courses;
void addCourse(Course *c);
void removeCourse(Course *c);
friend class Course;
public:
Student(std::string n, int i);
Student(const Student& s);
Student(Student&& s);
~Student();
Student& operator=(Student rhs);
int getId() const;
string getName() const;
std::vector<Course*> getCourses() const;
void enroll(Course &c);
void drop(Course &c);
};
#endif
Student.cpp
#include "Student.h"
#include "Course.h"
Student::Student(std::string n, int i) {
name = n;
id = i;
}
Student::Student(const Student& s) {
name = s.name;
id = s.id;
for (Course *c : s.courses) {
c->enroll(*this);
}
}
Student::Student(Student&& s) {
name = std::move(s.name);
id = s.id; s.id = 0;
courses = std::move(s.courses);
for (Course *c : courses) {
c->removeStudent(&s);
c->addStudent(this);
}
}
Student::~Student() {
for(Course *c : courses) {
c->removeStudent(this);
}
}
Student& Student::operator=(Student rhs) {
Student temp(std::move(rhs));
for (Course *c : courses) {
c->removeStudent(this);
}
name = std::move(temp.name);
id = temp.id; temp.id = 0;
courses = std::move(temp.courses);
for (Course *c : courses) {
c->removeStudent(&temp);
c->addStudent(this);
}
return *this;
}
void Student::addCourse(Course *c) {
if (std::find(courses.begin(), courses.end(), c) == courses.end()) {
courses.push_back(c);
}
}
void Student::removeCourse(Course *c) {
auto iter = std::find(courses.begin(), courses.end(), c);
if (iter != courses.end())
courses.erase(iter);
}
}
int Student::getId() const {
return id;
}
string Student::getName() const {
return name;
}
std::vector<Course*> Student::getCourses() const {
return courses;
}
void Student::enroll(Course &c) {
c.enroll(*this);
}
void Student::drop(Course &c) {
c.drop(*this);
}
Course.h
#ifndef CourseH
#define CourseH
#include <string>
#include <vector>
class Student;
class Course {
std::string name;
std::vector<Student*> students;
void addStudent(Student *s);
void removeStudent(Student *s);
friend class Student;
public:
Course(std::string n);
Course(const Course& c);
Course(Course&& c);
~Course();
Course& operator=(Course rhs);
string getName() const;
std::vector<Student*> getStudents() const;
void enroll(Student &s);
void drop(Student &s);
};
#endif
Course.cpp
#include "Course.h"
#include "Student.h"
Course::Course(std::string n) {
name = n;
}
Course::Course(const Course& c) {
name = c.name;
for (Student *s : c.students) {
enroll(*s);
}
}
Course::Course(Course&& c) {
name = std::move(c.name);
students = std::move(c.students);
for (Student *s : students) {
s->removeCourse(&c);
s->addCourse(this);
}
}
Course::~Course()
{
for(Student *s : students) {
s->removeCourse(this);
}
}
Course& Course::operator=(Course rhs)
{
Course temp(std::move(rhs));
for (Student *s : students) {
s->removeCourse(this);
}
name = std::move(temp.name);
students = std::move(temp.students);
for (Student *s : students) {
s->removeCourse(&temp);
s->addCourse(this);
}
return *this;
}
void Course::addStudent(Student *s) {
if (std::find(students.begin(), students.end(), s) == students.end()) {
students.push_back(s);
}
}
void Course::removeStudent(Student *s) {
auto iter = std::find(students.begin(), students.end(), s);
if (iter != students.end())
students.erase(iter);
}
}
string Course::getName() const {
return name;
}
std::vector<Student*> Course::getStudents() const {
return students;
}
void Course::enroll(Student &s) {
addStudent(&s);
s.addCourse(this);
}
void Course::drop(Student &s) {
removeStudent(&s);
s.removeCourse(this);
}
Main.cpp
#include "Student.h"
#include "Course.h"
int main()
{
Course c("Math");
Student p("Joe", 12345);
p.enroll(c);
return 0;
}
You have infinite recursion. The Course constructor calls the Student constructor. The Student constructor calls the Course constructor. This continues until you've used up all the stack space.
You'll want to rethink your design of your two classes.
I have a class called Person:
class Person {
private:
char name[50];
unsigned int number;
public:
void set_data(const char *newname, unsigned int number) {
*name= *newname; //THE MAIN PROBLEM I WANT TO SOLVE
this->number = number;
}
char* get_name() {
return this->name;
}
unsigned int get_number() {
return this->number;
}
};
I have arrays:
const char *names[] = {"Mark", "Pavel", "Bill", "Jasur", "Jeff"};
int phones[] = { 1234567890, 9876543210, 123321456654, 1998946848479, 1234554321 };
I'm using for loop to set Person members:
Person p;
for (int i = 0; i < 5; i++) {
p.set_data(names[i], phones[i]);
cout << p.get_name() << endl;
}
When I run this, I'm getting wrong output.
Using * the way you are, you are only copying the 1st char into name. You are also not null-terminating name either, which is required by the overloaded operator<< that takes a char* as input.
To copy the entire string, you need to use std::strcpy() (or better, std::strncpy()) instead, eg:
#include <cstring>
void set_data(const char *newname, unsigned int newnumber) {
//std::strcpy(name, newname);
std::strncpy(name, newname, 49);
name[49] = '\0';
number = newnumber;
}
However, since this is C++ and not C, you really should be using std::string instead:
#include <string>
class Person {
private:
std::string name;
unsigned int number;
public:
void set_data(const std::string &newname, unsigned int newnumber) {
name = newname;
number = newnumber;
}
std::string get_name() const {
return name;
}
/* or:
const std::string& get_name() const {
return name;
}
*/
unsigned int get_number() const {
return number;
}
};
I have a class that contain some game level settings.
class Stage {
private:
int level;
int stars;
std::string imgName;
public:
int getLevel(){ return level; };
void setLevel(int n){ level = n; };
int getStars(){ return stars; };
void setStars(int n){ stars = n; };
std::string getImgName(){ return imgName; };
void setImgName(std::string name){ imgName = name; };
};
Then in my program I set the info.
Stage* stagesArr = new Stage[3];
stagesArr[0].setLevel(0);
stagesArr[0].setStars(1200);
stagesArr[0].setImgName("stage0.png");
Then if I want to get this info the string is giving me an odd output.
CCLOG("Level: %i", stagesArr[0].getLevel());
CCLOG("Required stars: %i", stagesArr[0].getStars());
CCLOG("Image Name: %s", stagesArr[0].getImgName());
//Level:0
//Required stars: 1200
//Image Name: T%s //Or just random stuff.
What am I missing here?
Suspected that CCLOG() uses the same formatting rules like the <x>printf() function family does, you need to pass a const char* with the format specifier %s.
Your getImgName() returns a std::string value though, which isn't directly compatible with a const char*.
To achieve the latter, you should call the std::string::c_str() function:
CCLOG("Image Name: %s", stagesArr[0].getImgName().c_str());
Also you can improve your getter/setter functions specifying constness applicability more clear:
int getLevel() const { return level; }
// ^^^^^^
int getStars() const { return stars; }
// ^^^^^^
const std::string& getImgName() const { return imgName; }
// ^^^^^ // ^^^^^^
void setImgName(const std::string& name) { imgName = name; }
// ^^^^^
Note:
As a matter of style you can omit the get / set prefixes for getter/setter functions in c++, as the signatures are disambiguate enough:
int Level() const { return level; }
void Level(int n){ level = n; }
int Stars() const { return stars; }
void Stars(int n){ stars = n; }
const std::string& ImgName() const { return imgName; }
void ImgName(const std::string& name){ imgName = name; }
My personally preferred style is to use lower caps and disambiguate class member variables with a _ postfix:
class Stage {
private:
int level_;
int stars_;
std::string img_name_;
public:
int level() const { return level_; }
void level(int n) { level_ = n; }
int stars() const { return stars_; }
void stars(int n){ stars_ = n; }
const std::string& img_name() const { return img_name_; }
void img_name(const std::string& name) { img_name_ = name; };
};
This is the code
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
class FIXML {
private: Order Order_object = new Order();
public:
Order getOrder_object()
{
return Order_object;
}
void setOrder_object(Order Order_object)
{
this->Order_object = Order_object;
}
};
class Order {
public:
string ClOrdID = "123456";
string Side = "2";
string TransactTm = "2001-09-11T09:30:47-05:00";
string OrdTyp = "2";
string Px = "93.25";
string Acct = "26522154";
Hdr Hdr_object = Hdr();
Instrmt Instrmt_object = Instrmt();
OrdQty OrdQty_object = OrdQty();
public:
string getClOrdID()
{
return ClOrdID;
}
string getSide()
{
return Side;
}
string getTransactTm()
{
return TransactTm;
}
string getOrdTyp()
{
return OrdTyp;
}
string getPx()
{
return Px;
}
string getAcct()
{
return Acct;
}
Hdr getHdr_object()
{
return Hdr_object;
}
Instrmt getInstrmt_object()
{
return Instrmt_object;
}
OrdQty getOrdQty_object()
{
return OrdQty_object;
}
void setClOrdID(string ClOrdID)
{
this->ClOrdID = ClOrdID;
}
void setSide(string Side)
{
this->Side = Side;
}
void setTransactTm(string TransactTm)
{
this->TransactTm = TransactTm;
}
void setOrdTyp(string OrdTyp)
{
this->OrdTyp = OrdTyp;
}
void setPx(string Px)
{
this->Px = Px;
}
void setAcct(string Acct)
{
this->Acct = Acct;
}
void setHdr_object(Hdr Hdr_object)
{
this->Hdr_object = Hdr_object;
}
void setInstrmt_object(Instrmt Instrmt_object)
{
this->Instrmt_object = Instrmt_object;
}
void setOrdQty_object(OrdQty OrdQty_object)
{
this->OrdQty_object = OrdQty_object;
}
};
class Hdr {
private:
string Snt = "2001-09-11T09:30:47-05:00";
string PosDup = "N";
string PosRsnd = "N";
string SeqNum = "521";
Sndr Sndr_object = Sndr();
Tgt Tgt_object = Tgt();
public:
string getSnt()
{
return Snt;
}
string getPosDup()
{
return PosDup;
}
string getPosRsnd()
{
return PosRsnd;
}
string getSeqNum()
{
return SeqNum;
}
Sndr getSndr_object()
{
return Sndr_object;
}
Tgt getTgt_object()
{
return Tgt_object;
}
void setSnt(string Snt)
{
this->Snt = Snt;
}
void setPosDup(string PosDup)
{
this->PosDup = PosDup;
}
void setPosRsnd(string PosRsnd)
{
this->PosRsnd = PosRsnd;
}
void setSeqNum(string SeqNum)
{
this->SeqNum = SeqNum;
}
void setSndr_object(Sndr Sndr_object)
{
this->Sndr_object = Sndr_object;
}
void setTgt_object(Tgt Tgt_object)
{
this->Tgt_object = Tgt_object;
}
};
class Sndr {
private:
string ID = "AFUNDMGR";
public:
string getID()
{
return ID;
}
void setID(string ID)
{
this->ID = ID;
}
};
class Tgt {
private:
string ID = "ABROKER";
public:
string getID()
{
return ID;
}
void setID(string ID)
{
this->ID = ID;
}
};
class Instrmt {
private:
string Sym = "IBM";
string ID = "459200101";
string IDSrc = "1";
public:
string getSym()
{
return Sym;
}
string getID()
{
return ID;
}
string getIDSrc()
{
return IDSrc;
}
void setSym(string Sym)
{
this->Sym = Sym;
}
void setID(string ID)
{
this->ID = ID;
}
void setIDSrc(string IDSrc)
{
this->IDSrc = IDSrc;
}
};
class OrdQty {
private:
string Qty = "1000";
public:
string getQty()
{
return Qty;
}
void setQty(string Qty)
{
this->Qty = Qty;
}
};
return 0;
}
All the classes I've declared, whether it's Order, Tgt, Sndr. Whenever I make a new instance of these classes, I get the error "Error: identifier classname is undefined"
Thanks in advance
Try declaring them (a) before you use them, and (b) outside of any function:
#include <iostream>
class Test
{
public:
Test() { std::cout << "Test!" << std::endl; }
};
int main()
{
Test t;
}
Once you finish reordering them based on which classes are used by which, it may end up like this:
class OrdQty {
// ...
};
class Instrmt {
// ...
};
class Sndr {
// ...
};
class Tgt {
// ...
};
class Hdr {
// ...
};
class Order {
// ...
};
class FIXML {
// ...
};
int main()
{
return 0;
}
Once you finish that, you'll find that this line is incorrect:
private: Order Order_object = new Order();
You can't initialize a member of a class like this. You'll need to do this in the constructor, copy constructor, and assignment operator, and then clean it up in the destructor.
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 = ...