Question

Kindly write the code in C++ and also please show the screenshot of the output.

Will appreciate your kind response. Thank you.

Question 2: Code this UML using pure virtual function. Shape polymorphic operations draw(g: Graphics ) getArea(): int getBoun

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

Since its not mentioned here so I am assuming that g is an object if Graphics class which is coming as an input parameter in draw(g:Graphics) function. Therefore I am taking Graphics as a dummy empty class below. Change it as per your business logic.

In UML, if function name and class names are written in italics that means they are abstract. So here we have 2 pure virtual functions:-

1) draw(g:Graphics) -> Assuming the return type as void

2) getArea() : int

whose defintion will be provided by the concrete sub-classes Square and Circle.

Since Shape is an abstract class, we cannot instantiate its object.

Please find below in red an outline of the C++ code as per the given UML Diagram:-

You can fill up the business logic inside the functions as per your requirements.

#include <iostream>

class Graphics
{
//dummy class
};

class Shape
{
public:
Shape() = default; // Default constructor
virtual ~Shape() = default; // default virtual destructor
virtual void draw(Graphics g) = 0; // pure virtual function
virtual int getArea() = 0; // pure virtual function
int getBoundingArea();
};

int Shape::getBoundingArea()
{
std::cout << "Inside Shape::getBoundingArea();\n";
// TO-DO: fill up your business logic here
return 0;
}

class Square : public Shape {
public:
Square() = default;
virtual ~Square() = default; // Keeping virtual destructor in case of sub classing .
// If sub classing is not to be done then can be normal destructor
void draw(Graphics g) override; // This means it is overriding the base class function
int getArea() override; // Overriding base class function
};

void Square::draw(Graphics g)
{
std::cout << "Square::draw() -> Drawing Square shape\n";
// TO-DO: fill up your business logic here
}

int Square::getArea()
{
std::cout << "Square::getArea() -> Getting area of Square shape\n";
// TO-DO: fill up your business logic here
return 0;
}

class Circle : public Shape {
public:
Circle() = default;
virtual ~Circle() = default; // Keeping virtual destructor in case of sub classing .
// If sub classing is not to be done then can be normal destructor
void draw(Graphics g) override; // This means it is overriding the base class function
int getArea() override; // Overriding base class function
};

void Circle::draw(Graphics g)
{
std::cout << "Circle::draw() -> Drawing Circle shape\n";
// TO-DO: fill up your business logic here
}

int Circle::getArea()
{
std::cout << "Circle::getArea() -> Getting area of Circle shape\n";
// TO-DO: fill up your business logic here
return 0;
}

int main()
{
Graphics g; // Dummy Graphics class object
Shape *square = new Square(); // Creating Square class object of base class type i.e Shape
square->draw(g); // It will call Square::draw() since its overridden by subclass Square
square->getArea(); // It will call Square::getArea() since its overridden by subclass Square
square->getBoundingArea(); // It will call Shape::getBoundingArea() since its not overridden by subclass Square
  
Shape *circle = new Circle(); // Creating Circle class object of base class type i.e Shape
circle->draw(g); // It will call Circle::draw() since its overridden by subclass Circle
circle->getArea(); // It will call Circle::getArea() since its overridden by subclass Circle
circle->getBoundingArea(); // It will call Shape::getBoundingArea() since its not overridden by subclass Circle
  
delete(square); // Deleting square object
delete(circle); // Deleting circle object

return 0;
}

Output:-

Square::draw() -> Drawing Square shape                                                                                                          

Square::getArea() -> Getting area of Square shape                                                                                               

Inside Shape::getBoundingArea();                                                                                                                

Circle::draw() -> Drawing Circle shape                                                                                                          

Circle::getArea() -> Getting area of Circle shape                                                                                               

Inside Shape::getBoundingArea();   

...Program finished with exit code 0                                                                                                            

Press ENTER to exit console.   

As you can see in the main function, we have used the Pointer of Base class type i.e. Shape which is referring to the subclass Square/Circle. This is the functionality of virtual functions in C++.

Code will be compiled on C++11 or above since override keyword has been introduced in C++11.

For your reference, please find below the screenshots for the code as well as of the output screen.

CODE:-

#include <iostream> class Graphics { //dummy class }; class Shape { public: Shape() = default; // Default constructor virtual

