473,779 Members | 1,873 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Aff
Dear Brothers,
i am facing a problem

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

public:

compactdisc();
compactdisc(cha r [],int);
};

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

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

cd_drive::cd_dr ive(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_d rive()
{
// 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("micro soft",210,c2);
delete c2;
return 0;
}

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

thanks
your Brother.

Oct 16 '06 #1
1 1349
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(cha r [],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_dr ive(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[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.
ptr=new compactdisc;
ptr=a;
Why have you done this? This leaks the compactdisc object you have just
created.
>
}

*************** *************** *************** ******
cd_drive::~cd_d rive()
{
// 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("micro soft",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.
Oct 16 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
1113
by: matt dittman | last post by:
I have created a windows service that reads emails from a drop directory and moves them to the appropriate mail folder every 15 seconds. I can move, rename and delete the files as needed, up until the CDO.DropDirectory.GetMessages() method is called. At this point, the files are locked until I shut down the service. After processing and delivery, I need to be able to delete all the files in the drop directory. I can delete them via...
4
1609
by: Merlin | last post by:
Hi Imagine the following classes (A class diagram will help) BASE, A, B, C, D, E, F, G. A, B, C, D, G inherit from BASE. E, F inherit from D.
14
1384
by: Jason Heyes | last post by:
I use a template class called Derived to instantiate derived classes of class Abstract. Is this an OO concept? Here is some code to illustrate: #include <iostream> class Abstract { public: virtual void func() const = 0;
3
2351
by: Steven Fox | last post by:
============================================================ About DB2 Administration Tools Environment ============================================================ DB2 administration tools level: Product identifier SQL08015 Level identifier 02060106 Level DB2 v8.1.5.449 Build level s040212 PTF WR21334...
5
3782
by: John Wood | last post by:
Let's say you're provided with an instance of a class. The instantiation takes place in another module that you have no control over. However, you've extended that class with your own value-added functionality. In C#, given such an instance, and a derived class, there's no way to 'attach' the instance to the class -- you have to either change the way the class was instantiated (not possible in this case), or wrap the class and delegate...
4
10968
by: cmrchs | last post by:
Hi, how do I implement aggregation and how composition in C# ? When I say : an Airplane has a Pilot then I use aggregation but when I say : an Airplane has a Cockpit then I use composition. How do I implement the difference in C# ? Here's what I try : class Pilot
23
2129
by: SenthilVel | last post by:
Hi Can any one let me know the websites/Pdf for learning Aggragation in C#?? Thanks Senthil
2
14827
by: Gary Wessle | last post by:
Hi what is the difference "in code writing" between aggregation and composition? is the following correct? aggregation (life time of both objects are independent). class A{ /* ... */ }; class B{ A& a; /* ... */};
6
7849
by: Jeff | last post by:
hey Can OO Aggregation be described as: - A system of objects that are built using each other any comments? Jeff
0
9632
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9471
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10302
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
7478
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6723
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5372
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5501
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3631
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2867
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.