13040 - Pyramid   

Description

Design a class named Pyramid which is derived from an existed class named Rectangle.

 

Rectangle:

Protected Variables:

  • l(int) as rectangle’s length.
  • w(int) as rectangle’s width.

note that 0 <= l, w <= 100.

Constructors:

  • The default constructor of the class should initialize l, and w to 0.
  • The parameterized constructor Rectangle(int _l, int _w) should initialize rectangle's l, and w to _l, and _w.

Methods:

  • int getLength() - Return rectangle's l.
  • int getWidth() - Return rectangle's w.
  • int calculateArea() - Return rectangle's area(l * w).

 

 

Pyramid:

Protected Variables:

  • l(int) as pyramid’s length.
  • w(int) as pyramid’s width.
  • h(int) as pyramid’s height.

note that l, w is derived from Rectangle.

Constructors:

  • The default constructor of the class should initialize pyramid’s l, w, h to 0.
  • The parameterized constructor Pyramid(int _l, int _w, int _h) should initialize pyramid’s l, w, h to _l, _w, _h.
  • The parameterized constructor Pyramid(Rectangle& rect, int _h) should initialize pyramid’s l, w, h to rect’s l, rect’s w, _h.

Methods:

  • int getLength() - Return pyramid’s l.
  • int getWidth() - Return pyramid’s w.
  • int getHeight() - Return pyramid’s h.
  • int calculateArea() - Return pyramid’s base area(l * w).
  • double calculateVolume() - Return pyramid’s volume(1/3 * base area * h).

// YOU DON’T NEED TO IMPLEMENT BELOW TWO METHODS

  • void printBasicInfo() – print Length: l, Width: w, Height: h. For example: “Length: 20, Width: 30, Height: 40”.
  • void printFullInfo() – print Length: l, Width: w, Height: h, Area: base area, Volume: volume. For example: “Length: 20, Width: 30, Height: 40, Area: 600, Volume: 8000.00”. Note that, std::fixed() and std::setpercision(int) is help to print a floating variable to 2nd decimal place.

Example:

#include <iostream> // std::cout, std::fixed
#include <iomanip> // std::setprecision
 
std::cout << std::fixed  << std::setprecision(2) << some_double_variable;

 

main.cpp

#include <iostream>
#include <iomanip>
 
using namespace std;
 
void check();
 
class Rectangle
{
public:
    Rectangle() : l(0), w(0) {}
    Rectangle(int _lint _w) : l(_l), w(_w) {}
 
    int getLength() { return l; }
    int getWidth() { return w; }
 
protected:
    int lw;
    int calculateArea() { return l * w; }
};
 
/* Todo */
// Create a class named Pyramid, derived from class Rectangle
// 1. Three constructors: Pyramid(), Pyramid(int _l, int _w, int _h), Pyramid(Rectangle& rect, int _h)
// 2. Four public methods: getHeight(), calculateVolume(), printBasicInfo(), printFullInfo()
/* You may use the below functions directly */
// void printBasicInfo() { cout << "Length: " << getLength() << ", Width: " << getWidth() << ", Height: " << getHeight() << endl; }
// void printFullInfo() { cout << "Length: " << getLength() << ", Width: " << getWidth() << ", Height: " << getHeight() << ", Area: " << calculateArea() << ", Volume: " << fixed << setprecision(2) << calculateVolume() << endl; }
// 3. One protected variable: h
 
// Don't touch below functions when submitting the code
void check()
{
    int testcase;
    cin >> testcase;
 
    if (testcase == 1)
    {
        Pyramid pyrm1 = Pyramid();
        pyrm1.printBasicInfo();
 
        Pyramid pyrm2 = Pyramid(345);
        pyrm2.printBasicInfo();
    }
 
    if (testcase == 2)
    {
        Pyramid pyrm1 = Pyramid();
        pyrm1.printFullInfo();
 
        Pyramid pyrm2 = Pyramid(131415);
        pyrm2.printFullInfo();
    }
 
    if (testcase == 3)
    {
        Rectangle rect = Rectangle(1010);
        Pyramid pyrm = Pyramid(rect30);
        pyrm.printFullInfo();
    }
 
    if (testcase == 4)
    {
        Pyramid pyrm1 = Pyramid(233455);
        pyrm1.printFullInfo();
 
        Rectangle rect = Rectangle(1111);
        Pyramid pyrm2 = Pyramid(rect, 17);
        pyrm2.printFullInfo();
    }
 
    if (testcase == 5)
    {
        Pyramid pyrm = Pyramid(100100100);
        pyrm.printFullInfo();
    }
}
 
int main()
{
    check();
    return 0;
}

Input

No need to handle input. (Input would be number 1~5 to represent the index of test-case)

Output

No need to handle output.

Sample Input  Download

Sample Output  Download

Tags




Discuss