0

This is a program I wrote for a test (don't worry, it has ended). I implemented composition and aggregation as wanted by the question. However, I didn't get the output that I wanted.

Here is the code


    #include <iostream>
    #include <string>
    #include <iomanip>
    using namespace std;
    
    class Name
    {
        private:
            string firstName, lastName;
            
        public:
            Name(string fN, string lN)
            {
                fN = firstName;
                lN = lastName;
            }
            string getFullName()
            {
                string space = " ";
                string full;
                firstName.append(space);
                full = firstName.append(lastName);
                return full;
            }
    };
    
    class Product
    {
        private:
            string name, category;
            double price;
            int quantity;
            
        public:
            Product()
            {
                name = " ";
                category = " ";
                price = 0;
                quantity = 0;
            }
            Product(string n, string c, double p)
            {
                name = n;
                category = c;
                price = p;
            }
            string getName()
            {
                return name;
            }
            string getCategory()
            {
                return category;
            }
            double getPrice()
            {
                return price;
            }
            int getQuantity()
            {
                return quantity;
            }
            void setQuantity(int q)
            {
                quantity = q;
            }
    };
    
    class Customer
    {
        private:
            string address;
            int numProduct;
            Product *product;
            
        public:
            Name name;
            Customer(string fn, string ln, string add) : name(fn, ln)
            {
                address = add;
                numProduct = 0;
                Product **ptr = new Product*[4];
            }
            void buy(Product p, int num)
            {
                int i=0;
                if (i<4)
                {
                num = numProduct;
                p.setQuantity(num);
                i++;
                //p[i].setQuantity(num);
                }
                else
                {
                cout << "Sorry!! You already reached the maximum number of products purchased." << endl;
                }
            }
            
            void print()
            {
                double total=0;
                cout << left << setw(7) << "Name" << ": " << name.getFullName() << endl;
                cout << "Address: " << address << endl;
                cout << "Number of products purchased: 4" << endl;
                
                cout << left;
                cout << setw(4) << "No" << setw(15) << "Product Name" << setw(10) << "Category"
                << setw(10) << "Quantity" << setw(20) << "Unit Price (RM)" << setw(15) << "Amount (RM)" << endl;
                
                for (int i=0; i<4; i++)
                {
                    cout << left;
                    cout << fixed << setprecision(2);
                    cout << setw(4) << i+1 << setw(15) << product[i].getName() << setw(15) << product[i].getCategory()
                    << setw(10) << product[i].getQuantity() << setw(20) << product[i].getPrice() << setw(15) <<
                    product[i].getPrice()*product[i].getQuantity() << endl; 
                    
                    total += product[i].getPrice()*product[i].getQuantity();
                } 
                
                cout << fixed << setprecision(2) << "Total price = " << total;
            }
    };
    
    int main() {
        Customer cust("Amir", "Jalil", "Masai, Johor");
        Product p1("Jacob", "Biscuit", 14.8);
        Product p2("Twister", "Drink", 7.5);
        Product p3("Ayamas", "Nugget", 18.4);
        Product p4("Oreo", "Biscuit", 3.8);
        
        cust.buy(p4, 5);
        cust.buy(p2, 4);
        cust.buy(p3, 2);
        cust.buy(p1, 3);
        cust.print();
        
        return 0;
    }

Here is the output that I should get: output

But, this is the output that I got: output

I know that things gone wrong in the Customer class, but I don't know how to solve it. If needed, the below is the instruction for the class. instruction

Majid Hajibaba
  • 3,105
  • 6
  • 23
  • 55
flyhigh
  • 1
  • 2

2 Answers2

0
Product **ptr = new Product*[4];

Sets a local variable. It doesn't prepare any storage in your object. And the variable is immediately discarded. You probably want something like:

product = new Product[4]; // <-- EDIT

The line where you comment out:

 //p[i].setQuantity(num);

Attempted to write to a specific product. However, as-is, your code just modifies a product passed by value. Which also discard the changes. You probably want:

product[i].setQuantity(num); // <-- EDIT

You'll need to fix this in smaller steps.

Jeffrey
  • 11,063
  • 1
  • 21
  • 42
  • without the array, how do I assign Product pointer to array of Product objects as instructed in the question? or I should declare it outside of the constructor? – flyhigh May 25 '21 at 11:38
  • see edits. But please, from here, work on it, try to understand this and other answers. Don't just ask the next thing right away :-) – Jeffrey May 25 '21 at 13:30
  • aha now I see it. Will work on it. The **ptr is a new thing for me, so it got a little confusing. Thank you for the help! – flyhigh May 26 '21 at 03:52
0
Name(string fN, string lN)
        {
            fN = firstName;
            lN = lastName;
        }

Wrong constructor equivalent.

firstName=fN; lastName=lN;

In this case fN and lN are getting trash values.

  • oh gosh, thank you so much for pointing out that one. such a careless mistake. the output has displayed the name, but not the products list yet. nevertheless, thank you again. – flyhigh May 25 '21 at 11:46
  • @flyhigh Also a wrong definition of 2d pointer array. check [link](https://stackoverflow.com/questions/1768294/how-to-allocate-a-2d-array-of-pointers-in-c) – Rawad Shakeb May 25 '21 at 11:51