Solved

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

Posted on 2003-12-02
5
227 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

726 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