Instantiating a class does not work, constructor parameter did not pass

I tried to initialize an circle object from my test class, but the parameter(5.5) did not pass. The result is wrong. I tried to debug and find out the radius is 0.00 in circle class, 5.5 did not pass into the Circle class.
Anyone can help me with that?

This is my output:

The area of circle is:  3.14

This is my test class:

public class ShapeTest {
        public static void main(String[] args){
            Circle circle = new Circle(5.5);
            System.out.println(circle);
            }


        }
    }

This is my circle class:

public class Circle extends TwoDimensionalshape {
        private double radius;

        public Circle(double radius){
            super(radius);
        }

    public void setRadius(double radius){
        this.radius = radius;
    }
    public double getRadius(){
        return radius;
    }

    @Override
    public double getArea(){
        return 3.14+getRadius()+getRadius();
    }

    @Override
    public String toString(){
        return String.format("%s %,.2f%n ","The area of circle is: ",getArea());
    }



}

This is my super class:

public class TwoDimensionalshape implements Area{
        private double radius;
        private double base;
        private double height;

        public TwoDimensionalshape(double radius){
            this.radius = radius;
    }

    public TwoDimensionalshape(double base, double height){
        this.base = base;
        this.height = height;
    }

    public double getRadius() {
        return radius;
    }


    public double getBase() {
        return base;
    }

    public double getHeight() {
        return height;
    }

    @Override
    public double getArea(){
        return 1;
    }

    public String toString(){
        return "The area is: "+getArea();
    }


}

Solution:

Your radius variable in Circle hides the radius variable in TwoDimensionalshape. They are 2 different variables. Your constructor sets the one in TwoDimensionalShape, but getArea is using the one in Circle.

Remove the radius variable in Circle. Let Circle inherit getRadius by removing that method from Circle. Also in Circle, move setRadius to TwoDimensionalShape.

Also, in getArea, multiply the radius twice instead of adding it twice. You can also use Math.PI instead of 3.14.

return Math.PI * getRadius() * getRadius();

Python multiple inheritance is not showing class variables or method of second inherited base class

Hello awesome community,
I was learning the OOPS concepts with python as a part of my curriculum. I am having a problem with multiple inheritance in python. The following is my code:

#!/usr/bin/env python

class Base1(object):
    def __init__(self):
        self.base1var = "this is base1"


class Base2(object):
    def __init__(self):
        self.base2var = "this is base2"

class MainClass(Base1, Base2):
    def __init__(self):
        super(MainClass, self).__init__()


if __name__ == "__main__":
    a = MainClass()
    print a.base1var
    print a.base2var

and when running, I am getting the following error

print a.base2var
AttributeError: 'MainClass' object has no attribute 'base2var'

If I swap the order of classes inherited, the variable name in the error changes accordingly.

Am I using super() wrong when I want to call the constructors of the two inherited classes?

How can I correctly inherit from multiple base classes and use the variables and methods in the main class without this error?

Thank you.

Solution:

You need to add a super call to Base1 so that Base2’s __init__ will get called after Base1’s. You can also add a super call to Base2. It’s not necessary, but it won’t hurt.

class Base1(object):
    def __init__(self):
        super(Base1, self).__init__()
        self.base1var = "this is base1"

class Base2(object):
    def __init__(self):
        #super(Base2, self).__init__()
        self.base2var = "this is base2"

class MainClass(Base1, Base2):
    def __init__(self):
        super(MainClass, self).__init__()


if __name__ == "__main__":
    a = MainClass()
    print a.base1var
    print a.base2var

output

this is base1
this is base2

BTW, you really should be using Python 3. super is much nicer in modern Python. 🙂

Is there an equivalent of the Java <? extends ClassName> in C++?

I am looking at this https://docs.oracle.com/javase/tutorial/java/generics/subtyping.html and https://docs.oracle.com/javase/tutorial/java/generics/inheritance.html and ask myself how it could be implemented in C++.

I have this small example to illustrate :

#include <iostream>

class Animal
{
public:
    virtual std::string type() const = 0;
    virtual ~Animal() {}
};

class Dog : public Animal
{
public:
    virtual std::string type() const {
        return "I am a dog";
    }
};

class Cat : public Animal
{
public:
    virtual std::string type() const {
        return "I am a cat";
    }
};

template <typename T>
class AnimalFarm
{
};

void farmTest(const AnimalFarm<Animal *> &farm)
{        
    std::cout << "test farm";
}


int main(int argc, char *argv[])
{          
    AnimalFarm<Dog *> dogFarm;
    AnimalFarm<Animal *> animalFarm;

    farmTest(animalFarm); // OK
    farmTest(dogFarm); // NOK compiler error as class AnimalFarm<Dog *> does not inherits from class AnimalFarm<Animal *>

    return 0;
}

I understand why it does not work in C++. In Java, the solution is to use the following construction :

List<? extends Integer> intList = new ArrayList<>();
List<? extends Number>  numList = intList;  // OK. List<? extends Integer> is a subtype of List<? extends Number>

(given that Integer is a subclass of Number as pointed out in the example links).

Using :

template <typename U>
void farmTest(const AnimalFarm<U *> &farm);

might be solution but is there a better way to do that in C++ without loosing the fact that Cat or Dog inherits from Animal ( as Integer is a subtype of Number) ?

Thank you.

Solution:

If your end game is to form an is-a relationship between AnimalFarm<Dog *> and AnimalFarm<Animal *>, then a bit of template specialization with type traits can do that.

template <typename T, typename = void>
class AnimalFarm // Primary template
{
};

template<typename T>
class AnimalFarm<T*,
  std::enable_if_t<
     !std::is_same<T, Animal>::value &&
      std::is_base_of<Animal, T>::value
  >
> // Specialization only instantiated when both conditions hold
  // Otherwise SFINAE
  : public AnimalFarm<Animal*>
{
};

Since AnimalFarm<Animal*> becomes a public base of AnimalFarm<Dog*>, the function parameter reference will bind to it. Though you should note that the corresponding hierarchy is flat, no matter how deep the Animal one goes.

You can check it out live.