Thread: please check this out

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    9

    please check this out

    #include <iostream.h>
    #include <conio.h>
    #include <string.h>

    class PassBook {
    protected:
    char PassBookId[6];
    int age;
    float balance;
    char name[30];
    public :
    void PrintData()
    {
    cout<<name<<" is "<<age<<" "<<"yrs. old and he has a passbook. The identification of"<<endl;
    cout<<"the pass book is "<<PassBookId<<" "<<"and the balance is "<<balance<<endl;

    }
    };

    class Customer: public PassBook {
    public:
    void setCustomer(char n[],int a)
    {
    cout<<n;
    strcpy(n,name);
    age = a;
    }
    };

    class AccountDetails: public PassBook {
    public :
    void setAccount(float bal){
    balance = bal;
    strcpy(PassBookId,"A1235F");
    }
    };

    Customer cust;
    AccountDetails custad;

    int main()
    {
    clrscr();

    cust.setCustomer("Mr. PeterWood Black", 34);
    custad.setAccount(40000);
    cust.PrintData();

    getch();
    return 0;
    }

    it wont return value passbookid
    and balance
    please help
    tnx

  2. #2
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    When someone's erady to reply, they will - this is the third time you've posted this question... Cut it out....

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    9

    Talking soory

    sorry

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    It should be strcpy(name, n);

    and in this line PassBookId is declared as

    char PassBookId[6];

    meaning it will hold 5 useful char and a terminating null char as the sixth char. However, in this line:

    strcpy(PassBookId,"A1235F");

    you try to copy 6 useful char into PassBookId which is going to overwrite the array PassBookId uses and prevent the terminating null char from being entered into PassBookId so it can't be used as a string so PrintData() can't output it as a string. To correct this change the size of PassBookId in the declaration.

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    9
    didnt work
    i tried to use pointer
    but returned hex

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    in setCustomer() you have a line:

    cout << n << endl;

    to check that the name you pass from main() to setCustomer() is being passed correctly. I write simialar lines after:

    strcpy(name, n);

    and

    age = a;

    to be sure the variables are being assigned correctly. You can do the same in setAccount(). This will help you localize the error.

    On a more basic level I don't see the need for the derived classes as they don't add any additional details to the base class. You could use public functions within the base class do set the various protected members and lose the headache of inheriting. In fact, now that I think about it, that is probably a big part of the problem you describe. Think of it like this: cust is a PassBook and custAd is a passBook but cust and custAd have nothing to do with each other, other than they are both derived from the same class. (Just like a lizard is an animal and a human is an animal, but otherwise the two classes of animals are separate and any given lizard will have little to do with any given human.) The information you enter into cust by using setCustomer() doesn't change the name and age in custad and the information you enter into custAd using setAccount() does not affect the balance and PassBookId in cust. Therefore PassBookId and balance remain undefined in cust; and name and age remain undefined in custAd in your code. To prove it declare a default constructor in PassBook and in it intialize PassBookId to "blank" and initialize age to 0 and initialize name to "Hal" and initialize balance to 0.00. Then add the line custAd.PrintData() just before return 0; in main().

    You still need to fix the problems identified in my first post and in the first item of discussion as well, but this is also a boo boo.
    Last edited by elad; 03-15-2002 at 11:28 AM.

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    9

    Question

    is informatics a good institute?

  8. #8
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    HEYYYY

    This fellow is registered as Unregistered, which prevents anyone else from posting w/o logging in.

  9. #9
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    Thanks

  10. #10
    Actually he registered as "notregistered" not "Unregistered" and i dont believe it would matter either way, AFAIK. The board would still work fine. He would appear as "Unregistered" and people that really weren't would appear as "Unregistered".
    "There's always another way"
    -lightatdawn (lightatdawn.cprogramming.com)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BN_CLICKED, change button style
    By bennyandthejets in forum Windows Programming
    Replies: 13
    Last Post: 07-05-2010, 11:42 PM
  2. how to check input is decimal or not?
    By kalamram in forum C Programming
    Replies: 3
    Last Post: 08-31-2007, 07:07 PM
  3. Please check this loop
    By Daesom in forum C++ Programming
    Replies: 13
    Last Post: 11-02-2006, 01:52 AM
  4. A way to check for Win98 or WinXP
    By Shadow in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 10-31-2002, 11:06 AM
  5. how to check for end of line in a text file
    By anooj123 in forum C++ Programming
    Replies: 6
    Last Post: 10-24-2002, 11:21 PM