| | |
Creating and destroying objects
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jan 2004
Posts: 7
Reputation:
Solved Threads: 0
Hi
After getting great help with my first post here I thought I would try again. I need to create a (dynamic) number of objects of two types, eggs and fry. I then need to be able to delete the fry and then make a new group of fry objects with all the info from the members of the egg objects (i.e. the egg objects will become the new fry objects). the number of objects will vary so I cannot just over write the old values. I have some code that actually works, but as the program exits a Windows error box appears. I am thinking some sort of memory problem but I am stumped. My code is:
#include <iostream>
#include <stdlib.h>
using namespace std;
#include "class1.hpp"
#include "class2.hpp"
int main()
{
Stage1 *eggs;
eggs = new Stage1[3];
for(int i=0;i<3;i++)
{
cout << "Egg number " << i << " Age " << eggs[i].getAge() << " Weight " << eggs[i].getWeight() << endl;
}
Stage2 *fry;
fry = new Stage2[3];
cout << endl;
for(int i=0;i<3;i++)
{
cout << "Fry number " << i << " Age " << fry[i].getAge() << " Weight " << fry[i].getWeight() << endl;
}
delete [] fry;
cout << endl;
for(int i=0;i<3;i++)
{
Stage2 *fry;
fry = new Stage2[i];
fry[i].setAge( eggs[i].getAge() );
fry[i].setWeight( eggs[i].getWeight() );
cout << "New Fry number " << i << " Age " << fry[i].getAge() << " Weight " << fry[i].getWeight() << endl;
}
cin.get();
return 0;
}
The header files are:
class1.hpp
//********************************************
class Stage1
{
public:
Stage1();
~Stage1();
int getAge() {return itsAge;}
void setAge (int Age) {itsAge = Age;}
int getWeight () {return itsWeight;}
void setWeight (int Weight) {itsWeight = Weight;}
protected:
int itsAge;
int itsWeight;
};
Stage1:
tage1():
itsAge(1),
itsWeight(5)
{
}
Stage1::~Stage1()
{
}
class2.hpp
//********************************************
class Stage2
{
public:
Stage2();
~Stage2();
int getAge() {return itsAge;}
void setAge (int Age) {itsAge = Age;}
int getWeight () {return itsWeight;}
void setWeight (int Weight) {itsWeight = Weight;}
protected:
int itsAge;
int itsWeight;
};
Stage2:
tage2():
itsAge(2),
itsWeight(9)
{
}
Stage2::~Stage2()
{
}
As I said, this works fine and I get the correct values, however on program exit I get an error box saying errors have been generated and the top of the log file looks like this:
Application exception occurred:
App: (pid=680)
When: 12/5/2003 @ 14:50:59.859
Exception number: c0000005 (access violation)
Please help
Fishman
PS I know this code could have been done better with derived or virtual classes but it is just to try to illustrate my problem
After getting great help with my first post here I thought I would try again. I need to create a (dynamic) number of objects of two types, eggs and fry. I then need to be able to delete the fry and then make a new group of fry objects with all the info from the members of the egg objects (i.e. the egg objects will become the new fry objects). the number of objects will vary so I cannot just over write the old values. I have some code that actually works, but as the program exits a Windows error box appears. I am thinking some sort of memory problem but I am stumped. My code is:
#include <iostream>
#include <stdlib.h>
using namespace std;
#include "class1.hpp"
#include "class2.hpp"
int main()
{
Stage1 *eggs;
eggs = new Stage1[3];
for(int i=0;i<3;i++)
{
cout << "Egg number " << i << " Age " << eggs[i].getAge() << " Weight " << eggs[i].getWeight() << endl;
}
Stage2 *fry;
fry = new Stage2[3];
cout << endl;
for(int i=0;i<3;i++)
{
cout << "Fry number " << i << " Age " << fry[i].getAge() << " Weight " << fry[i].getWeight() << endl;
}
delete [] fry;
cout << endl;
for(int i=0;i<3;i++)
{
Stage2 *fry;
fry = new Stage2[i];
fry[i].setAge( eggs[i].getAge() );
fry[i].setWeight( eggs[i].getWeight() );
cout << "New Fry number " << i << " Age " << fry[i].getAge() << " Weight " << fry[i].getWeight() << endl;
}
cin.get();
return 0;
}
The header files are:
class1.hpp
//********************************************
class Stage1
{
public:
Stage1();
~Stage1();
int getAge() {return itsAge;}
void setAge (int Age) {itsAge = Age;}
int getWeight () {return itsWeight;}
void setWeight (int Weight) {itsWeight = Weight;}
protected:
int itsAge;
int itsWeight;
};
Stage1:
tage1():itsAge(1),
itsWeight(5)
{
}
Stage1::~Stage1()
{
}
class2.hpp
//********************************************
class Stage2
{
public:
Stage2();
~Stage2();
int getAge() {return itsAge;}
void setAge (int Age) {itsAge = Age;}
int getWeight () {return itsWeight;}
void setWeight (int Weight) {itsWeight = Weight;}
protected:
int itsAge;
int itsWeight;
};
Stage2:
tage2():itsAge(2),
itsWeight(9)
{
}
Stage2::~Stage2()
{
}
As I said, this works fine and I get the correct values, however on program exit I get an error box saying errors have been generated and the top of the log file looks like this:
Application exception occurred:
App: (pid=680)
When: 12/5/2003 @ 14:50:59.859
Exception number: c0000005 (access violation)
Please help
Fishman
PS I know this code could have been done better with derived or virtual classes but it is just to try to illustrate my problem
•
•
Join Date: Jan 2004
Posts: 7
Reputation:
Solved Threads: 0
Hi Bob
Sorry if cross posting is frowned upon, my impatience got the better of me! I have not been able to log onto the other forum (I think their server is playing up) but have been trying to solve the problem on my own and think I have succeeded, at least I have some working code. I think the problem with the original was that I was creating objects in different scopes. The first batch of fry were made in the main function, the second batch were in curly brackets in an if loop. If I try to do all from the main function it all seems to work.
int noeggs = 5, nofry = 5;
Stage1 *eggs;
eggs = new Stage1[noeggs](5);
cout << "Eggs age is " << eggs[1].getAge() << endl;
Stage2 *fry;
fry = new Stage2[nofry](10);
cout << "Fry age is " << fry[1].getAge() << endl;
delete [] fry;
fry = new Stage2[noeggs](0);
cout << "new fry age is " << fry[1].getAge() << endl;
fry[1].setAge(eggs[1].getAge());
cout << "reset fry age is " << eggs[1].getAge() << endl;
This is a bit of code to show how I solved the problem, it illustrates how I now creata and destroy and re-create the objects in the main function and pass the egg ages to the fry. I will post the reply from the other forum when I can log on for others information and will not cross post in future.
fishman
Sorry if cross posting is frowned upon, my impatience got the better of me! I have not been able to log onto the other forum (I think their server is playing up) but have been trying to solve the problem on my own and think I have succeeded, at least I have some working code. I think the problem with the original was that I was creating objects in different scopes. The first batch of fry were made in the main function, the second batch were in curly brackets in an if loop. If I try to do all from the main function it all seems to work.
int noeggs = 5, nofry = 5;
Stage1 *eggs;
eggs = new Stage1[noeggs](5);
cout << "Eggs age is " << eggs[1].getAge() << endl;
Stage2 *fry;
fry = new Stage2[nofry](10);
cout << "Fry age is " << fry[1].getAge() << endl;
delete [] fry;
fry = new Stage2[noeggs](0);
cout << "new fry age is " << fry[1].getAge() << endl;
fry[1].setAge(eggs[1].getAge());
cout << "reset fry age is " << eggs[1].getAge() << endl;
This is a bit of code to show how I solved the problem, it illustrates how I now creata and destroy and re-create the objects in the main function and pass the egg ages to the fry. I will post the reply from the other forum when I can log on for others information and will not cross post in future.
fishman
![]() |
Similar Threads
- garbage collector (C#)
- Creating Instantces of objects w/ pygame (Python)
- Am I creating/using the objects correctly? (C++)
- Several C++ Designing Questions (C++)
- Efficient Programming (Computer Science)
Other Threads in the C++ Forum
- Previous Thread: How do you add animation to visual C++ program?
- Next Thread: Cannot get switch to execute
| Thread Tools | Search this Thread |
api array arrays based binary c++ c/c++ calculator char char* class classes code coding compile console conversion convert count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets





