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 6892001
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:28:49+00:00 2026-05-27T06:28:49+00:00

I am trying to use a paymentList vector which has Cash, Cheque and Credit

  • 0

I am trying to use a paymentList vector which has Cash, Cheque and Credit objects (which are derived classes of Payment) inside of the vector.

I declare the vector like this:

typedef std::vector<Payment*> ListOfPayments;

I add payments like this:

std::cout << "How would you like to pay?" << std::endl;
std::cout << "1. Cash"  <<std::endl;
std::cout << "2. Credit"<<std::endl;
std::cout << "3. Cheque"<<std::endl;
std::cin >> choice;

while(choice < 1 || choice > 3)
{
  std::cout<<"Please enter a correct number from 1 to 3"<<std::endl;
  std::cin >> choice;
}
if(choice == 1)
{
  paymentList->push_back(addCash(paymentId,orderId));
}
else if(choice == 2)
{
  paymentList->push_back(addCredit(paymentId,orderId));
}
else
{
  paymentList->push_back(addCheque(paymentId,orderId));
}

I now want to save this vector to a file. I have started a save function but I’m unsure where to go from here:

void savePayment(ListOfPayments *paymentList)
{
    int method;
    Cheque * pCheque = dynamic_cast<Cheque *>(paymentList->at(paymentList->size()-1));
    Cash * pCash = dynamic_cast<Cash *>(paymentList->at(paymentList->size()-1));
    Credit * pCredit = dynamic_cast<Credit *>(paymentList->at(paymentList->size()-1));
    std::ofstream* save = new std::ofstream(); // creates a pointer to a new ofstream
    save->open("Payments.txt"); //opens a text file called payments.
    if (!save->is_open())
    {
        std::cout<<"The file is not open.";
    }
    else
    {
        *save << paymentList->size() << "\n";
        ListOfPayments::iterator iter = paymentList->begin(); 
        while(iter != paymentList->end()) //runs to end 
        {
            method = (*iter)->getMethod();
            *save << method << "\n";
            if(method == 1)
            {
                pCash->saveCashPayments(save);
            }
            else if(method == 2)
            {
                pCredit->saveCreditPayments(save);
            }
            else
            {
                pCheque->saveChequePayments(save);
            }
            iter++;
        }
        save->close();
        delete save;
    }
}

It works if I save one type of payment, but as soon as I have two or more payments in the list I get a violation reading location error. I’m guessing it has to do with the type casts being wrong or something? In case I’m wrong here is an example of my save function that runs based on the method variable.

void Cash::saveCashPayments(std::ofstream* save)
{
*save << this->cashTendered << "\n";
*save << this->getId() << "\n";
*save << this->getAmount() << "\n";
*save << this->getOrderId() << "\n";
*save << this->getMethod() << "\n";
} 

Any help would be appreciated 🙂

  • 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-27T06:28:49+00:00Added an answer on May 27, 2026 at 6:28 am

    That is completely wrong approach.

    A better approach would be runtime polymorphism. Declare a virtual function called Save in base class and define it in each derived class.

    For example, if Payment is the base class, then do this:

    class Payment
    {
       public:
         virtual void Save(std::ostream & out) = 0;
    };
    

    Then implement Save in all derived classes.

    class Cheque : public Payment
    {
       public:
         virtual void Save(std::ostream & out) 
         {
                //implement it
         }
    };
    
    class Cash : public Payment
    {
       public:
         virtual void Save(std::ostream & out) 
         {
                //implement it
         }
    };
    
    class Credit : public Payment
    {
       public:
         virtual void Save(std::ostream & out) 
         {
                //implement it
         }
    };
    

    And then call Save using pointer of Payment* type.

    void savePayment(ListOfPayments & payments)
    {
        std::ofstream file("Payments.txt");
        for(ListOfPayments::iterator it = payments.begin(); it != payments.end(); ++it)
        {
             it->Save(file);
        }
    }
    

    No need to pass payment by pointer; also don’t use new std::ofstream.

    Read about Runtime Polymorphism in C++.

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

Sidebar

Related Questions

I'm trying use an object which wasn't available until SDK level 5. It seems
Trying to use an excpetion class which could provide location reference for XML parsing,
i'm trying use facebook API to upload photo in my fan page. I downloaded
I am trying use gem tire to search in my application. I have tables
I was trying use a set of filter functions to run the appropriate routine,
I'm trying use self-signed certificate (c#): X509Certificate2 cert = new X509Certificate2( Server.MapPath(~/App_Data/myhost.pfx), pass); on
I'm trying use mod_rewrite to rewrite URLs from the following: http://www.site.com/one-two-file.php to http://www.site.com/one/two/file.php The
I am trying use a Java Uploader in a ROR app (for its ease
Hi I'm trying use a datepicker on a field I have. I'm trying to
I am need paint my image. I'm trying use JQuery in here this link:

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.