Classes

So i have to make a function in the Class Sfera in which i calculate the volume of the Sphere and in main function after writing sfr.volum()it should show the answer, i really dont have an ideea how to write the volume function in the class, can you help me? the radius is 4.1 [ Sfera sfr(pct, 4.1)]


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
 #include <iostream>

using namespace std;

class Punct3D
{
public:

	Punct3D(double x = 0, double y = 0, double z = 0) {
		setX(x);
		setY(y);
		setZ(z);
	}
	double getX() { return x; }
	void setX(double x) { this->x = x; }
	double getY() { return y; }
	void setY(double y) { this->y = y; }
	double getZ() { return z; }
	void setZ(double z) { this->z = z; }

	friend ostream& operator<<(ostream& o, Punct3D& p);

private:
	double x;
	double y;
	double z;
};


ostream& operator<<(ostream& o, Punct3D& p)
{
	Punct3D pct;
	o <<"["<< p.getX() << ";" << p.getY() << ";" << p.getZ() << "]";
	return o;
}

class Sfera
{
public:
    Punct3D c;
    Sfera(Punct3D, double r=0){
    setR(r);
    }
    double getR() { return r; }
	void setR(double r) { this->r = r; }
	

private:
 double r;
};

ostream& operator<<(ostream& o, Sfera& s)
{
Punct3D pct(-1.1, 2.2, 3.3);
 Sfera sfr();
 o <<"[" << pct <<";"<< s.getR() << "]";
 return o;

}

int main() {
	Punct3D pct(-1.1, 2.2, 3.3);
    Sfera sfr(pct, 4.1);
	cout << "Punct3D: " << pct << endl
	     << "Sfera: " << sfr << endl
         << "Volumul: " << sfr.volum() << endl;
	return 0;
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Sfera
{
public:
    Punct3D c;
    Sfera(Punct3D, double r=0){
    setR(r);
    }
    double getR() { return r; }
	void setR(double r) { this->r = r; }

    double volum(){return ( 4.0 * 3.14159 * r * r * r / 3.0);
	

private:
 double r;
};
it doesnt work, it just cracks the code more. :(
it doesnt work, it just cracks the code more. :(

It works smoothly. You could have taken care of adding the missing closing curly parenthesis by yourself.

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
93
94
95
#include <iostream>


class Punct3D {
public:
    Punct3D( double = 0.0, double = 0.0, double = 0.0 );
    double getX() const;
    void setX(double);
    double getY() const;
    void setY(double);
    double getZ() const;
    void setZ(double);

private:
    double x;
    double y;
    double z;

//friend:
    friend std::ostream& operator<< ( std::ostream&, const Punct3D& );
};


Punct3D::Punct3D( double x_arg, double y_arg, double z_arg )
    : x { x_arg }
    , y { y_arg }
    , z { z_arg }
{
}


double Punct3D::getX() const { return x; }
void Punct3D::setX( double x_arg ) { x = x_arg; }
double Punct3D::getY() const { return y; }
void Punct3D::setY( double y_arg ) { y = y_arg; }
double Punct3D::getZ() const { return z; }
void Punct3D::setZ( double z_arg ) { z = z_arg; }


std::ostream& operator<< (std::ostream& o, const Punct3D& rhs)
{
    o << '{' << rhs.x << "; " << rhs.y << "; " << rhs.z << '}';
    return o;
}


class Sfera {
public:
    Punct3D center;
    Sfera(Punct3D = {}, double = 0.0);
    double getR() const;
    void setR(double);

    double volume() const;

private:
    double radius;
};


Sfera::Sfera(Punct3D center_arg, double radius_arg)
    : center { center_arg }
    , radius { radius_arg }
{
}


double Sfera::getR() const { return radius; }
void Sfera::setR(double radius_arg) { radius = radius_arg; }


// Thanks to Repeater:
double Sfera::volume() const
{
    return ( 4.0 * 3.14159 * radius * radius * radius / 3.0);
}


std::ostream& operator<< (std::ostream& o, const Sfera& s)
{
    o << "\n    center: " << s.center
      << "\n    radius: " << s.getR()
      << "\n    volume: " << s.volume();
    return o;
}


int main()
{
    Punct3D pct( -1.1, 2.2, 3.3 );
    Sfera sfr(pct, 4.1);
    std::cout << "Punct3D: " << pct << '\n'
              << "Sfera: " << sfr << '\n';
    return 0;
}


Output:
Punct3D: {-1.1; 2.2; 3.3}
Sfera:
    center: {-1.1; 2.2; 3.3}
    radius: 4.1
    volume: 288.695


Delete your other thread:
http://www.cplusplus.com/forum/beginner/269061/
Topic archived. No new replies allowed.