Question

C++ programming. Please provide copy/paste code and a screenshot of the code being ran in the...

C++ programming. Please provide copy/paste code and a screenshot of the code being ran in the compiler.

Write a program that takes two rectangle objects and then adds them and returns the final rectangle object. You are required to overload the addition operator to add the two objects.

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

Code:

#include<iostream>
using namespace std;
//created a class named "r"
class r{
    //taking two varibles named "l" for length , "b" for breadth
    int l, b;
public:

    r (){
    l = 0;
    b = 0;
    //initiating l,b
}

r (int x, int y){
    l = x;
    b = y;
}

r operator+ (r rect)
//addition operator to add objects

{
    r n;
    n.l = l + rect.l;
    n.b = b + rect.b;
    return n;
}

void disp_r ()
{
    cout << endl << "length of the rectangle = " << l;
    cout << endl << "breadth of the rectangle = " << b;
    //assigning values of the l and b
}

};

int main ()
{

r rec1 (4,5), rec2 (2,3), rec3;
//values of first and second rectangles

cout << "------Rectangles------" << endl;
cout << "Rectangle-->1 : " << endl;
rec1.disp_r ();
cout << endl << endl;
cout << "Rectangle-->2 : " << endl;
rec2.disp_r ();
cout << endl << endl;

rec3 = rec1 + rec2;

cout << "Final Rectangle : " << endl;
rec3.disp_r ();
//printing the final rectangle

return 0;
}

Screenshot of the code:

main.cpp 1 #include<iostream> 2 using namespace std; 3 //dreated a class named r 4. class r{ //taking two varibles named

void dispr () cout << endl << length of the rectangle = << 1; cout << endl << breadth of the rectangle = << bi //assign

Screenshot of the Output:

pus ------Rectangles------ Rectangle-->1 : length of the rectangle = 4 breadth of the rectangle = 5 Rectangle-->2 : length of

Note:

Please Comment if you have any doubts. Thank You.

Add a comment
Know the answer?
Add Answer to:
C++ programming. Please provide copy/paste code and a screenshot of the code being ran in the...
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
  • C++ programming. Please provide copy/paste code and a screenshot of the code being ran in the...

    C++ programming. Please provide copy/paste code and a screenshot of the code being ran in the compiler. Vertebrates are the types of animals that typically have backbones with a bony skeleton and a brain. Vertebrates can be mammals, reptiles, amphibians, fish, and birds. Based on this information, do the following: Create a C++ hierarchy of the vertebrates class along with the attributes (variables) and behaviors (functions) using C++ syntax. Is this an example of single inheritance or multiple inheritance? Why...

  • C Programming only. (NO C++) Copy & paste all source code with your answer. Also take...

    C Programming only. (NO C++) Copy & paste all source code with your answer. Also take screenshot of it compiled. 11. Write a program with the following software architecture int main) void ReadX/ OuT */ Ant *x) void IncrementX4Times(/t IO */ int *x) void Printx(/*IN */ int x) such that (1) the function ReadX) is called by the function main0 to input the value for main0 variable x from the program user as shown in the screenshot below; (2) IncrementX4Times0...

  • Programming logic C# visual studio. Please paste the code with the screenshot. Compute the average of...

    Programming logic C# visual studio. Please paste the code with the screenshot. Compute the average of the elements of the given array and then output the average. int[] numbers = new int[10]; Add these numbers to the array 87, 68, 94, 100 , 83, 78, 85, 91, 76, 87 The output should be like this: The average is ……

  • Please Provide me all Necessary Screenshot with copy able code. Thanks Write a scheme function named...

    Please Provide me all Necessary Screenshot with copy able code. Thanks Write a scheme function named up-to-first-number that takes a list as its input and returns a list containing all the elements up to the first numeric element in the input list. You can use the number? predicate function to determine whether an element is a number or not. Sample runs: (up-to-first-number '(a b c d 1 2 3 )) returns (a b c d) (up-to-first-number '(d e f 7))...

  • C Programming: Functions Please answer 1 and 2 on paper in C code. Not C++ or...

    C Programming: Functions Please answer 1 and 2 on paper in C code. Not C++ or C# just C. If you could provide some comments with a brief explanation of why you did what you did, that would be very much appreciated. Thank you for your help!! :) 1. Write a function that takes a single float, and returns that number rounded as an integer (not truncated, rounded properly). Ex: round(4.5) -> 5, round (4.4) -> 4 2. Write a...

  • Programming in Java. Please attached the screenshot of source code and the output Thanks. Java Programming:...

    Programming in Java. Please attached the screenshot of source code and the output Thanks. Java Programming: A Comprehensive Introductia See this solution in the app :3 Chapter 3, Problem 17E Bookmark Show all steps: 0 ON Problem Write a program that uses a loop to print a list of 100 numbers consisting of alternating 1's and -1's, starting with 1.

  • programming language is C++. Please also provide an explanation of how to create a file with...

    programming language is C++. Please also provide an explanation of how to create a file with the given information and how to use that file in the program Let's consider a file with the following student information: John Smith 99.0 Sarah Johnson 85.0 Jim Robinson 70.0 Mary Anderson 100.0 Michael Jackson 92.0 Each line in the file contains the first name, last name, and test score of a student. Write a program that prompts the user for the file name...

  • PLEASE DO IN C# AND MAKE SURE I CAN COPY CODE IN VISUAL STUDIO Exercise #1:...

    PLEASE DO IN C# AND MAKE SURE I CAN COPY CODE IN VISUAL STUDIO Exercise #1: Design and implement class Rectangle to represent a rectangle object. The class defines the following attributes (variables) and methods: 1. Two Class variables of type double named height and width to represent the height and width of the rectangle. Set their default values to 1.0 in the default constructor. 2. A non-argument constructor method to create a default rectangle. 3. Another constructor method to...

  • Hi, please provide a code solution written in the C++ language to the following programming problem....

    Hi, please provide a code solution written in the C++ language to the following programming problem. The answer you provide has to include code comments, as it is being explained to a person with no technical background. In your answer, please show which portion of the code is a .cpp and which is a .h (if used). We are asked to separate them out (ie. main.cpp, classname.cpp, headername.h). Please show the output of your program to show it compiled error...

  • Java Program (PLEASE PROVIDE SCREENSHOT OF THE CODE) Create a class Person which is a super...

    Java Program (PLEASE PROVIDE SCREENSHOT OF THE CODE) Create a class Person which is a super class. The class includes four private String instance variables: first name, last name, social security number, and state. Write a constructor and get methods for each instance variable. Also, add a toString method to print the details of the person. After that create a class Teacher which extends the class, Person. Add a private instance variable to store number of courses. Write a constructor...

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