void Square:: draw(Graphics g) { std::cout << Square::draw() -> Drawing Square shape\n; // TO-DO: fill up your business Log

{ int Circle::getArea() std::cout << Circle::getArea() -> Getting area of Circle shape\n; // TO-DO: fill up your business L

OUTPUT:-

Square::draw() -> Drawing Square shape Square::getArea() -> Getting area of Square shape Inside Shape::getBoundingArea(); Cir

Hope it helps!

Cheers!!

Add a comment
Know the answer?
Add Answer to:
Kindly write the code in C++ and also please show the screenshot of the output. Will...
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
  • 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...

  • A general shape class is shown below. Shape has a dimension. It also defines constructors, getters,...

    A general shape class is shown below. Shape has a dimension. It also defines constructors, getters, setters and a toString method. class Shape{ int dimension; public Shape(){} public Shape(int newDimension){ dimension = newDimension; } public int getDimension(){ return dimension; } public void setDimension(int newDimension){ dimension = newDimension; } public String toString(){ return "Shape has dimension "+dimension; } } a. Define classes Circle and Square, which inherit from Shape class. b. Both classes must have two constructors similar to Shape class....

  • This question is about java program. Please show the output and the detail code and comment.And...

    This question is about java program. Please show the output and the detail code and comment.And the output (the test mthod )must be all the true! Thank you! And the question is contain 7 parts: Question 1 Create a Shape class with the following UML specification: (All the UML "-" means private and "+" means public) +------------------------------------+ | Shape | +------------------------------------+ | - x: double | | - y: double | +------------------------------------+ | + Shape(double x, double y) | |...

  • this is for java programming Please Use Comments I am not 100% sure if my code...

    this is for java programming Please Use Comments I am not 100% sure if my code needs work, this code is for the geometric if you feel like you need to edit it please do :) Problem Description: Modify the GeometricObject class to implement the Comparable interface and define a static max method in the GeometricObject class for finding the larger (in area) of two GeometricObject objects. Draw the UML and implement the new GeometricObject class and its two subclasses...

  • Java Programming .zip or .jar with 4 files: Shape.java, Square.java, TSquare.java, Triangle.java In this homework, you w...

    Java Programming .zip or .jar with 4 files: Shape.java, Square.java, TSquare.java, Triangle.java In this homework, you will build the below hierarchy: Overview: You will implement the supplier code: Shape, Square, TSquare, and Triangle, which will be used by the Homework7Main program. This program will use the DrawingPanel to draw these objects onto a canvas. Specification: You will take advantage of inheritance with using a super class, Shape.java. (below) Shape is an abstract class, which means it can be extended, but...

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

  • Java Painter Class This is the class that will contain main. Main will create a new...

    Java Painter Class This is the class that will contain main. Main will create a new Painter object - and this is the only thing it will do. Most of the work is done in Painter’s constructor. The Painter class should extend JFrame in its constructor.  Recall that you will want to set its size and the default close operation. You will also want to create an overall holder JPanel to add the various components to. It is this JPanel that...

  • How do I start this c++ code homework? Write a code in C++ for a BlackJack...

    How do I start this c++ code homework? Write a code in C++ for a BlackJack card game using the following simplified rules: Each card has a numerical value. Numbered cards are counted at their face value (two counts as 2 points, three, 3 points, and so on) An Ace count as either 1 point or 11 points (whichever suits the player best) Jack, queen and king count 10 points each The player will compete against the computer which represents...

  • Code in JAVA UML //TEST HARNESS //DO NOT CHANGE CODE FOR TEST HARNESS import java.util.Scanner; //...

    Code in JAVA UML //TEST HARNESS //DO NOT CHANGE CODE FOR TEST HARNESS import java.util.Scanner; // Scanner class to support user input public class TestPetHierarchy { /* * All the 'work' of the process takes place in the main method. This is actually poor design/structure, * but we will use this (very simple) form to begin the semester... */ public static void main( String[] args ) { /* * Variables required for processing */ Scanner input = new Scanner( System.in...

  • Please explain each line of code, all code will be in Java. Thank you JKL Restaurant...

    Please explain each line of code, all code will be in Java. Thank you JKL Restaurant maintains a members’ club for its customers.  There are three levels of membership: (1) Basic, (2) Silver, and (3) Gold. A certain member has to be exactly a Basic, Silver, or Gold member at any point in time.  Whenever a member spends money at JKL, he/she gets points and as these points are accumulated, one can redeem one or more $20 dining certificates.  Each Gold member can...

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