Notifications
Clear all

[Closed] Object Oriented techniques - Example 1 - "Hello Class"

302 Posts
10 Users
229 Likes
20.1 K Views
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 7006
 

@will Ok, then what the heck is that statement doing then?

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
Biny reacted
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2535
 
Posted by: @zander

@will Like I said, it's been 20 years.

I know, I'm just easing you into the right mindset 🙂

Anything seems possible when you don't know what you're talking about.


   
Biny reacted
frogandtoad
(@frogandtoad)
Member
Joined: 5 years ago
Posts: 1458
 

@zander

Posted by: @zander

@frogandtoad Ok, I think I get most of that, but the syntax is 100% unfamiliar to me. What I mean is gpioPin appears to be an integer when used in statements like digitalWrite but when I see gpioPin(PinNumber) I see an array of PinNumber entries. Sorry if I am totally missing something but this is 100% new to me. I have seen enough here to think I might be able to use this condtruct in a productive way if I can understand it. Thanks for your patience.

class MicroController {
   public:
     MicroController() : ledPin(13), servoPin(6) 
      {
       // ...
      }

  int ledPin;
  int servoPin;
 };

MicroController Uno;

void setup() {
  Serial.begin(9600);

  Serial.println(Uno.ledPin);
  Serial.println(Uno.servoPin);
 }

void loop() {
  // ...
 }

It's really quite simple... I've cut out all the guts, and hard coded the initializer list and made everything public for ease of understanding.  Now we don't pass any arguments upon instantiation, so no constructor parameters to use for initialization - all hard coded.

Does this clear it up?

Cheers


   
Biny and Ron reacted
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2535
 
Posted by: @zander

@will Ok, then what the heck is that statement doing then?

It's more like a function (using round brackets) that assigns the value in brackets to the class member.

This is why I oppose this forums a platform for teaching OOP - it's simply not the right environment because it confuses all the non-programmers. You won't really need to look at C++ unless you want to write a class.

Anything seems possible when you don't know what you're talking about.


   
Biny reacted
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 7006
 

@will When you are used to using a proper high level language C is a bit of a pain at times so I might be a little biased against it even though I am more or less forced to use it nowadays, but at least so far it is pretty generic stuff, I wasn't even aware it was C when I first started looking at sample arduino sketches, I thought sketch was the language. LOL

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
Biny reacted
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2535
 
Posted by: @zander

@will When you are used to using a proper high level language C is a bit of a pain at times so I might be a little biased against it even though I am more or less forced to use it nowadays, but at least so far it is pretty generic stuff, I wasn't even aware it was C when I first started looking at sample arduino sketches, I thought sketch was the language. LOL

You're not even seeing the heavy side of C, it was designed to scrape the bare metal of the hardware and has some truly frightening operations on memory.

Since we're generally writing C functions and subroutines as add-ons we normally have relatively uncluttered names. Only when you need to access a C++ method do you need to start using compounds. Serial.print(something) is an example where we're telling the object Serial that we want it to do whatever its method print() does with the something we set it.

Inside the C++ class, there will be more than one definition of print, one for integers, one for real numbers, one for strings and so on. the class will check the argument type of the "something" we passed in and pass that on to the method best suited to handle the argument type.

Anything seems possible when you don't know what you're talking about.


   
Biny and Ron reacted
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 7006
 

@frogandtoad I think so, if I may indulge you a bit more, it's the equivalent to saying 'int ledPin = 13;' in non OOP C.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
Biny reacted
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 7006
 

@will I made the 'mistake' of googling class constructor and saw that they have invented a lot more since I looked at it in the early to mid 80's. I never used it in a job and spent only a few hours 'playing' with it in hobby mode. In fact when Borland Turbo went full on into OOP, I lost interest in their products and I had been the Toronto President of the Turbo Pascal SIG of the TO PC Club.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
Biny reacted
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2535
 
Posted by: @zander

@will I made the 'mistake' of googling class constructor and saw that they have invented a lot more since I looked at it in the early to mid 80's.

Yes, and think how productive you've been using the Arduino and ESPs without ever having seen any of that (quite a lot). And then consider how much extra value you'll get out of your software by having seen it (not a sausage) 🙂

Anything seems possible when you don't know what you're talking about.


   
Biny and Ron reacted
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 7006
 

@will That last part is a handy feature, and oft quoted as a benefit of OOP but it is a trivial exercise in regular code to do 'almost' the same thing. Remember I said 'almost'.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
Biny reacted
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 7006
 

@will At the moment at least I don't see any application of classes in the projects I am planning. The majority of what I want to do with the Pi's, arduino's, esp's is centered on camera and WiFi/web access, and of course various sensors.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
Biny reacted
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2535
 

@zander 

Exactly, and the classes (libraries) you'll need have already been written by somebody else !

Anything seems possible when you don't know what you're talking about.


   
Biny and Ron reacted
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 7006
 

@will YES,, and I am grateful for that. As you might have been able to tell so far, I prefer to write my own code including library code as it's called in this environment. In all my past experience, they were just called modules. At IBM we did have modules that everybody had to use, for example I wrote the module that formatted a report header. All the report header elements had to be printed in the same location and each report had a name assigned that was recorded in a database by the admin department in charge of 'all knowing' 😉

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
Biny reacted
frogandtoad
(@frogandtoad)
Member
Joined: 5 years ago
Posts: 1458
 

@zander 

Posted by: @zander

@frogandtoad I think so, if I may indulge you a bit more, it's the equivalent to saying 'int ledPin = 13;' in non OOP C.

If you mean by doing that in the body (scope) of the constructor, then yes.

Cheers


   
Biny reacted
frogandtoad
(@frogandtoad)
Member
Joined: 5 years ago
Posts: 1458
 

@zander

Posted by: @zander

@frogandtoad Ok, I think I get most of that, but the syntax is 100% unfamiliar to me. What I mean is gpioPin appears to be an integer when used in statements like digitalWrite but when I see gpioPin(PinNumber) I see an array of PinNumber entries. Sorry if I am totally missing something but this is 100% new to me. I have seen enough here to think I might be able to use this condtruct in a productive way if I can understand it. Thanks for your patience.

No problem, and you're welcome!

Once you understand it, you won't ever look at procedural code again 😉


   
Biny reacted
Page 9 / 21