Question

I am having difficulty with completing this task. thank you. Task 10.1 (a) Define a C++...

I am having difficulty with completing this task.

thank you.

Task 10.1 (a) Define a C++ base class named Rectangle containing length and width data members. From this class, derive a class named Box with another data member named depth. The member functions for the base class Rectangle should consist of a constructor and an area() function. The derived class Box should have a constructor, a volume() function and an override function named area() that returns the surface area of the box.

(b) Include the classes written in part a, above in a working C++ program that creates an object for each class and calls each member function for each class to test them. Verify the results manually

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>

using namespace std;

// Creating an Rectangle class(Base class)
class Rectangle
{
private:
// Declaring instance variables
double length;
double width;

public:
// Zero argumented constructor
Rectangle()
{
}
// Parameterized constructor
Rectangle(double len, double width)
{
this->length = len;
this->width = width;
}

// getters and setters
void setLength(double len)
{
this->length = len;
}
void setWidth(double width)
{
this->width = width;
}
double getLength()
{
return length;
}
double getWidth()
{
return width;
}

// Function which calculates the area of the Rectangle
double area()
{
return length * width;
}
};

// Creating an Box class(deriving from Rectangle class)
class Box : public Rectangle
{
private:
// Declaring varibles
double depth;

public:
// parameterized constructor
Box(double length, double width, double depth)
: Rectangle(length, width)
{
this->depth = depth;
}

// This function will calculates the volume of the Box
double volume()
{
return getLength() * getWidth() * depth;
}

// This function will calculates the area of the Box
double area()
{
return 2 * (getLength() * depth + getWidth() * depth + getLength() * getWidth());
}
};
int main()
{

// Declaring variables
double len, wid, depth;

// Getting the input entered by the user
cout << "Enter length :";
cin >> len;

cout << "Enter Width :";
cin >> wid;

cout << "Enter depth :";
cin >> depth;

/* Creating an Rectangle class Object by passing
* length and width as parameters
*/
Rectangle r(len, wid);
// Displaying the area of Rectangle as output
cout << "Area of the Rectangle is :" << r.area() << endl;

/* Creating an Box class Object by passing
* length , width and depth as parameters
*/
Box b(len, wid, depth);

// Displaying the area and volume of box as output
cout << "Area of the Box is :" << b.area() << endl;
cout << "Volume of the Box is :" << b.volume() << endl;


return 0;
}

__________________

Output:

_____________Could you rate me well.Plz .Thank You

Add a comment
Know the answer?
Add Answer to:
I am having difficulty with completing this task. thank you. Task 10.1 (a) Define a C++...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Code the following Program in C++ Define a class named Payment that contains a member variable...

    Code the following Program in C++ Define a class named Payment that contains a member variable of type float that stores the amount of the payment and appropriate accessor and mutator methods.    Also create a member function named paymentDetails that outputs an English sentence that describes the amount of the payment. Next define a class named CashPayment that is derived from Payment. This class should redefine the paymentDetails function to indicate that the payment is in cash. Include appropriate constructor(s)....

  • Construct a C++ class named Rectangle that has floating-point data members named length and width.  The class...

    Construct a C++ class named Rectangle that has floating-point data members named length and width.  The class should have a zero-argument constructor that initializes each data member to 0. It should have member functions named calcPerimeter() and calcArea() that calculate the perimeter and area of a rectangle respectively, a member function setLength() and setWidth() to set the length and width, member functions getLength() and getWidth() to return the length and width, and a member function showData() that displays the rectangle’s length,...

  • a. Write a constructor which initializes the canvas_frame, canvas, and title. The constructor receives a string...

    a. Write a constructor which initializes the canvas_frame, canvas, and title. The constructor receives a string parameter to initialize the title, a Rectangle object to pass to the Rectangle constructor when creating the canvas_frame object, and a Rectangle object to pass to the Rectangle constructor when creating the canvas object. b. Write a print method that prints the title and prints the perimeter of canvas_frame and the area of canvas - call the methods area and perimeter to get both...

  • Pure Abstract Base Class Project. Define a class called BasicShape which will be a pure abstract class. The clas...

    Pure Abstract Base Class Project. Define a class called BasicShape which will be a pure abstract class. The class will have one protected data member that will be a double called area. It will provide a function called getArea which should return the value of the data member area. It will also provide a function called calcArea which must be a pure virtual function. Define a class called Circle. It should be a derived class of the BasicShape class. This...

  • .Your solution must include header, implementation file, and test files .In C++ write a code to...

    .Your solution must include header, implementation file, and test files .In C++ write a code to Consider a graphics system that has classes for various figures rectangles, squares, triangles, circles, and so on. For example, a rectangle might have data members for Height, Width and center point, while a square and circle might have only a center point and an edge length or radius. In a well-designed system, these would be derived from a common class, Figure. You are to...

  • Question 1) Consider a class Point that models a 2-D point with x and y coordinates. Define the c...

    C++ Question 1) Consider a class Point that models a 2-D point with x and y coordinates. Define the class point that should have the following Private data members x and y (of type int), with default values of 0 A constant ID of type int A private static integer data member named numOfPoints This data member should be o Incremented whenever a new point object is created. o Decremented whenever a point object is destructed. A default constructor An...

  • C++ code For each of the following classes create default constructor and parameterized constructors(calling parent(s) parameterized...

    C++ code For each of the following classes create default constructor and parameterized constructors(calling parent(s) parameterized constructors where it applies Add accessor and mutator function for the attribute inherent to the class Create a Base class called Shape2d with the protected floating point attribute area operator overload the + & - and operations to return the float respective to the area Derive from the Base class from called Shape2d called Rectangle with the additional floating-point attributes length & width Derive...

  • C++ Could you check my code, it work but professor said that there are some mistakes(check...

    C++ Could you check my code, it work but professor said that there are some mistakes(check virtual functions, prin() and others, according assignment) . Assignment: My code: #include<iostream> #include<string> using namespace std; class BasicShape { //Abstract base class protected: double area; private:    string name; public: BasicShape(double a, string n) { area=a; name=n; } void virtual calcArea()=0;//Pure Virtual Function virtual void print() { cout<<"Area of "<<getName()<<" is: "<<area<<"\n"; } string getName(){    return name; } }; class Circle : public...

  • its about in C++ You will implement a simple calendar application The implementation should include a class named Calendar, an abstract class named Event and two concrete classes named Task an...

    its about in C++ You will implement a simple calendar application The implementation should include a class named Calendar, an abstract class named Event and two concrete classes named Task and Appointment which inherit from the Event class The Event Class This will be an abstract class. It will have four private integer members called year, month, day and hour which designate the time of the event It should also have an integer member called id which should be a...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT