Question

C++ question. Must use #include <iostream> and "using namespace std;" You must also have only a...

C++ question. Must use #include <iostream> and "using namespace std;" You must also have only a .cpp and .h file

Define a class called Rectangle that can store the height and width of a rectangle as instance variables, has a constructor that allows you to set the height and width when you create a Rectangle, a default constructor that sets both height and width to 0, and functions to return the following values:

* the perimeter [ which should be 2 * (height + width)]
* the area [which should be height * width]

Homework Answers

Answer #1

C++ CODE:

Rectangle.h file code:


class Rectangle{
   int height;
   int width;
public:
   Rectangle(int x=0,int y=0){
       height = x;
       width = y;
   }
   int perimeter(){
       return 2*(height+width);
   }
   int area(){
       return height*width;
   }
};
Main Program CODE:

#include <iostream>
#include "rectangle.h"
using namespace std;

int main() {
   Rectangle rec(5,4);
   cout<<rec.perimeter()<<'\n';
   cout<<rec.area()<<'\n';
   return 0;
}

CODE SCREENSHOT:

Know the answer?
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
Similar Questions
complete the following tasks in C++ code: #include <iostream> using namespace std; class Cube{ public: Cube(int,...
complete the following tasks in C++ code: #include <iostream> using namespace std; class Cube{ public: Cube(int, int,int); int getVolume(); private: int height; int weight; int depth; }; Cube::Cube(int h, int w, int l) { height = h; weight = w; depth = l; } int main(){ }
Lab: RectClass (constructor) Code in C++ This program creates a Rectangle object, then displays the rectangle's...
Lab: RectClass (constructor) Code in C++ This program creates a Rectangle object, then displays the rectangle's length, width, and area Define an overloaded constructor and use it when creating the Rectangle object instead of using the setters. Change this program to calculate and display the rectangle's perimeter. Example: In feet, how wide is your house? 20 In feet, how long is your house? 25 The house is 20.00 feet wide. The house is 25.00 feet long. The house has 500.00...
#ifndef BAG_H #define BAG_H #include <cstdlib> // Provides size_t using namespace std; class bag { public:...
#ifndef BAG_H #define BAG_H #include <cstdlib> // Provides size_t using namespace std; class bag { public: // TYPEDEFS and MEMBER CONSTANTS typedef int value_type; typedef std::size_t size_type; static const size_type CAPACITY = 30; // CONSTRUCTOR bag() {used = 0;} // MODIFICATION MEMBER FUNCTIONS size_type erase(const value_type& target); bool erase_one(const value_type& target); void insert(const value_type& entry); void operator +=(const bag& addend); void sort(const bag& b); //Sort the array in the bag object // CONSTANT MEMBER FUNCTIONS size_type size( ) const {...
Take the following program and translate it into PEP/9 assembly language: #include <iostream> using namespace std;...
Take the following program and translate it into PEP/9 assembly language: #include <iostream> using namespace std; int fib(int n) { int temp; if (n <= 0)    return 0; else if (n <= 2)    return 1; else {    temp = fib(n – 1);    return temp + fib(n-2); } } int main() {    int num;    cout << "Which fibonacci number? ";    cin >> num;    cout << fib(num) << endl;    return 0; } You...
Menu.cpp isn't working. C++  utilize inheritance to create a banking app. The architecture of the app is...
Menu.cpp isn't working. C++  utilize inheritance to create a banking app. The architecture of the app is up to you as long as you implement some form of inheritance. Below is my code however the credit and the actual menu isn't working. Can i get some help on getting the menu to work properly. // BankAccount.h #ifndef ACCOUNT_H #define ACCOUNT_H #include <string> #include <iostream> using namespace std; class BankAccount { protected : int noOfWithdrawls; private: // Declaring variables    float balance;...
My assignment is listed below. I already have the code complete, but I cannot figure out...
My assignment is listed below. I already have the code complete, but I cannot figure out how to complete this portion: You must create a makefile to compile and build your program. I can't figure out if I need to create a makefile, and if I do, what commands do I use for that? Create a  ContactInfo class that contains the following member variables: name age phoneNumber The ContactInfo class should have a default constructor that sets name = "", phoneNumber...
Cpp Task: Create a class called Mixed. Objects of type Mixed will store and manage rational...
Cpp Task: Create a class called Mixed. Objects of type Mixed will store and manage rational numbers in a mixed number format (integer part and a fraction part). Details and Requirements Your class must allow for storage of rational numbers in a mixed number format. Remember that a mixed number consists of an integer part and a fraction part (like 3 1/2 – “three and one-half”). The Mixed class must allow for both positive and negative mixed number values. A...
C++ PROGRAMMING Hi! I have to make a program that adds fractions and simplifies them. I...
C++ PROGRAMMING Hi! I have to make a program that adds fractions and simplifies them. I feel like I know how to write that. What I'm having trouble with is implementing two files the professer gave us. I would appreicate any help in understanding their purpose as in if Im supposed to take information from those files or give it information. Thank you! I have attatched the homework instructions and the two files given. Implementation The main program, called calculator.cpp...
//evil_server.cpp #include <string> #include <cstdlib> #include <iostream> #include "evil_server.h" using namespace std; EvilServer :: EvilServer() {...
//evil_server.cpp #include <string> #include <cstdlib> #include <iostream> #include "evil_server.h" using namespace std; EvilServer :: EvilServer() {    hacked[0] = hacked[1] = hacked[2] = false;    passwords[agent_index(MrMean)] = random_pw(MrMean);    passwords[agent_index(MsChief)] = random_pw(MsChief);    passwords[agent_index(DrEvil)] = random_pw(DrEvil); } void EvilServer :: change_pw(EvilAgent agent, string new_pw) {    int index = agent_index(agent);    if (new_pw == passwords[index])        return;    hacked[index] = false;    passwords[index] = new_pw; } string EvilServer :: random_pw(EvilAgent agent) {    string password;    int length;   ...
C++ ONLY -- PRACTICE ASSIGNMENT For our practice assignment we have to turn in 2 files...
C++ ONLY -- PRACTICE ASSIGNMENT For our practice assignment we have to turn in 2 files - Driver.cpp and StringQueue.h Driver.cpp is provided for us, but if there are any changes needed to be made to that file please let me know. Based on the instructions below, what should the code look like for StringQueue.h ? Create a class named StringQueue in a file named StringQueue.h. Create a QueueNode structure as a private member of the class. The node should...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT
Active Questions
  • Provide the following: A list of two functional requirements. A list of two nonfunctional requirements (must...
    asked 2 minutes ago
  • Select a restaurant with which you are familiar and determine the most likely storage management challenge...
    asked 7 minutes ago
  • Proved 3 examples of problems that have surfaced in health and social services from the arrival...
    asked 7 minutes ago
  • A soccer goalie allows a goal on average of 2.3 times per game. Make an estimate...
    asked 7 minutes ago
  • What are some of the similarities and differences associated with caregiver/parental support across these classifications?
    asked 14 minutes ago
  • It is believed that the mean height of middle school students who play basketball on the...
    asked 14 minutes ago
  • Python Indicate whether each statement will evaluate to True or False after running these initializations: a...
    asked 56 minutes ago
  • Briefly explain with two examples how traditional leaders in communities can employ the Ubuntu concept for...
    asked 1 hour ago
  • Question 1 Which are two of the four key aspects of hegemony? Group of answer choices...
    asked 2 hours ago
  • A consumer’s preferences are represented by the following utility function: u(x, y) = lnx + 1/2...
    asked 2 hours ago
  • Kingsford Furnishings Company manufactures designer furniture. Kingsford Furnishings uses a job order cost system. Balances on...
    asked 2 hours ago
  • 300 words:      (b) Do you think that it is appropriate for firms like Black Diamond...
    asked 3 hours ago