Need a little OOP help

I am learning about Object Oriented Programming and need a little help getting this sketch to work. The book I got the example from actually has a C# .net example but I converted it to an arduino sketch. I haven't found a lot of OOP information regarding arduino, so I could uses a little help with this.

This sketch has a shape class and two other classes, circle and rectangle inherit from it. Both calls to the circle and rectangle get the same error. error: conversion from 'Circle*' to non-scalar type 'Circle' requested

Can someone tell me what is wrong with this?

class Shape
{
  protected : double area;
  public : double calcArea();
};

class Circle : Shape
{
  private : double radius;
  public : Circle(double r)
    {
      radius = r;
    }

  public : virtual double calcArea()
    {
      area = 3.14 * (radius * radius);
      return (area);
    }
};

class Rectangle : Shape
{
  private : double length;
  private : double width;

  public : Rectangle(double l, double w)
    {
      length = l;
      width = w;
    }

  public : virtual double calcArea()
    {
      area = length * width;
      return (area);
    }
};

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

void loop() {
  Circle circle = new Circle(5);
  Rectangle rectangle = new Rectangle(4, 5);

  Serial.println(circle.calcArea());
  Serial.println(rectangle.calcArea());
here:
  goto here;
}

new returns a pointer...

Allocates memory for an object or array of objects of type-name from the free store and returns a suitably typed, nonzero pointer to the object.

try like this:

void loop() 
{
  Circle* circle = new Circle(5);
  Rectangle* rectangle = new Rectangle(4, 5);
  Serial.println(circle->calcArea());
  Serial.println(rectangle->calcArea());
  while(true)
  {
    // do nothing
  }
}

FYI, people will sharply criticize your use of goto...

Yes, that fixed the problem. Thank you.

I also got rid of the goto.

If anyone knows of resources for oop syntax for arduino, I would appreciate a link to it.

You're confusing C# with C++.

change

Circle circle = new Circle(5);
Rectangle rectangle = new Rectangle(4, 5);

to

Circle circle = Circle(5);
Rectangle rectangle = Rectangle(4, 5);

PickyBiker:
Yes, that fixed the problem. Thank you.

I also got rid of the goto.

If anyone knows of resources for oop syntax for arduino, I would appreciate a link to it.

You can find what you need in any good C++ reference book that is beyond the very basics. Or just Google C++ tutorials!!!!

Yes, C# is different from C++. Changed the code as ieee488 suggested and that allowed me to use

Serial.println(circle.calcArea());

and

Serial.println(rectangle.calcArea());

PickyBiker:
Yes, C# is different from C++. Changed the code as ieee488 suggested and that allowed me to use

Serial.println(circle.calcArea());

and

Serial.println(rectangle.calcArea());

So you learned that you can create a class object that you can call.

Or, you can create a pointer to a class object and initialize that pointer to a class object. Then, use the de-reference operator "->" to access a member function dereferencing the pointer to the object.

Two ways, twice the fun.

Another kind of syntax for the latter would be:

(*ptr).method();

But... This is more handsome:

ptr->method();

:o

One of the difficult things for a c#/Java programmer to get about C is that in C#/Java, you only ever deal with references to objects, and objects are "owned" by the virtual machine.

In C, objects are put directly in other objects, directly on the stack, and can be passed around as by-value parameters.

// C++
Circle *a = new Circle(5); // by reference
Circle b(5);  // by value

In C#/Java you can only do by reference, and so C#/Java uses the simpler by-value syntax. What's even worse is that this has been back-fitted to C++: reference syntax looks the same as by-value syntax, dots instead of arrows.

Not sure where I am going with this post …

BulldogLowell:

void loop() 

{
  Circle* circle = new Circle(5);
  Rectangle* rectangle = new Rectangle(4, 5);
  Serial.println(circle->calcArea());
  Serial.println(rectangle->calcArea());
  while(true)
  {
    // do nothing
  }
}

Without the while(true), this will run out of memory very quickly. After about 50 calls to loop() :frowning:

@OP: get into the habit of deleting your objects created with the 'new' keyword; or don't use the 'new' keyword.

void loop() {
  Circle *circle = new Circle(5);
  Rectangle *rectangle = new Rectangle(4, 5);

  Serial.print("circle "); Serial.print(circle->calcArea());
  Serial.print(" rectangle "); Serial.println(rectangle->calcArea());

  delete(circle);
  delete(rectangle);
}

Or

void loop() {
  Circle circle = Circle(5);
  Rectangle rectangle = Rectangle(4, 5);

  Serial.print("circle "); Serial.print(circle.calcArea());
  Serial.print(" rectangle "); Serial.println(rectangle.calcArea());
}

sterretje:
@OP: get into the habit of deleting your objects created with the 'new' keyword; or don't use the 'new' keyword.

Yes, unless of course you want a global object pointer.

You will see this a lot in libraries out there:

Circle* circle;

void setup()
{
  circle = new Circle(params);
  circle->begin();
}

void loop()
{
  circle->someMethod();
  // etc...
}

...particularly when the constructor or begin() calls objects/functions not available from outside a function or may not be reliably called pre-setup (e.g. pinMode()).