Question

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

C++ code

  1. 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
  2. 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
  3. Derive from the Base class from called Shape2d called Rectangle
    • with the additional floating-point attributes length & width
  4. Derive from the Base class from called Shape2d called Circle
    • with the additional floating-point attributes radius
  5. Create the Base class called Shape3d
    • with the protected floating point attribute volume
    • operator overload the *(have it add) & /(have it subtract) and operations to return the float respective to the volume
  6. Derive from both Rectangle and Shape3d a class called Box
    • with the additional floating-point attributes height
  7. Derive from both Circle and Shape3d a class called Cylinder
    • with the additional floating-point attributes height
  8. For each of the following classes [Circle, Rectangle, Box & Cylinder] create 3 instances each (using the parameterized constructor {no interactive input required}
  9. Demonstrate the + & - operation overloading using the following combinations
    Circle + Circle
    Rectangle + Rectangle
    Rectangle + Circle
    Circle - Circle
    Rectangle - Rectangle
    Rectangle - Circle
    Box - Cylinder
    Box+Cylinder
    Box + Box
    Box -Box
    Rectangle + Box
    Box + Circle
    Box - Circle
    Rectangle - Box
    Cylinder + Cylinder
    Rectangle + Cylinder
    Cylinder + Circle
    Cylinder - Circle
    Rectangle - Cylinder
    Cylinder - Cylinder
  10. Demonstrate the * & / operation overloading using the following combinations
    Box * Box
    Cylinder * Cylinder
    Box * Cylinder
    Box / Box
    Cylinder / Cylinder
    Box / Cylinder
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<iostream>
using namespace std;

class Shape2D
{
protected:
float area;
public:
Shape2D()
{
area = 0;
}
Shape2D(float a)
{
area = a;
}
float getArea()
{
return area;
}
friend Shape2D operator + (Shape2D &s1, Shape2D &s2)
{
Shape2D temp;
temp.area = s1.getArea()+ s2.getArea();
return temp;
}
};

class Rectangle : public Shape2D
{
float length, width;
public:
Rectangle()
{
length = width = 0;
}
Rectangle(float l, float w) : Shape2D(0.0f)
{
length = l;
width = w;
area = length * width;
}
};

class Circle : public Shape2D
{
float radius;
public:
Circle()
{
radius = 0;
}
Circle(float r) : Shape2D(0.0f)
{
radius = r;
area = 3.141f r r;
}
};

int main()
{
Rectangle rr1(2, 3), rr2(3, 4);
Circle c1(5), c2(3), c3;
Shape2D &r1 = rr1, &r2 = rr2, r3;
r3 = r1 + r2;
cout<<"\n Rectangle + Rectangle: "<<r3.getArea();
r1 = c1; r2 = c2;
r3 = r1 + r2;
cout<<"\n Circle + Circle: "<<r3.getArea();
r1 = rr1; r2 = c1;
r3 = r1 + r2;
cout<<"\n Circle + Rectangle: "<<r3.getArea();
}

Sample Output:

Rectangle + Rectangle: 18
Circle + Circle: 106.794
Circle + Rectangle: 157.05

Add a comment
Know the answer?
Add Answer to:
C++ code For each of the following classes create default constructor and parameterized constructors(calling parent(s) parameterized...
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
  • using java write a code with notepad++ Create the following classes using inheritance and polymorphism Ship...

    using java write a code with notepad++ Create the following classes using inheritance and polymorphism Ship (all attributes private) String attribute for the name of ship float attribute for the maximum speed the of ship int attribute for the year the ship was built Add the following behaviors constructor(default and parameterized) , finalizer, and appropriate accessor and mutator methods Override the toString() method to output in the following format if the attributes were name="Sailboat Sally", speed=35.0f and year built of...

  • Program must be written in C++: Create a parent class called CompSciProfessor. Each professor has name...

    Program must be written in C++: Create a parent class called CompSciProfessor. Each professor has name (type string), email (type string), and facultyId (type long). Specialize the CompSciProfessor class into two more classes: AdjunctProf, and TenureTrackProf classes. The specialized class AdjunctProf has three attributes of its own: degree (type char), NoOfTA (type int), and NoOfCourses (type int). The attribute ‘degree’ refers to the degree of the adjunct professor. You assign ‘B’ to represent bachelor degree, ‘M’ for Master degree, and...

  • 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...

  • Please use C++ and Please do all file separate separately. And also I need output too....

    Please use C++ and Please do all file separate separately. And also I need output too. thanks. Please do it complete work. ShapeNodePoly.cpp Avaliable from: Wednesday, July 27, 2016, 10:20 AM Requested files: Point.h, Point.cpp, Shape.h, Shape.cpp, Polygon.h, Polygon.cpp, Ellipse.h, Ellipse.cpp, ShapeNodePoly.cpp, ShapeNodePoly_test.cpp (Download) Type of work: Individual work In this assignment, you will create a class that can behave as any of the shapes listed below. You will use object inheritance to enable all shapes to be managed using...

  • Project 1 – Classes and Top-down Design Overview Software development projects using the object-oriented approach involve...

    Project 1 – Classes and Top-down Design Overview Software development projects using the object-oriented approach involve breaking the problem down into multiple classes that can be tied together into a single solution. In this project, you are given the task of writing some classes that would work together for providing a solution to a problem involving some basic computations. Learning Objectives The focus of this assignment is on the following learning objectives: • Be able to identify the contents of...

  • in C++ use Inheritance for the following Shape Circle Sphere Cylinder Rectangle Square Cube You must define one class for each shape. And, use public inheritance when deriving from base cla...

    in C++ use Inheritance for the following Shape Circle Sphere Cylinder Rectangle Square Cube You must define one class for each shape. And, use public inheritance when deriving from base classes. Circle int r; void area(); void perimeter(); void volume(); //No volume for circle Sphere int r; void area();//Sphere surface area= 4 × pi × radius2 void perimeter(); //No perimeter for Sphere void volume();//Sphere volume= 4/3 × pi × radius2 Cylinder int r, height; void area();// Cylinder surface area=perimeter of...

  • code in java please:) Part I Various public transporation can be described as follows: A PublicTransportation...

    code in java please:) Part I Various public transporation can be described as follows: A PublicTransportation class with the following: ticket price (double type) and mumber of stops (int type). A CityBus is a PublicTransportation that in addition has the following: an route number (long type), an begin operation year (int type), a line name (String type), and driver(smame (String type) -A Tram is a CityBus that in addition has the following: maximum speed (int type), which indicates the maximum...

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