Question

C++ Please complete the implementation of the following source code (Question3.cpp). You need to add your...

C++
Please complete the implementation of the following source code (Question3.cpp). You need to add your code in the source code where the comment “// your code” locates. After you finish the implementation, please also provide the output of your program.

#include <iostream>

using namespace std;

class Shape
{
  protected:
        // your code
  public:
        void setWidth (int w)
        {
                // your code
        }
        void setHeight (int h)
        {
                // your code
        }
};

class Rectangle: public Shape
{
  public:
        int getArea ()
        {
                // your code
        }
};

class Triangle: public Shape
{
  public:
        int getArea ()
        {
                // your code
        }
};

int main (){

        Rectangle r;
        Triangle t;
        r.setWidth(1);
        r.setHeight(2);
        t.setWidth(3);
        t.setHeight(4);
        cout << r.getArea() << endl;
        cout << t.getArea() << endl;
        return 0;
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>

using namespace std;

class Shape {
protected:
    int width, height;
public:
    void setWidth(int w) {
        width = w;
    }

    void setHeight(int h) {
        height = h;
    }
};

class Rectangle : public Shape {
public:
    int getArea() {
        return width * height;
    }
};

class Triangle : public Shape {
public:
    int getArea() {
        return (width * height) / 2;
    }
};

int main() {
    Rectangle r;
    Triangle t;
    r.setWidth(1);
    r.setHeight(2);
    t.setWidth(3);
    t.setHeight(4);
    cout << r.getArea() << endl;
    cout << t.getArea() << endl;
    return 0;
}
Add a comment
Know the answer?
Add Answer to:
C++ Please complete the implementation of the following source code (Question3.cpp). You need to add your...
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
  • Hello I have a question. I would like to check if the code follows this requirement....

    Hello I have a question. I would like to check if the code follows this requirement. Rectangle.h - A complete Rectangle Class including both declaration and definition appRectangle,cpp separate in two different files the class definition and implementation Code: rectangle.h // Rectangle class declaration. class Rectangle { private: double width; double length; public: void setWidth(double); void setLength(double); double getWidth() const; double getLength() const; double getArea() const; }; //************************************************** // setWidth assigns a value to the width member. * //************************************************** void...

  • ((( Using Only The mentioned Concepts Solve the following question in DETAILS “ Take your time but don’t disappoint me plleeaasseee”: 1- Control structure (if/if else/while/for/switch/do while/ brea...

    ((( Using Only The mentioned Concepts Solve the following question in DETAILS “ Take your time but don’t disappoint me plleeaasseee”: 1- Control structure (if/if else/while/for/switch/do while/ break continue) 2- Functions 3- Arrays 4- pointers and strings 5- classes 6- file processing ))) if it was a program i need to see the code printed on computer and the output too please . 10. Write a C++code that include two classes: Rectangle and Triangle as shown below: #include <iostream> using...

  • Given a matrix, clockwise-rotate elements in it. Please add code to problem3.cpp and the makefile. I...

    Given a matrix, clockwise-rotate elements in it. Please add code to problem3.cpp and the makefile. I have also given the main_problem3.cpp and problem3.h to test. problem3.cpp::: #include "problem3.h" // A function to rotate a matrix mat[][MAX] void rotatematrix(int m, int n, int mat[][MAX]) { // your code here    } Makefile::: all : problem3.o    g++ -o mainp3 # your code here problem3.o : problem3.h problem3.cpp    # your code here clean:    rm -f *.o mainp3 main_problem3.cpp::: #include <iostream>...

  • Do not change anything in the supplied code below that will be the Ch12_Ex9.cpp except to...

    Do not change anything in the supplied code below that will be the Ch12_Ex9.cpp except to add documentation and your name. Please use the file names listed below since your file will have the following components: Ch12_Ex9.cpp given file //Data: 18 42 78 22 42 5 42 57 #include <iostream> #include "unorderedArrayListType.h" using namespace std; int main() { unorderedArrayListType intList(25);     int number;     cout << "Enter 8 integers: ";     for (int count = 0; count < 8; count++)...

  • HI USING C++ CAN YOU PLEASE PROGRAM THIS ASSIGNMENT AND ADD COMMENTS: stackARRAY: #include<iostream> #define SIZE...

    HI USING C++ CAN YOU PLEASE PROGRAM THIS ASSIGNMENT AND ADD COMMENTS: stackARRAY: #include<iostream> #define SIZE 100 #define NO_ELEMENT -999999 using namespace std; class Stack { int arr[SIZE]; // array to store Stack elements int top; public: Stack() { top = -1; } void push(int); // push an element into Stack int pop(); // pop the top element from Stack int topElement(); // get the top element void display(); // display Stack elements from top to bottom }; void Stack...

  • Given the following code: #include <iostream> #include <iomanip> using namespace std; class Animal{ private: int height;...

    Given the following code: #include <iostream> #include <iomanip> using namespace std; class Animal{ private: int height; int weight; public: void setHeight(int h){ height = h; } int getHeight(){ return height; } void setWeight(int w){ weight = w; } int getWeight(){ return weight; } virtual void makeSound() = 0; virtual void eat(); }; class Dog: public Animal{ public: void makeSound(){ cout << "Bow Bow\n"; } void eat (){ cout <<"The dog is eating\n"; } void Catch(){ cout << "The dog is...

  • Code in C++ Given class Triangle (in files Triangle.h and Triangle.cpp), complete main() to read and...

    Code in C++ Given class Triangle (in files Triangle.h and Triangle.cpp), complete main() to read and set the base and height of triangle1 and of triangle2, determine which triangle's area is larger, and output that triangle's info, making use of Triangle's relevant member functions. Ex: If the input is: 3.0 4.0 4.0 5.0 where 3.0 is triangle1's base, 4.0 is triangle1's height, 4.0 is triangle2's base, and 5.0 is triangle2's height, the output is: Triangle with larger area: Base: 4.00...

  • C++ Language I have a class named movie which allows a movie object to take the...

    C++ Language I have a class named movie which allows a movie object to take the name, movie and rating of a movie that the user inputs. Everything works fine but when the user enters a space in the movie's name, the next function that is called loops infinetly. I can't find the source of the problem. Down below are my .cpp and .h file for the program. #include <iostream> #include "movie.h" using namespace std; movie::movie() { movieName = "Not...

  • Creating a Class in C++ Summary In this lab, you create a programmer-defined class and then...

    Creating a Class in C++ Summary In this lab, you create a programmer-defined class and then use it in a C++ program. The program should create two Rectangle objects and find their area and perimeter. Instructions Ensure the class file named Rectangle.cpp is open in your editor. In the Rectangle class, create two private attributes named length and width. Bothlength and width should be data type double. Write public set methods to set the values for lengthand width. Write public...

  • Design a class named Triangle that extends GeometricObject class. The class contains: Three double data fields...

    Design a class named Triangle that extends GeometricObject class. The class contains: Three double data fields named side1, side2, and side3 with default values 1.0 to denote three sides of the triangle. A no-arg constructor that creates a default triangle with color = "blue", filled = true. A constructor that creates a triangle with the specified side1, side2, side3 and color = "blue", filled = true. The accessor functions for all three data fields, named getSide1(), getSide2(), getSide3(). A function...

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