Question to classes in c

Hi,

I am trying to understand the following instantiation of bno, in the main routine of Arduino.

Adafruit_BNO055  bno = Adafruit_BNO055();

further down bno is then used.

bno.setExtCrystalUse(true);

In the pertaining Adafruit_BNO055h. file, the class Adafruit_BNO055 is created. With Class Adfruit_BNO055_Sensor being the base class, defined in an other .h file

class Adafruit_BNO055 : public Adafruit_Sensor {
public:

And the constructor is created in the Adafruit_BNO055.cpp

Adafruit_BNO055::Adafruit_BNO055(int32_t sensorID, uint8_t address,
                                 TwoWire *theWire) {

What I dont understand, is: shouldnt it read
Adafruit_BNO055  bno;  to create an instance of the class? The book says: Type Adafruit_BNO055, the class name, and the variable, bno, in this case.

Why is the constructor there? reading Adafruit_BNO055  bno = Adafruit_BNO055();

there are no classes in C.
you can initialize a variable both described ways in C++

http://www.cplusplus.com/doc/tutorial/variables/#initialization

Hi, thanks, that was actually pretty basic, sorry for rather stupid question

So bno is initialized with

Adafruit_BNO055 bno = Adafruit_BNO055();

Whats actually in () ?

The constructor in library Adafruit_BNO055.cpp reads:

Adafruit_BNO055::Adafruit_BNO055(int32_t sensorID, uint8_t address,
TwoWire *theWire) {....

In another code sample, incorperating the BNO055 I found this:

Adafruit_BNO055 bno = Adafruit_BNO055(55, BNO055_ADDRESS);

What is 55, and BNO055_ADDRESS ?

I couldn`t find any in any library, in the library I find BNO055_ADDRESS_A, or BNO055_ADDRESS_B

And, also the Constructor in the library has 3 variables, int32_t sensorID, uint8_t address,
TwoWire *theWire) {

where is (55, BNO055_ADDRESS) comming from? meaning what is bno being initialised with?

probably also basic stuff, but I am stuck here.

appreciate any help.

The constructor in library Adafruit_BNO055.cpp reads:

What about Adafruit_BNO055.h?
That's far more important, because your sketch knows nothing about Adafruit_BNO055.cpp

You can have several constructors, depending on the number of arguments. So

Adafruit_BNO055  bno = Adafruit_BNO055();

will call a different constructor, which you haven't found yet.

The .h file that declares the classes and methods may also contain the implementations for the simpler methods. A constructor with no arguments may just call the "real" constructor by passing it the defaults for each of those arguments. That's a 1-line function so it may be implemented in the .h file.

Juraj:
there are no classes in C.
you can initialize a variable both described ways in C++

http://www.cplusplus.com/doc/tutorial/variables/#initialization

Arduino uses C++, so this is a pedantic distinction.

johnguy:
What I dont understand, is: shouldnt it read

Adafruit_BNO055  bno;

to create an instance of the class? The book says: Type Adafruit_BNO055, the class name, and the variable, bno, in this case.

Why is the constructor there? reading

Adafruit_BNO055  bno = Adafruit_BNO055();

Both of those methods of construction will give the same result. When you don't use an explicit constructor, the compiler will select the default constructor (the one that needs no arguments). If you don't have a default constructor, you'll get an error trying to use that first snippet.

The .cpp file contains the implementation code of the class, but the .h file contains its definition. It's just as important as the .cpp file, especially in this case. You are correct to notice that the constructor in the .cpp requires arguments. What you miss is this line in the .h file:

  Adafruit_BNO055(int32_t sensorID = -1, uint8_t address = BNO055_ADDRESS_A,
                  TwoWire *theWire = &Wire);

The .h file defines default values that will automatically be assigned to those arguments if you choose to not specify them. In this case, all the arguments have default values, so you don't need to specify them if the defaults are what you need.

The constant BNO055_ADDRESS_A is also defined in the .h file.