Question

In: Computer Science

I want to indent this c++ program #include<iostream> using namespace std; class Rectangle{ private: double width;...

I want to indent this c++ program

#include<iostream>
using namespace std;

class Rectangle{
    private:
        double width;
        double length;
    public:
        void setWidth(double);
        void setLength(double);
        double getWidth() const;
        double getLength() const;
        double getArea() const;
        double getPerimeter() const;
        bool isSquare() const;
};
void Rectangle::setWidth(double w){
    width = w;
}
void Rectangle::setLength(double l){
        length = l;
}
double Rectangle::getWidth() const{
        return width;
}
double Rectangle::getLength() const{
        return length;
}

// Added Method definitions

double Rectangle::getArea() const{
        return (width * length);
}

double Rectangle::getPerimeter() const{
        return (2 * width + 2 * length);
}

bool Rectangle::isSquare() const{
        if(width==length)
                return true;
        return false;
}

int main(){
        Rectangle kitchen;
        Rectangle bedroom;
        Rectangle square;
        double totalArea, totalPerimeter;
        kitchen.setWidth(15);
        kitchen.setLength(12);
        bedroom.setWidth(10);
        bedroom.setLength(12);
        totalArea = kitchen.getArea() + bedroom.getArea();
        cout << "Your house has total area: " << totalArea << " sq ft.\n";
        totalPerimeter = kitchen.getPerimeter() + bedroom.getPerimeter();
        cout << "Your house has total perimeter: " << totalPerimeter << " ft.\n";
        square.setWidth(10);
        square.setLength(10);
        if(square.isSquare()){
                cout << "This object is a square! Area: " << square.getArea();
        }
        return 0;
}

Solutions

Expert Solution

#include <iostream>
using namespace std;

class Rectangle
{
        private:
                double width;
        double length;
        public:
                void setWidth(double);
        void setLength(double);
        double getWidth() const;
        double getLength() const;
        double getArea() const;
        double getPerimeter() const;
        bool isSquare() const;
};
void Rectangle::setWidth(double w)
{
        width = w;
}
void Rectangle::setLength(double l)
{
        length = l;
}
double Rectangle::getWidth() const
{
        return width;
}
double Rectangle::getLength() const
{
        return length;
}

// Added Method definitions

double Rectangle::getArea() const
{
        return (width *length);
}

double Rectangle::getPerimeter() const
{
        return (2 *width + 2 *length);
}

bool Rectangle::isSquare() const
{
        if (width == length)
                return true;
        return false;
}

int main()
{
        Rectangle kitchen;
        Rectangle bedroom;
        Rectangle square;
        double totalArea, totalPerimeter;
        kitchen.setWidth(15);
        kitchen.setLength(12);
        bedroom.setWidth(10);
        bedroom.setLength(12);
        totalArea = kitchen.getArea() + bedroom.getArea();
        cout << "Your house has total area: " << totalArea << " sq ft.\n";
        totalPerimeter = kitchen.getPerimeter() + bedroom.getPerimeter();
        cout << "Your house has total perimeter: " << totalPerimeter << " ft.\n";
        square.setWidth(10);
        square.setLength(10);
        if (square.isSquare())
        {
                cout << "This object is a square! Area: " << square.getArea();
        }
        return 0;
}

Related Solutions

