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

How do you acomplish this assignment? Assignment Inheritance CSIS-1410 Learning

ID: 3885166 • Letter: H

Question

How do you acomplish this assignment?

Assignment Inheritance CSIS-1410 Learning Objectives Declare a subclass that derives from a superclass Demonstrate polymorphic behavior Declare a variable of the superclass type and assign it an instance of the subclass type Access the public members of the superclass type Notice how the overridden versions of the subclass type are called Notice how the subclass specific members are inaccessible Create an array of superclass type and use a foreach loop to iterate through it Style /Comments: On this and all upcoming assignments pay attention to style. Follow Java naming convention, use proper indentation, group related code statements with single empty lines, use descriptive names etc. Strive to write code that is clear and easy to read Also: use doc comments on all your code Description Write a program to demonstrate the use of inheritance and polymorphism You will create 4 classes: Rectangle, Square, IsoscelesRightTriangle, and Circle In addition you will create a class called InheritanceApp. This class includes the main method. Here we test the four other classes and we demonstrate the polymorphic behavior Declare the classes as described below: Class Retangle . Rectangle has 2 private final fields of type int: length and width . It has (exactly) one parameterized constructor that initializes both fields It provides a getter (get accessor method) for each of the fields (no setter) It overrides the toString method so that it produces a result of the form Rectangle (lengthxwidth) e.g. Rectangle (5x4) Class Square: . Square extends Rectangle . It has a parameterized constructor with (only) one parameter * It has a method called getSide to expose the side-length of the square No fields are declared in class Square The parameter is used to initialize both fields of Rectangle Override the toString method so that it will return a String of the following form: Square (side) e.g. Square (4)

Explanation / Answer

#include <iostream>
using namespace std;

class Shape {
protected:
int width, height;
  
public:
Shape( int a = 0, int b = 0){
width = a;
height = b;
}
int area() {
cout << "Parent class area :" <<endl;
return 0;
}
};
class Rectangle: public Shape {
public:
Rectangle( int a = 0, int b = 0):Shape(a, b) { }
  
int area () {
cout << "Rectangle class area :" <<endl;
return (width * height);
}
};

class Triangle: public Shape {
public:
Triangle( int a = 0, int b = 0):Shape(a, b) { }
  
int area () {
cout << "Triangle class area :" <<endl;
return (width * height / 2);
}
};

// Main function for the program
int main() {
Shape *shape;
Rectangle rec(10,7);
Triangle tri(10,5);

// store the address of Rectangle
shape = &rec;

// call rectangle area.
shape->area();

// store the address of Triangle
shape = &tri;

// call triangle area.
shape->area();

return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote