Program crashing when initialized

Hi, I just finished coding a program that is based on polymorphism and inheritance but when I ran the program it crashed? I do not know what is the cause of the program crashing.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include<iostream>
#include<string>
using namespace std;

class Shape{
      float density;
      string *name;
      public:
             Shape(string title, float n)
             {
                          *name = title;
                          density = n;
             }
             virtual float dimension()=0;
             string getName(void){return *name;}
             float getDensity(void){return density;}
             virtual void getColor()=0;
             };
             
class Rectangle:public Shape{
      
      public:
             float length;
             float width;
             string rectColor;
             Rectangle(float le, float wi, string co, float de, string tye)
                             :Shape(tye, de)
                             {
                                        length = le;
                                        width = wi;
                                        rectColor = co;
                             }
                             void getColor(void)
                             {
                                  cout<<"Color : " <<rectColor;
                             }
                             float dimension(void)
                             {
                                   float t;
                                   t=length*width;
                                   return t;
                             }
                             };
class Cube: public Rectangle{
      float height;
      public:
             Cube(float len, float wid, float hei, string colo, float dens, string typ)
                        :Rectangle(len,wid,colo,dens,typ)
                        {
                            height = hei;
                        }
                        float dimenstion(void)
                        {
                              float k;
                              k = height*width*length;
                        }
                        };
       void find_Mass(Shape *Sobj_ref)
       {
                cout<<"Dimension : " <<Sobj_ref->dimension();
                cout<<"Density : " <<Sobj_ref->getDensity();
       }
 int main()
 {
     Shape*n[4];
     n[0]=new Rectangle(30,40,"Blue",0.34,"Rectangle A");
     n[1]=new Cube(10,20,5,"Green",1.5,"Cube 1");
     n[2]=new Rectangle(70,9,"Yellow",2.1,"Rectangle B");
     n[3]=new Cube(33,5,15,"Black",0.5,"Cube 2");
     for (int i=0; i<=3; i++)
     find_Mass(n[i]);
     system("PAUSE");
 }    
Last edited on
Your dimension function for Cube doesn't actually return anything, despite supposedly returning a float. You then try to output it.
How would I fix this?
Return something from the function.
k would probably be the best bet.
After I added "return k;" to the coding the program still crashed. :(

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include<iostream>
#include<string>
using namespace std;

class Shape{
      float density;
      string *name;
      public:
             Shape(string title, float n)
             {
                          *name = title;
                          density = n;
             }
             virtual float dimension()=0;
             string getName(void){return *name;}
             float getDensity(void){return density;}
             virtual void getColor()=0;
             };
             
class Rectangle:public Shape{
      
      public:
             float length;
             float width;
             string rectColor;
             Rectangle(float le, float wi, string co, float de, string tye)
                             :Shape(tye, de)
                             {
                                        length = le;
                                        width = wi;
                                        rectColor = co;
                             }
                             void getColor(void)
                             {
                                  cout<<"Color : " <<rectColor;
                             }
                             float dimension(void)
                             {
                                   float t;
                                   t=length*width;
                                   return t;
                             }
                             };
class Cube: public Rectangle{
      float height;
      public:
             Cube(float len, float wid, float hei, string colo, float dens, string typ)
                        :Rectangle(len,wid,colo,dens,typ)
                        {
                            height = hei;
                        }
                        float dimenstion(void)
                        {
                              float k;
                              k = height*width*length;
                              return k;
                        }
                        };
       void find_Mass(Shape *Sobj_ref)
       {
                cout<<"Dimension : " <<Sobj_ref->dimension();
                cout<<"Density : " <<Sobj_ref->getDensity();
       }
 int main()
 {
     Shape*n[4];
     n[0]=new Rectangle(30,40,"Blue",0.34,"Rectangle A");
     n[1]=new Cube(10,20,5,"Green",1.5,"Cube 1");
     n[2]=new Rectangle(70,9,"Yellow",2.1,"Rectangle B");
     n[3]=new Cube(33,5,15,"Black",0.5,"Cube 2");
     for (int i=0; i<=3; i++)
     find_Mass(n[i]);
     system("PAUSE");
 }
Didn't look through the most of the program but you should realize you are misspelling your function at line 52. I'm guessing that's messing up your function call in find_Mass.
Last edited on
Oh haha yes it should've been dimensions but it is still crashing sadly.
It's crashing when you do *name = title; in your Shape constructor. You're dereferencing an uninitialized pointer.

Why not just use a string object directly instead of a pointer to one?
Edit: Going off that... change your Shape to this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Shape{
      float density;
      string name;
      public:
             Shape(string title, float n)
             {
                          name = title;
                          density = n;
             }
             virtual float dimension()=0;
             string getName(void){return name;}
             float getDensity(void){return density;}
             virtual void getColor()=0;
};


Works fine for me now.
Last edited on
Yes it did work after removing the * from the shape constructor! Thank you Ganado and everyone else for the help! Here is the finished coding that displays the dimension, density, color and name when it is run.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include<iostream>
#include<string>
using namespace std;

class Shape{
      float density;
      string name;
      public:
             Shape(string title, float n)
             {
                          name = title;
                          density = n;
             }
             virtual float dimension()=0;
             string getName(void){return name;}
             float getDensity(void){return density;}
             virtual string getColor()=0;
             };
             
class Rectangle:public Shape{
      
      public:
             float length;
             float width;
             string rectColor;
             Rectangle(float le, float wi, string co, float de, string tye)
                             :Shape(tye, de)
                             {
                                        length = le;
                                        width = wi;
                                        rectColor = co;
                             }
                             string getColor(void)
                             {
                                    string r;
                                    r = rectColor;
                                    return r;
                             }
                             float dimension(void)
                             {
                                   float t;
                                   t=length*width;
                                   return t;
                             }
                             };
class Cube: public Rectangle{
      float height;
      public:
             Cube(float len, float wid, float hei, string colo, float dens, string typ)
                        :Rectangle(len,wid,colo,dens,typ)
                        {
                            height = hei;
                        }
                        float dimension(void)
                        {
                              float k;
                              k = height*width*length;
                              return k;
                        }
                        };
       void find_Mass(Shape *Sobj_ref)
       {
                cout<<"Name : " <<Sobj_ref->getName()<<endl;
                cout<<"Color : "<<Sobj_ref->getColor()<<endl;
                cout<<"Dimension : " <<Sobj_ref->dimension()<<endl;
                cout<<"Density : " <<Sobj_ref->getDensity()<<endl;
                
       }
 int main()
 {
     Shape*n[4];
     n[0]=new Rectangle(30,40,"Blue",0.34,"Rectangle A");
     n[1]=new Cube(10,20,5,"Green",1.5,"Cube 1");
     n[2]=new Rectangle(70,9,"Yellow",2.1,"Rectangle B");
     n[3]=new Cube(33,5,15,"Black",0.5,"Cube 2");
     for (int i=0; i<=3; i++)
     find_Mass(n[i]);
     system("PAUSE");
 }
Topic archived. No new replies allowed.