C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Problem in the aggregation concept.(during deletion of the p

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Aff@n
Guest





PostPosted: Mon Oct 16, 2006 6:51 am    Post subject: Problem in the aggregation concept.(during deletion of the p Reply with quote



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[i]=a[i];
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.
Back to top
Ian Collins
Guest





PostPosted: Mon Oct 16, 2006 9:10 am    Post subject: Re: Problem in the aggregation concept.(during deletion of t Reply with quote



Aff@n wrote:
Quote:
Dear Brothers,

I'm sure there are some sisters lurking here!

Quote:
i am facing a problem

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


Quote:
{
char title[20];/string a;

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

Quote:
int capacity;

public:

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

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

Quote:
};

class cd_drive
{
int speed;
char manufact[20];
Again, why not a std::string?


Quote:
compactdisc *ptr;

Not a good choice of name. If this class takes ownership of the
compactdisc object, consider using std::auto_ptr here.
Quote:

public:
cd_drive();
cd_drive(char [], int ,compactdisc *);
Again, why not use const std::string& as the first parameter?


Quote:
~cd_drive();
};

cd_drive::cd_drive(char a[], int spd ,compactdisc * a);
{
int counter=0;
counter=strlen(a);
Prefer initialiser lists over assignment.


Quote:
for(int i=0;i<counter;i++)
manufact[i]=a[i];
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.

Quote:
ptr=new compactdisc;
ptr=a;

Why have you done this? This leaks the compactdisc object you have just
created.
Quote:

}

***************************************************
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.
Quote:
}


int main()
{
compactdisc c1("programming",700);
compactdisc *c2;
c2=new compactdisc;

Do these in one line.

Quote:
cd_drive("microsoft",210,c2);

Syntax error, where's the variable name?

Quote:
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.
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.