Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Description In this assignment, you will be using inheritance and polymorphism t

ID: 3800097 • Letter: D

Question

Description In this assignment, you will be using inheritance and polymorphism to implement a similar s to the ape example in the lecture. For this, you will need to design and write a base e and three derived classes that inherit from the base (similar to the shape class and the derived shape base asses). You will also need a "manager" abject that will hold a (fixed) array of pointers to your type (similar to the drawing class) 2. The base class or may have any member variables itself, depending on your design. The may not derived classes should have at least one each describe the structure of that class, member to There should be at least one virtual functian in the base class that each derived class will override The base class' virtual function should be pure virtual (a.k.a. abstract) unless the base does indeed have an implementation. Each derived type's override the virtual function should cause some of output to std-cout to indicate that it was invoked 4. Each class should inelude a full set of constructors to initialize their member(s) as well as the base class. Each derived class should call the base class' constructor in their initialization lists if needed 5. Your manager class will have an array of pointers to the base type that it manages and a method to add objects (by pointer-to-base) into the array and track how many items have been added 6. Include a function in the manager class that will loop through all of the objects in the array and invoke the virtual function for each element Gust like drawing: draw looped through all of its shapes) In your main function, instantiate a "manager object 8. Add several objects of your types to the manager object. These objects will have to be created dynamically (with new) and passed into the function you made in (5) that will add the objects to the 9. Call the manager object's function from (6) that will loop through all objects you just added. 10. Make sure that the manager's constructor cleans up all of its objects when the manager object goes out of scope 11. Place all classes in their own headers (protecting against multiple inclusion) and in a namespace of your own choosing. Please turn in all source (and nothing but the source) on Blackboard.

Explanation / Answer

1. There could be base class as "Shape". We can define 2 abstract method 'area' and 'perimeter'
   2. Then we can derive 3 sub classes from it
       1.RECTANGLE
       2.CIRCLE
       3.TRIANGLE

       each sub class would have there own implementstion for both the method
   Example :
   public abstract class Shape {
       public abstract double area();
       public abstract double perimeter();
   }

   //Rectangle subclass
   public class Rectangle extends Shape {
private final double w, l; //sides

public Rectangle() {
this(5,5);
}
public Rectangle(double w, double w) {
this.w = w;
this.l = l;
}

@Override
public double area() {
// A = w * l
return w * l;
}

@Override
public double perimeter() {
// P = 2(w + l)
return 2 * (w + l);
}

   }        

//subclass Circle
   public class Circle extends Shape {
private final double r;
final double pi = Math.PI;

public Circle() {
this(1);
}   
public Circle(double r) {
this.r = r;
}

@Override
public double area() {
// A = r^2
return pi * Math.pow(r, 2);
}

public double perimeter() {
// P = 2r
return 2 * pi * r;
}
   }

  
   public class Triangle extends Shape {
private final double a, b, c; // sides

public Triangle() {
this(1,1,1);
}
public Triangle(double a, double b, double c) {
this.a = a;
this.b = b;
this.c = c;
}

@Override
public double area() {
// Heron's formula:
double s = (a + b + c) / 2;
return Math.sqrt(s * (s - a) * (s - b) * (s - c));
}

@Override
public double perimeter() {
// P = a + b + c
return a + b + c;
}
}