Link to home
Start Free TrialLog in
Avatar of africano
africano

asked on

getting logical errors as a result of using POINTERS, with code based on inheritance

i am experiencing a big logical error, i was given this assignment of creating a base class(which i call Client) having two sub classes Customer and Dealer, also create classes called Input and Output, these two would allow for input and output..... i have coded and compiled... no errors but i get junk characters for the output....

HERE IS THE CODE.....
JUST COPY AND PASTE INTO ANY EDITOR
COMPILE AND RUN AND PLEASE TELL WHAT I AM DOING WRONG.......
YOUR HELP IS MY RESOURCE ....

CODE.
===========================
#include <iostream.h>
#include <stdlib.h>
class Client
{
      //pointer variables
      private:
      char *name;
      char *address;

      public:   //constructor
      Client ()
      {
             name = new char;
             address = new char;
      }

      //access functions
      void addName(char *strName){name = strName;}
      void getName()const{cout << name << endl;}
      void addAddress(char *strAddress){address = strAddress;}
      void getAddress()const{cout << address << endl;}
};

class Customer:public Client  //sub class of base class Client
{
      private:
      char *orderNo;
      char *deliveryDate;
      float cost;

      public:
      Customer()
      {
            orderNo = new char;
            deliveryDate = new char;
      }
      ~Customer()
      {
            delete orderNo;
            delete deliveryDate;
      }
     
      //functions
      void addOrderNo(char *strOrderNo){orderNo = strOrderNo;}
      void getOrderNo() const{cout << orderNo << endl;}

      void addDeliveryDate(char *strDelivery){deliveryDate = strDelivery;}
      void getDeliveryDate() const{cout << deliveryDate << endl;}

      void addCost(float fcost){cost = fcost;}
      void getCost()const{cout << cost << endl;}
};

class Dealer:public Client  //sub class of base class Client
{
      private:
      char *invoiceNo;
      float purchaseCost;

      public:
      Dealer()
      {
          invoiceNo = new char;

      }
      ~Dealer()
      {
            delete invoiceNo;
      }

      void addInvoiceNo(char *strInvoice){invoiceNo = strInvoice;}
      void getInvoiceNo() const{cout << invoiceNo << endl;}

      void addPurchaseCost(float fpCost){purchaseCost = fpCost;}
      void getPurchaseCost() const{cout << purchaseCost << endl;}
};

class Input
{

     private:
    //define variables to accept inputs
    char *cusName;
    char *cusAddress;
    char *cusOrderNo;
    char *cusDeliveryDate;
    float cusCost;

    char *dealName;
    char *dealAddress;
    char *dealInvoiceNo;
    float dealPurchase;

     public:
     Input()
     {
         cusName = new char;
         cusAddress = new char;
         cusOrderNo = new char;
         cusDeliveryDate = new char;

         dealName = new char;
         dealAddress = new char;
         dealInvoiceNo = new char;

     }

    //create objects of classes Customer and Dealer
    Customer cus;
    Dealer deal;
    //define input functions
    void acceptInput()
    {
         cout << "Accept input for Customer" << endl;
         cout << "Enter Name";
         cin >> cusName;

         cout << "Enter the Address" << endl;
         cin >> cusAddress;

         cout << "Enter the Order No."<< endl;
         cin >> cusOrderNo;

         cout << "Enter the Delivery Date" << endl;
         cin >>  cusDeliveryDate;

         cout << "Enter the Total Cost" << endl;
         cin >> cusCost;

         cout << "=====================================" << endl;
         cout << "Enter input for Dealer" << endl;
         cout << "Enter the name" << endl;
         cin >> dealName;

         cout << "Enter the Address" << endl;
         cin >> dealAddress;

         cout << "Enter the InvoiceNo" << endl;
         cin >> dealInvoiceNo;

         cout << "Enter the Total Cost of Purchase" << endl;
         cin >> dealPurchase;

         //load input into functions
         cus.addName(cusName);
         cus.addAddress(cusAddress);
         cus.addOrderNo(cusOrderNo);
         cus.addDeliveryDate(cusDeliveryDate);
         cus.addCost(cusCost);

         deal.addName(dealName);
         deal.addAddress(dealAddress);
         deal.addInvoiceNo(dealInvoiceNo);
         deal.addPurchaseCost(dealPurchase);

    }
};

class Output
{
      public:
      //create object of the classes customer and dealer
      Customer cus;
      Dealer deal;

      void displayOutput()
      {
           cout << "Customer Details" << endl;
           cout << "------------------" << endl;
           cus.getName();
           cus.getAddress();
           cus.getOrderNo();
           cus.getDeliveryDate();
           cus.getCost();

           cout << "Dealer Details" << endl;
           cout << "----------------" << endl;
           deal.getName();
           deal.getAddress();
           deal.getInvoiceNo();
           deal.getPurchaseCost();
      }
};



int main()
{
           //create objects of input and output classes
           Input inp;
           Output outp;
           
           //accept
           inp.acceptInput();
           outp.displayOutput();
           

     

      system("PAUSE");
      return 0;
}
ASKER CERTIFIED SOLUTION
Avatar of mnashadka
mnashadka

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Another solution would be to use a string class instead of the char arrays. The STL string should be available with any modern compiler. A string class would take care of all the memory management, so you would not have to allocate and free memory.

Avatar of migoEX
migoEX

No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Accept mnashadka's comment as answer.

Please leave any comments here within the next four days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

migoEX
EE Cleanup Volunteer
Split between mnashadka and khkremer