Solved

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

Posted on 2003-12-02
5
225 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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
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 goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

770 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