473,387 Members | 1,585 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Overloading classes problem...

I'm trying to run this program and it works, but I would like to clean it up and make it more acceptable. One of the problems I am having is that when the program runs when I put p1 to 100 to print the random numbers it will print them, but then the program crashes. When I put p1 = 1300, it runs because that is the number of actual integers in the file I'm reading from. Here is the source code.

Expand|Select|Wrap|Line Numbers
  1. class Data1{
  2. protected:
  3. int *p1;
  4. int size1;
  5. public:
  6. Data1();
  7. Data1(int);
  8. ~Data1();
  9. int randomData();
  10. int printData();
  11. }; // Data1
  12.  
  13. Data1::Data1(){
  14. p1 = new int[1300];
  15. size1 = 1300;
  16. }
  17. Data1::Data1(int x1){
  18. p1 = new int[x1];
  19. size1 = x1;
  20. }
  21.  
  22. Data1::~Data1(){
  23. delete [] p1;
  24. }
  25.  
  26. Data1::randomData(){ 
  27. int i;
  28. for(i=0; i<size1; i++){
  29. *(p1+i) = random(899) + 100;
  30. }
  31. return 0;
  32. }
  33.  
  34.  
  35. Data1::printData(){
  36. int i;
  37. cout << "\n";
  38. for(i=0; i<size1; i++){
  39. cout << "\t" << *(p1+i);
  40. }
  41. return 0;
  42. }
  43. class Data2a: public Data1{
  44. protected:
  45. int forever;
  46. int ct;
  47. public:
  48. Data2a();
  49. Data2a(int);
  50. ~Data2a();
  51. int fileInput1();
  52. int fileOutput1();
  53. }; // data2
  54. Data2a::Data2a(){
  55. forever = 1;
  56. ct = 0;
  57. }
  58. Data2a::Data2a(int n1){
  59. p1 = new int[n1];
  60. size1 = n1;
  61. }
  62.  
  63. Data2a::~Data2a(){
  64. delete[]p1;
  65. }
  66.  
  67. Data2a::fileInput1(){
  68. ifstream in1("nums.dat");
  69. while(forever){ 
  70. in1 >> *(p1+ct); 
  71. if(*(p1+ct) == -1) break;
  72. ct++; 
  73. in1.close();
  74. cout << "\n\n\tFile size = " << ct;
  75. return ct;
  76. }
  77.  
  78. Data2a::fileOutput1(){
  79. for(int i=0; i<ct; i++){
  80. cout << " " << *(p1+i);
  81. if((i%15==0)&&(i>0)) cout << "\n";
  82. if((i%150==0)&&(i>0)){
  83. cout << "\n\n\tEnter to continue...\n";
  84. getch();
  85. } // if
  86. } // FOR LOOP
  87. return 0;
  88. }
  89.  
  90. int main(){
  91. clrscr();
  92. Data1 *d1;
  93. Data2a *d2;
  94. d1 = new Data1;
  95. d2 = new Data2a;
  96.  
  97. d1->randomData();
  98. d1->printData();
  99. cout << "\n\n";
  100. d2->fileInput1();
  101. cout << "\n\n";
  102. d2->fileOutput1();
  103.  
  104.  
  105. delete d1;
  106. delete d2;
  107. cout << "\n\n\n\tComplete";
  108. return 0;
  109. } //MAIN
Mar 25 '10 #1
6 1299
RedSon
5,000 Expert 4TB
First off I see hard coded values and magic numbers, which I don't like. What does your code look like in your 100 number example?
Mar 25 '10 #2
if i'm understanding you correctly, i would just change


Expand|Select|Wrap|Line Numbers
  1. Data1::Data1(){
  2.  p1 = new int[1300];
  3.  size1 = 1300;
  4. }
to

Expand|Select|Wrap|Line Numbers
  1. Data1::Data1(){
  2.  p1 = new int[100];
  3.  size1 = 100;
  4. }
Mar 25 '10 #3
RedSon
5,000 Expert 4TB
That's it? Okay, are you still having problems if you make those changes?
Mar 25 '10 #4
correct, when I do that it crashes after printing the 100 random numbers.
Mar 25 '10 #5
weaknessforcats
9,208 Expert Mod 8TB
Probably it'a crashing in your destructors. Set a breakpoint and start debugging from that point. A code step-through should flush the problem out.
Mar 26 '10 #6
okay thanks for your help!
Mar 26 '10 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

17
by: Terje Slettebų | last post by:
To round off my trilogy of "why"'s about PHP... :) If this subject have been discussed before, I'd appreciate a pointer to it. I again haven't found it in a search of the PHP groups. The PHP...
4
by: Dave Theese | last post by:
Hello all, I'm trying to get a grasp of the difference between specializing a function template and overloading it. The example below has a primary template, a specialization and an overload. ...
5
by: | last post by:
Hi all, I've been using C++ for quite a while now and I've come to the point where I need to overload new and delete inorder to track memory and probably some profiling stuff too. I know that...
2
by: pmatos | last post by:
Hi all, I'm overloading operator<< for a lot of classes. The question is about style. I define in each class header the prototype of the overloading as a friend. Now, where should I define the...
16
by: WittyGuy | last post by:
Hi, What is the major difference between function overloading and function templates? Thanks! http://www.gotw.ca/resources/clcm.htm for info about ]
39
by: zeus | last post by:
I know function overloading is not supported in C. I have a few questions about this: 1. Why? is it from technical reasons? if so, which? 2. why wasn't it introduced to the ANSI? 3. Is there any...
10
by: Mihai Osian | last post by:
Hi everyone, Given the code below, can anyone tell me: a) Is this normal behaviour ? b) If it is, what is the reason behind it ? I would expect the A::method(int) to be inherited by B. ...
12
by: Achim Domma | last post by:
Hi, I want to use Python to script some formulas in my application. The user should be able to write something like A = B * C where A,B,C are instances of some wrapper classes. Overloading...
3
by: Chameleon | last post by:
What is better if you want upcasting in intermediate classes like below? Multiple Inheritance and Overloading or simply RTTI? RTTI wants time but MI and Overloading create big objects (because of...
3
by: y-man | last post by:
Hi, I am trying to get an overloaded operator to work inside the class it works on. The situation is something like this: main.cc: #include "object.hh" #include "somefile.hh" object obj,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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,...

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.