Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2003-12-02
5
Medium Priority
?
232 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 400 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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 tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

610 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