Need To Multiply An Object From A Different Class With Object In Same c++

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
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <iostream>
#include <cstring>
#include <cmath>
using namespace std;
#define PI 3.14159

class values{
      
public:
double sideA;      //a= length of side
double sideBR;    //b = breadth
double sideB;    //b = base
double sideH;   //h = height
double sideVH; //vh = vertical height
double sideR; //r = radius of a circle
};



class twoDobjects: public values{ //some values that are obtained via Pythagora's Theorem included
      
public:    
            
double sideC () // C is in fact a hypotenuse of a triangle  
{
       return sqrt ((sideR*sideR)+(sideVH*sideVH));
}      
            
            
                  
double squarearea ()
{
       return sideA*sideA;
}


double trianglearea ()
{
       return (sideB*sideVH)/2;
}


double circlearea ()
{
       return PI*(sideR*sideR);
}



};




class threeDobjects: public twoDobjects{

public:
double cubearea ()
{
       return squarearea()*6;
}

double conearea ()
{
       return circlearea () + PI * sideR * sideC ();
}




};




//
int main ()
{
twoDobjects twoD;
threeDobjects threeD;
int a;
cout << "Insert the side of the  " <<endl;
cin>>a;
twoD.sideA=a;    

cout << "The area of the cube is: " << twoD.squarearea ()<<endl;

cout << " The volume of the cube: " << threeD.cubearea () <<endl;

system ("pause");
return 0;
}


i need to make a program with 3 classes, and need to find the volume and the area of a Cube, Pyramid and of a Cone. me and my friend cannot move any further... because the third class wont work ... :( anyone can help ???
I'm confused, because from your description at the bottom of your post, it sounds like you are expected to write

1
2
3
4
5
6
7
8
9
10
11
class Cube { 
  // etc
};

class Pyramid {
  // etc
};

class Cone {
  // etc
};


But your code looks like you have one mega object (twoDobjects) that tries to be a square, circle, and triangle all at the same time.

actually there should be 2 classes only
one for 2d objects
and one for 3d
the class for 3d objects should use values obtained from the class that calculates 2d objects in order to calculate things such as area of a cube or etc
Last edited on
What exactly isn't working on the third class? Seems like you're trying to do too much inside your classes to be honest. I'd probably do it more like

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
enum SHAPE { SQUARE, TRIANGLE, CIRCLE, PYRAMID, CONE, CUBE };

class Shape2D
{
     private:
          int width;
          int height;

          // it's probably better to create a derived class for each shape
          // but since you're limited to 2 classes you could do it this way.
          SHAPE typeOfShape;

     public:
          Shape2D(SHAPE initShapeType);    // constructor
          // add methods you want like computeArea() or something
};

class Shape3D : Shape2D
{
     private:
          int thickness;      // new attribute for all 3D objects
     public:
         // add methods you want computeVolume() etc.
};


That's just my opinion though

Last edited on
we've found a different solution... wrote diff classes and insertet them in main :D
it's finished (a little large, but finished)
Topic archived. No new replies allowed.