Class - inheritance

Hi everyone. I'm dealing with classes. Here I'm doing a class "materialPoint" which inheritate the data-members of other two classes ("particle" and "position"). I'm having troubles calling the methods of the class "position". Thank you all in advance guys.
I'm leaving here all the codes end the errror message:

test.cxx:45:26: error: ‘double position::getX() const’ is inaccessible within this context
45 | cout << "A: " << A.getX() << "\t" << A.getY() << "\t" << A.getZ() << endl;
| ^
In file included from test.cxx:3:
position.h:17:9: note: declared here
17 | double getX () const ;

codes:

***position.h***
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
  #pragma once

class  position {

 public:
										//constructor
 position () ;
 position ( double , double , double ) ;
 position ( const position& ) ;
 position ( position && ) ;
 position& operator= ( const position& ) ;
 position& operator= ( position&& ) ;
 

 										//methods
 //cartesian(default)
 double getX () const ;
 double getY () const ;
 double getZ () const ;
 
 //spherical
 double getR () const ;
 double getTheta () const ;
 double getPhi () const ;
 
 //cylindrical
 double getRho () const ;
 //phi as spherical
 //z as cartesian
 
 double distance ( const position& ) ;

 
 protected:

 double m_x , m_y , m_z;
	
};


***position.cxx***
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 "position.h"

#include<cmath>

position::position() : m_x {} , m_y {} , m_z {} { }

position::position ( double x, double y, double z ) : m_x {x} , m_y {y} , m_z {z} { }

position::position ( const position& p ) : position ( p.m_x , p.m_y , p.m_z ) { }

position::position ( position&& p ) : position ( p.m_x , p.m_y , p.m_z ) {
	p.m_x = 0 ;
	p.m_y = 0 ;
	p.m_x = 0 ;
}

position& position::operator= ( const position& p)  {
	m_x = p.m_x ;
	m_y = p.m_y ;
	m_z = p.m_z ;
	
	return *this ;	
}

position& position::operator= ( position&& p)  {
	m_x = p.m_x ;
	m_y = p.m_y ;
	m_z = p.m_z ;

	p.m_x = 0 ;
	p.m_y = 0 ;
	p.m_x = 0 ;
		
	return *this ;	
}


//cartesian
double position::getX() const { return m_x ; }

double position::getY() const { return m_y ; }

double position::getZ() const { return m_z ; }

//spherical
double position::getR() const { 
	return sqrt ( m_x*m_x + m_y*m_y + m_z*m_z) ;
}

double position::getTheta() const { 
	if ( m_z == 0 && getR () == 0 ) return 0 ; 
	return acos ( m_z / getR() ) ;
}

double position::getPhi() const {
	if ( m_y == 0 && m_x == 0 ) return 0 ; 
	return atan ( m_y / m_x ) ;
}

//cylindrical
double position::getRho() const {
	return sqrt( m_x*m_x + m_y*m_y ) ; 
}


double position::distance ( const position& p ) {
	return sqrt ( pow ( getX () - p.getX () , 2 ) + pow ( getY () - p.getY () , 2 ) +
				  pow ( getZ () - p.getZ () , 2 ) ) ; 
} 


***particle.h***
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
  #pragma once 

//order of data members: charge , mass --> particle p ( q, m)

class particle {

 public:
									//constructors
 particle () ;
 particle ( double , double ) ;
 particle ( const particle& ) ;
 particle ( particle&& ) ;
 particle& operator = ( const particle& ) ;
 particle& operator = ( particle&& ) ;
 
									//methods
 double getCharge () const { return m_charge ; } ;
 double getMass () const { return m_mass ; } ;
 void print () const ;  

 protected:

 double m_charge , m_mass ;
 
};


***particle.cxx***
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
  #include "particle.h"

#include<iostream>

																		//constructor
particle::particle () : m_charge {} , m_mass {} { }

particle::particle ( double q , double m ) : m_charge { q } , m_mass { m } { }

//copy constructor
particle::particle ( const particle& P ) : particle ( P.m_charge , P.m_mass ) { }

//move constructor
particle::particle ( particle&& P ) : particle ( P.m_charge , P.m_mass ) { 
	P.m_charge = 0 ;
	P.m_mass = 0 ;
}

//copy initializer 
particle& particle::operator = ( const particle& P ) {
	m_charge = P.m_charge ;
	m_mass = P.m_mass ;

	return *this ;
}

//move initializer 
particle& particle::operator = ( particle&& P ) {
	m_charge = P.m_charge ;
	m_mass = P.m_mass ;

	P.m_charge = 0 ;
	P.m_mass = 0 ;

	return *this ;	
}

																	//methods