A C++ question: I want to indent the code of this C++ program: #include<iostream> #include<cstring> using...
A C++ question: I want to indent the code of this C++ program: #include<iostream> #include<cstring> using namespace std; int lastIndexOf(char *s, char target) { int n=strlen(s); for(int i=n-1;i>=0;i--) { if(s[i]==target) { return i; } } return -1; } void reverse(char *s) { int n=strlen(s); int i=0,j=n-1; while(i<=j) { char temp=s[i]; s[i]=s[j]; s[j]=temp; i++; j--; } return; } int replace(char *s, char target, char replacementChar) { int len=strlen(s); int total=0; for(int i=0;i<len;i++) { if(s[i]==target) { s[i]=replacementChar; total+=1; } } return total;...
C++ please #include <iostream> using namespace std; /** * defining class circle */ class Circle {...
C++ please #include <iostream> using namespace std; /** * defining class circle */ class Circle { //defining public variables public: double pi; double radius; public: //default constructor to initialise variables Circle(){ pi = 3.14159; radius = 0; } Circle(double r){ pi = 3.14159; radius = r; } // defining getter and setters void setRadius(double r){ radius = r; } double getRadius(){ return radius; } // method to get Area double getArea(){ return pi*radius*radius; } }; //main method int main() {...
Writing a squareroot program in C++ using only: #include <iostream> using namespace std; The program must...
Writing a squareroot program in C++ using only: #include <iostream> using namespace std; The program must be very basic. Please don't use math sqrt. Assume that the user does not input anything less than 0. For example: the integer square root of 16 is 4 because 4 squared is 16. The integer square root of 18 is 5 because 4 squared is 16 and 5 squared is 25, so 18 is bigger than 16 but less than 25.  
I want Algorithim of this c++ code #include<iostream> using namespace std; int main() { char repeat...
I want Algorithim of this c++ code #include<iostream> using namespace std; int main() { char repeat = 'y'; for (;repeat == 'y';){ char emplyeename[35]; float basic_Salary,EPF, Dearness_Allow, tax, Net_Salary , emplyee_id; cout << "Enter Basic Salary : "; cin >> basic_Salary; Dearness_Allow = 0.40 * basic_Salary; switch (01) {case 1: if (basic_Salary <= 2,20,00) EPF = 0; case 2: if (basic_Salary > 28000 && basic_Salary <= 60000) EPF = 0.08*basic_Salary; case 3: if (basic_Salary > 60000 && basic_Salary <= 200000)...
Debug please. It's in C++ #include<iostream> #include<string> using namespace std; template <class T> double half(int x)...
Debug please. It's in C++ #include<iostream> #include<string> using namespace std; template <class T> double half(int x) { double h = x / 2; return h; } class TuitionBill { friend ostream& operator<<(ostream, TuitionBill); private: string student; double amount; public: TuitionBill(string, double); double operator/(int); }; TuitionBill::TuitionBill(string student, double amt) { student = student; amount = amt; } double TuitionBill::operator/(int factor) { double half = amount / factor; return hafl; } ostream& operator<<(ostream& o, TuitionBill) { o << t.student << " Tuition:...
c++ #include <iostream> #include <string> #include <ctime> using namespace std; void displayArray(double * items, int start,...
c++ #include <iostream> #include <string> #include <ctime> using namespace std; void displayArray(double * items, int start, int end) { for (int i = start; i <= end; i++) cout << items[i] << " "; cout << endl; } //The legendary "Blaze Sort" algorithm. //Sorts the specified portion of the array between index start and end (inclusive) //Hmmm... how fast is it? /* void blazeSort(double * items, int start, int end) { if (end - start > 0) { int p...
Debug please. It's in C++ #include<iostream> #include<string> using namespace std; class Prescription {    friend ostream&...
Debug please. It's in C++ #include<iostream> #include<string> using namespace std; class Prescription {    friend ostream& operator<<(ostream&, const Prescription&);    friend istream& operator>>(istream&, Prescription&);    private: int idNum; int numPills; double price;    public: Prescription(const int = 0, const int = 0, const double = 0.0); }; Prescription::Prescription(const int id, const int pills, const double p) {    id = id;    numPills = pills;    price = p; } ostream& operator<<(ostream& out, const Prescription pre) {    out <<...
#include <iostream> using namespace std; double print_instructions() { cout << "WELCOME TO BandN book stores" <<...
#include <iostream> using namespace std; double print_instructions() { cout << "WELCOME TO BandN book stores" << endl; cout << "Today we have a deal on e-books. Customers will receive a discount off their entire order.." << endl; cout << "The discount is 15% off your total order and cost per book is 8.99." << endl; cout << "Customers who buy 20 or more e-books will receive 20% off instead." << endl; cout << endl; return 0; } int no_of_books() {...
How do i make this program accept strings? #include <iostream> #include <algorithm> using namespace std; int...
How do i make this program accept strings? #include <iostream> #include <algorithm> using namespace std; int binaryToOctal(char digits[], int a) { int number; if (digits[0] == '0') { if (digits[1] == '1') if (digits[2] == '0') return 2; //found "010" else return 3; //found "011" else if (digits[1] == '0') if (digits[2] == '1') return 1; // found "001" else return 0; //found "000" } else { if (digits[0] == '1') if (digits[1] == '1') if (digits[2] == '0') return...
C++ I took 7/20 =( code: #include <iostream> #include<string.h> using namespace std; // function to calculate...
C++ I took 7/20 =( code: #include <iostream> #include<string.h> using namespace std; // function to calculate number non white space characters int GetNumOfNonWSCharacters(string str) { int i = 0; int count = 0; while(str[i] != '\0') { if(str[i] != ' ') { count += 1; } i++; } return count; } // function to calculate numbers of words int GetNumOfWords(string str) { int i = 0; int count = 1; while(str[i] != '\0') { if(str[i] == ' ' && str[i-1]...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT