Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6094485
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:41:03+00:00 2026-05-23T12:41:03+00:00

Have a homework assignment in which I’m supposed to create a vector of pointers

  • 0

Have a homework assignment in which I’m supposed to create a vector of pointers to objects

Later on down the load, I’ll be using inheritance/polymorphism to extend the class to include fees for two-day delivery, next day air, etc. However, that is not my concern right now. The final goal of the current program is to just print out every object’s content in the vector (name & address) and find it’s shipping cost (weight*cost).

My Trouble is not with the logic, I’m just confused on few points related to objects/pointers/vectors in general. But first my code. I basically cut out everything that does not mater right now, int main, will have user input, but right now I hard-coded two examples.

#include <iostream>
#include <string>
#include <vector>
using namespace std;

class Package {
public:
    Package(); //default constructor
    Package(string d_name, string d_add, string d_zip, string d_city, string d_state, double c, double w);
    double calculateCost(double, double);
    ~Package();

private:    
    string dest_name;
    string dest_address;
    string dest_zip;
    string dest_city;
    string dest_state;
    double weight;
    double cost;

};

Package::Package()
{
    cout<<"Constucting Package Object with default values: "<<endl;
    string dest_name="";
    string dest_address="";
    string dest_zip="";
    string dest_city="";
    string dest_state="";
    double weight=0;
    double cost=0;
}
Package::Package(string d_name, string d_add, string d_zip, string d_city, string d_state, string r_name, string r_add, string r_zip, string r_city, string r_state, double w, double c){

    cout<<"Constucting Package Object with user defined values: "<<endl;
    string dest_name=d_name;
    string dest_address=d_add;
    string dest_zip=d_zip;
    string dest_city=d_city;
    string dest_state=d_state;
    double weight=w;
    double cost=c;
}
Package::~Package()
{
    cout<<"Deconstructing Package Object!"<<endl;
    delete Package;
}
double Package::calculateCost(double x, double y){
    return x+y;
}
int main(){
    double cost=0;
    vector<Package*> shipment;
    cout<<"Enter Shipping Cost: "<<endl;
    cin>>cost;
    shipment.push_back(new Package("tom r","123 thunder road", "90210", "Red Bank", "NJ", cost, 10.5));
    shipment.push_back(new Package ("Harry Potter","10 Madison Avenue", "55555", "New York", "NY", cost, 32.3));
    return 0;

}

So my questions are:

  1. I’m told I have to use a vector
    of Object Pointers, not Objects.
    Why? My assignment calls for it
    specifically, but I’m also told it
    won’t work otherwise.
  2. Where should I be creating this
    vector?
    Should it be part of my Package
    Class? How do I go about adding
    objects into it then?
  3. Do I need a copy constructor? Why?

  4. What’s the proper way to deconstruct
    my vector of object pointers?

Any help would be appreciated. I’ve searched for a lot of related articles on here and I realize that my program will have memory leaks. Using one of the specialized ptrs from boost:: will not be available for me to use. Right now, I’m more concerned with getting the foundation of my program built. That way I can actually get down to the functionality I need to create.

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T12:41:04+00:00Added an answer on May 23, 2026 at 12:41 pm

    A vector of pointers can be reused for storing objects of sub-classes:

    class Person
    {
        public:
        virtual const std::string& to_string () = 0;
        virtual ~Person () { } 
    };
    
    class Student : public Person
    {
       const std::string& to_string ()
       {
           // return name + grade
       }
    };
    
    class Employee : public Person
    {
       const std::string& to_string ()
       {
          // return name + salary
       }
    };
    
    std::vector<Person*> persons;
    person.push_back (new Student (name, grade));
    person.push_back (new Employee (name, salary));
    person[0]->to_string (); // name + grade
    person[1]->to_string (); // name + salary
    

    Ideally the vector should be wrapped up in a class. This makes memory management easier. It also facilitates changing the support data structure (here an std::vector) without breaking existing client code:

    class PersonList
    {
       public:
       Person* AddStudent (const std::string& name, int grade)
       {
           Person* p = new Student (name, grade);
           persons.push_back (p);
           return p;
       }
    
       Person* AddEmployee (const std::string& name, double salary)
       {
           Person* p = new Employee (name, salary);
           persons.push_back (p);
           return p;
       }
        
       ~PersonList ()
       {
          size_t sz = persons.size ();
          for (size_t i = 0; i < sz; ++i)
              delete persons[i];
       }
    
       private
       std::vector<Person*> persons;
    };
    

    So we can re-write our code as:

    {
       PersonList persons;
       Person* student = persons.AddStudent (name, grade);
       Person* employee = persons.AddEmployee (name, salary);
       student.to_string ();
       employee.to_string ();
    } // The memory allocated for the Person objects will be deleted when
      // `persons` go out of scope here.
    

    Getting familiar with the Rule of Three will help you decide when to add a copy constructor to a class. Also read about const correctness.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is part of a homework assignment. What we have to do is write
In my program (it is for a homework assignment), I load in a model
For a homework assignment, I need the logic to find a series of numbers
I'm creating a very simple phone directory for a homework assignment but I'm stuck.
In Python, I have a Graph class that has a dictionary of vertex objects.
I've got what I think is a cipher (this isn't homework, just a challenge
I was referred to this site by a friend. I like that I was
I am currently taking a university course in data structures, and this topic has
Does loop unrolling effect data cache performance in any way? This is related to
Before I begin I want to make it clear that I don't want the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.