void particle::print () const {
	std::cout << "Particle of charge: " << m_charge << " and mass: " << m_mass << std::endl;
}



***materialPoint.h***
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  #pragma once

#include "position.h"
#include "particle.h"
#include "vectorField.h"

#include<iostream>
#include<cmath> 

class materialPoint : public particle, position {

 public:

 materialPoint () ;
 materialPoint ( double charge , double mass , const position& ) ;
 materialPoint ( double charge , double mass , double x , double y , double z ) ;

// vectorField ElectricField ( const position& ) const ;
// vectorField Gravitational_Field ( const position& ) const ;
 
};


***materialPoint.cxx***
1
2
3
4
5
6
7
8
9
10
11
12
13
  #include "materialPoint.h" 

//vacuum permittivity 8.854188E-12
//G constant		  6.67408E-11


materialPoint::materialPoint () : particle () , position () { }

materialPoint::materialPoint ( double q, double m, const position& p ) : particle ( q, m ) , position ( p ) { }

materialPoint::materialPoint ( double q, double m, double x, double y, double z ) : particle ( q, m ) , position ( x , y , z ) { }



***test.cxx***
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
#include "electron.h"
#include "particle.h"
#include "position.h"
#include "vectorField.h"
#include "materialPoint.h" 

#include<iostream>
#include<cstdlib>

using namespace std;

int main ( int argc , const char** argv ) {
	if ( argc != 4 ) {
		cout << "insert <nameprogram> <x> <y> <z>\n" ;
		exit (-1) ;
	}
	
	double x { atof ( argv[1] ) } ;
	double y { atof ( argv[2] ) } ;
	double z { atof ( argv[3] ) } ;

	position P ( x, y, z ) ;

	electron e ;
	particle n ( 1.2 , 2. ) ;

	materialPoint A ( 1.2 , 2. , P ) ;
	materialPoint B ( 1. , 2. , 3. , 4. , 5. ) ;

	//verify get-methods
	cout << "electron get\n" ;
	cout << e.getCharge () << "\t" << e.getMass() << endl;

	cout << "particle get\n" ;
	cout << n.getCharge () << "\t" << n.getMass() << endl;

	cout << "position get\n" ;
	cout << P.getX() << "\t" << P.getY() << "\t" << P.getZ() << endl ;

	cout << "***materialPoint get***\n" ;
	cout << "from partilce.h\n";
	cout << "A: " << A.getCharge() << "\t" << A.getMass() <<  endl;
	cout << "B: " << B.getCharge() << "\t" << B.getMass() <<  endl;
	cout << "from position.h\n" ;
	cout << "A: " << A.getX() << "\t" << A.getY() << "\t" << A.getZ() << endl;
	

	return 0;
 	 
}
Last edited on
I'm leaving here all the codes


No you aren't. You haven't included test.cxx
Which is where the error is reported to come from!
Last edited on
sorry, I'll edit the post (anyway I saved the main in a file test.cxx, sorry again)
Strictly your full error (ignore the line numbers - I dumped the code into a single file to make it workable) is
74:8: error: 'double position::getX() const' is inaccessible
225:26: error: within this context
225:26: error: 'position' is not an accessible base of 'materialPoint'


Make the materialPoint class declaration
class materialPoint : public particle, public position {
You need to deal with access to each base class separately.

Does it really make sense to use inheritance here?



Apropos of nothing your routine position::getPhi() will fail with the completely legitimate input m_x=0. Use atan2, not atan, to prevent division by 0.
Last edited on
Well, I already created the classes particle and position before. I could surely write the class materialPoint without inheritance, but I thaught it would be nice to use my older classes and also practice inheritance.
Anyway, thanks for the help!
Zhylian wrote:
I thaught it would be nice to use my older classes and also practice inheritance


There's nothing wrong with practising inheritance, but don't end up learning bad design in the process. Learning *when* to use inheritance is as important as learning *how* to do it.

Inheritance is (mostly) for modelling an "is-a" relationship, so that the derived class has the interface and properties of the base class. For example, a cat IS AN animal - it has all the externally visible behaviour of an animal, and has all the properties of an animal. Similarly, a car IS A vehicle, a square IS A shape, etc.

Is it true that a materialPoint IS A position? Or does it HAVE A position?

Is it true that a materialPoint IS A particle? Or does it HAE A particle?

You can certainly make use of your older classes - code reuse is something to be aimed for wherever helpful - but the correct relationship here might be composition, rather than inheritance.
Topic archived. No new replies allowed.