Derivative class does not work.

Hi all, I'm trying to write a little program that makes use of derived classes but it doesn't work and I don't know why. It's not very meaningful, it just does some monitor printing. Just to figure out how to connect the various .h and .cpp files.
Can you help me? Thanks

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
ifndef ROBOT_H_INCLUDED
#define ROBOT_H_INCLUDED

class Robot{  // Class base
public:
void MoveForward(); //Procedure
};

#endif // ROBOT_H_INCLUDED

************************************
#include <iostream>  
#include "Robot.h"

using namespace std;

void Robot::MoveForward()
{
    cout << "MoveForward" << endl;
}
*************************************
#ifndef SENSOR_H_INCLUDED
#define SENSOR_H_INCLUDED


class Sensor : public Robot //Sensor is derived from Robot
{
public:
    void MPU6050(); //Procedure
    Robot.MoveForward(); //Class base ERROR!
};
#endif // SENSOR_H_INCLUDED

**********************************
#include <iostream>
#include "Sensor.h"

using namespace std;

void Sensor::MPU6050()
{
    cout << "MPU6050" << endl;
}
***********************************

#include <iostream>
#include "Robot.h"
#include "Sensor.h"
using namespace std;

int main()
{
    Sensor s;
    s.MPU6050();
//  s.MoveForward(); Shouldn't he inherit from Robot? He won't let me.
    return 0;
}
Last edited on
Remove line 30.
Uncomment line 55. (But comment the English text on that line)

This program is tiny. Except for the purposes of teaching, there's not a pressing need to split everything up into files.
Last edited on
1
2
3
4
5
6
class Sensor : public Robot //Sensor is derived from Robot
{
public:
    void MPU6050(); //Procedure
    Robot.MoveForward(); //Class base ERROR!
};


That line in bold. It makes no sense at all.

Here is working code:
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
#include <iostream>

using namespace std;

class Robot{  // Class base
public:
void MoveForward(); //Procedure
};

void Robot::MoveForward()
{
    std::cout << "MoveForward" << endl;
}

class Sensor : public Robot //Sensor is derived from Robot
{
public:
    void MPU6050(); //Procedure
};

void Sensor::MPU6050()
{
    cout << "MPU6050" << endl;
}

int main()
{
    Sensor s;
    s.MPU6050();
    s.MoveForward();
    return 0;
}
> Sensor is derived from Robot
that makes no sense.

1
2
    Sensor s;
    s.MoveForward();

that makes no sense.
dictionary wrote:
sensor
<hardware> An electronic device used to measure a physical quantity such as temperature, pressure or loudness and convert it into an electronic signal of some kind (e.g a voltage).
Some sensor units encapsulate/report/modify their position and direction independent of the robot they might be mounted on.
Public inheritance models specialization. As such, publicly deriving Sensor from Robot indicates that a Sensor is a specialized kind of Robot, which is probably nonsense in a typical model

Instead it is more likely that a Robot is associated with or contains one or more Sensors.
Last edited on
Yawn!
Thank you! You're right, the program is tiny because I have to learn. Splitting the file into several files was how I needed to figure out how to connect them. In the future I could have many classes each with its own .h and a .cpp.I wrote this example program that doesn't make much sense, of course, just to understand how to connect the various files because around the network I have not found anything like that.
Thanks again and Happy New Year! :)
pische
:)
Topic archived. No new replies allowed.