Solved

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

Posted on 2003-12-02
5
222 Views
Last Modified: 2010-04-02
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;
}
0
Comment
Question by:africano
  • 2
5 Comments
 
LVL 8

Accepted Solution

by:
mnashadka earned 100 total points
ID: 9862279
For each of these, you're only allocating 1 character when you do, for example:
cusName = new char;

You need to make sure that it's big enough to handle the string.  For instance, if cusName could be up to 256 characters, you could do:
cusName = new char[256];
The rest of the code would be the same except for the delete:
delete [] cusName; // [] tells the compiler that it's an array

Another way to do this would be to make them arrays in your classes:

char cusName[256];

Good luck.
0
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
ID: 9862509
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.

0
 
LVL 5

Expert Comment

by:migoEX
ID: 10331491
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
0
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
ID: 10331922
Split between mnashadka and khkremer
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now