Problem in the aggregation concept.(during deletion of the pointer of other class).

Discussion in 'C++' started by Aff@n, Oct 16, 2006.

  1. Aff@n

    Aff@n Guest

    Dear Brothers,
    i am facing a problem

    class compactdisc
    {
    char title[20];/string a;
    int capacity;

    public:

    compactdisc();
    compactdisc(char [],int);
    };

    class cd_drive
    {
    int speed;
    char manufact[20];
    compactdisc *ptr;

    public:
    cd_drive();
    cd_drive(char [], int ,compactdisc *);
    ~cd_drive();
    };

    cd_drive::cd_drive(char a[], int spd ,compactdisc * a);
    {
    int counter=0;
    counter=strlen(a);
    for(int i=0;i<counter;i++)
    manufact=a;
    for(int j=counter;j<20;j++)
    manufact[j]=0;
    ptr=new compactdisc;
    ptr=a;

    }

    ***************************************************
    cd_drive::~cd_drive()
    {
    // how can i write the destructor of this if i write this
    statement it gives memory leakage
    // what is the reason about this ..is the way of deletion is
    incorrect or any thing else

    delete ptr;
    }


    int main()
    {
    compactdisc c1("programming",700);
    compactdisc *c2;
    c2=new compactdisc;
    cd_drive("microsoft",210,c2);
    delete c2;
    return 0;
    }

    Please explain ! and also if any suggestion to make the program
    efficient ..

    thanks
    your Brother.
    Aff@n, Oct 16, 2006
    #1
    1. Advertising

  2. Aff@n

    Ian Collins Guest

    Re: Problem in the aggregation concept.(during deletion of the pointerof other class).

    Aff@n wrote:
    > Dear Brothers,


    I'm sure there are some sisters lurking here!

    > i am facing a problem
    >
    > class compactdisc

    It's common practice to capitalise the fist letter of a class name.

    > {
    > char title[20];/string a;


    I assume these should have been two lines? Also, why isn't title a
    std::string?

    > int capacity;
    >
    > public:
    >
    > compactdisc();
    > compactdisc(char [],int);


    Why not use const std::string& as the first parameter?

    > };
    >
    > class cd_drive
    > {
    > int speed;
    > char manufact[20];

    Again, why not a std::string?

    > compactdisc *ptr;


    Not a good choice of name. If this class takes ownership of the
    compactdisc object, consider using std::auto_ptr here.
    >
    > public:
    > cd_drive();
    > cd_drive(char [], int ,compactdisc *);

    Again, why not use const std::string& as the first parameter?

    > ~cd_drive();
    > };
    >
    > cd_drive::cd_drive(char a[], int spd ,compactdisc * a);
    > {
    > int counter=0;
    > counter=strlen(a);

    Prefer initialiser lists over assignment.

    > for(int i=0;i<counter;i++)
    > manufact=a;
    > for(int j=counter;j<20;j++)
    > manufact[j]=0;


    If you had used std::string, these could have been part of the
    initialiser list.

    > ptr=new compactdisc;
    > ptr=a;


    Why have you done this? This leaks the compactdisc object you have just
    created.
    >
    > }
    >
    > ***************************************************
    > cd_drive::~cd_drive()
    > {
    > // how can i write the destructor of this if i write this
    > statement it gives memory leakage
    > // what is the reason about this ..is the way of deletion is
    > incorrect or any thing else
    >
    > delete ptr;


    Not required if you use std::auto_ptr.
    > }
    >
    >
    > int main()
    > {
    > compactdisc c1("programming",700);
    > compactdisc *c2;
    > c2=new compactdisc;


    Do these in one line.

    > cd_drive("microsoft",210,c2);


    Syntax error, where's the variable name?

    > delete c2;


    You have passed c2 into a cd_drive object that will delete it when it
    goes out of scope, so you end up deleting the same object twice. Not a
    good idea.

    --
    Ian Collins.
    Ian Collins, Oct 16, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rajen
    Replies:
    4
    Views:
    2,202
    Rolf Magnus
    Sep 6, 2006
  2. Astrith
    Replies:
    0
    Views:
    347
    Astrith
    Sep 25, 2008
  3. sjsean
    Replies:
    0
    Views:
    1,030
    sjsean
    Feb 7, 2009
  4. Charles Hixson
    Replies:
    0
    Views:
    45
    Charles Hixson
    Apr 25, 2014
  5. Chris Angelico
    Replies:
    0
    Views:
    51
    Chris Angelico
    Apr 25, 2014
Loading...

Share This Page