Question

In: Computer Science

Study the file polygon.h. It contains the header file for a class of regular polygons. Implement...

Study the file polygon.h. It contains the header file for a class of regular polygons. Implement the methods, and provide a driver to test it. It should be in C++

polygon.h file-

#ifndef POLY_RVC_H
#define POLY_RVC_H

#include <iostream>
using namespace std;


class Polygon
{
public:
   Polygon();
   Polygon(int n, double l);

   //accessors - all accessors should be declared "const"
   // usually, accessors are also inline functions
   int getSides() const { return sides; }
   double getLength() const { return length; }
  

   // function to set values (mutator)
   void setPoly(int n, double l);
   void setSides(int n);
   void setLength(double l);
   bool operator< (const Polygon & rhs) const
   {
       return area() < rhs.area();
   }
   double area() const;
   double perimeter() const;

private:
   int sides;
   double length;
  
};


ostream& operator << (ostream& outs, const Polygon& d);
bool operator == (const Polygon& p1, const Polygon& p2);


#include "poly_imp.cpp"

#endif

Solutions

Expert Solution

Note: While running if you get compile error please comment #include "poly_imp.cpp" in header file(polygon.h).

poly_imp.cpp

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

// Default constructor
Polygon::Polygon()
{
   sides=0;
   length=0;
}

// Parametrized constructor
Polygon::Polygon(int n, double l)
{
   sides=n;
   length=l;
}

// Set the polygon
void Polygon ::setPoly(int n, double l)
{
   sides=n;
   length=l;
}

// Set the sides of the polygon
void Polygon::setSides(int n)
{
   sides=n;
}

// Set the length of polygon
void Polygon::setLength(double l)
{
   length=l;
}

// Comput area of polygon
double Polygon:: area() const
{
   return (length*length*sides)/(4*tan(3.14/sides));
}

// Compute perimeter of polygon
double Polygon::perimeter() const
{
   return sides*length;
}

//Print the output
ostream& operator << (ostream& outs, const Polygon& d)
{
   outs<<"Number of sides: "<<d.getSides()<<endl;
   outs<<"Length of one side: "<<d.getLength()<<endl;
   outs<<"Area of polygon: "<<d.area()<<endl;
   outs<<"Perimeter of polygon: "<<d.perimeter()<<endl;
   return outs;
}

// Comparison
bool operator == (const Polygon& p1, const Polygon& p2)
{
   if((p1.getSides()==p2.getSides()) && (p1.getLength()==p2.getLength()))
       return true;
   else
       return false;
}

main.cpp

#include<iostream>
#include "polygon.h"
void main()
{
   // First polygon
   cout<<"First Polygon:"<<endl;
   Polygon p(7,6);
   cout<<p;
   cout<<endl;

   // Second polygon
   cout<<"Second Polygon:"<<endl;
   Polygon p1;
   p1.setSides(7);
   p1.setLength(6);
   cout<<p1;
   cout<<endl;

   // Check both are equal
   if(p==p1)
       cout<<"Both polygons are equal"<<endl;
   else
       cout<<"Both polygons are not equal"<<endl;
   system("pause");
}

Output:


Related Solutions

A header file contains a class template, and in that class there is a C++ string...
A header file contains a class template, and in that class there is a C++ string object. Group of answer choices(Pick one) 1)There should be a #include for the string library AND a using namespace std; in the header file. 2)There should be a #include for the string library. 3)There should be a #include for the string library AND a using namespace std; in the main program's CPP file, written before the H file's include.
Problem Statement: Implement the MyString class using a header and implementation file named MyString.h and MyString.cpp...
Problem Statement: Implement the MyString class using a header and implementation file named MyString.h and MyString.cpp respectively. Make sure to properly test your code on your own by creating a test driver that tests every function created in the MyString class. Deliverables: proj3-MyString.h proj3-MyString.cpp proj3-testMain.cpp Memory Requirements: Your MyString should start with 10 bytes of allocated memory and should grow in size by doubling. So, we should be able to predict the capacity of your MyString as acquiring a patten...
please complete the header file that contains a class template for ADT Queue and complete all...
please complete the header file that contains a class template for ADT Queue and complete all the member functions in the class template. Submit the header file only, but please write a source file that tests all the member functions to make sure they are working correctly. queue.h #ifndef _QUEUE #define _QUEUE #include"Node.h" template<class ItemType> class Queue { private:    Node<ItemType> *backPtr;    Node<ItemType> *frontPtr; public:    Queue(); //Default constructor    Queue(const Queue<ItemType> &aQueue);    bool isEmpty() const;    bool...
Write a C++ program that design a class definition to be put in a header file...
Write a C++ program that design a class definition to be put in a header file called fizzjazz.h A store sells online FizzJazz which are sound tones made by famous bands. For each FizzJazz, the store wants to keep track of the following attributes: * title - the name of the sound tone * band - Famous band name that created the tone * duration - this is in seconds and may be fractional: examples: 20.0, 34.5 Each attribute will...
Write a C++ program that design a class definition to be put in a header file...
Write a C++ program that design a class definition to be put in a header file called fizzjazz.h A store sells online FizzJazz which are sound tones made by famous bands. For each FizzJazz, the store wants to keep track of the following attributes: * title - the name of the sound tone * band - Famous band name that created the tone * duration - this is in seconds and may be fractional: examples: 20.0, 34.5 Each attribute will...
Write a C++ program that design a class definition to be put in a header file...
Write a C++ program that design a class definition to be put in a header file called fizzjazz.h A store sells online FizzJazz which are sound tones made by famous bands. For each FizzJazz, the store wants to keep track of the following attributes: * title - the name of the sound tone * band - Famous band name that created the tone * duration - this is in seconds and may be fractional: examples: 20.0, 34.5 Each attribute will...
In a header file Record.h, create a Record structure that contains the following information: recordID, firstName,...
In a header file Record.h, create a Record structure that contains the following information: recordID, firstName, lastName, startYear. recordID is an integer. In testRecord, first create a record (record1) using “normal” pointers. Set the values to 1001, Fred, Flintstone, 1995 In testRecord, create a new record (record2) using a unique smart pointer. Set the values to 1002, Barney, Rubble, 2000 Create a function (or functions) that will print the records to any output stream. Since the print function does not...
using the header: #include <pthread.h> // This is a header file for a Read/Right Lock Library....
using the header: #include <pthread.h> // This is a header file for a Read/Right Lock Library. Your C code //SHOULD access your routines using these exact function // prototypes typedef struct RW_lock_s { } RW_lock_t; void RW_lock_init(RW_lock_t *lock); /* This routine should be called on a pointer to a struct variable of RW_lock_t to initialize it and ready it for use. */ void RW_read_lock(RW_lock_t *lock); /* This routine should be called at the beginning of a READER critical section */...
JAVA PROGRAMMING Implement a class Purse. A purse contains a collection of coins. For simplicity, we...
JAVA PROGRAMMING Implement a class Purse. A purse contains a collection of coins. For simplicity, we will only store the coin names in an ArrayList<String>. Supply a method void addCoin(String coinName). Add a method toString to the Purse class that prints the coins in the purse in the format Purse[Quarter,Dime,Nickel,Dime]. Write a method reverse that reverses the sequence of coins in a purse. Implement a TestPurse class with a main method in it. It will use the toString method to...
IN JAVA Searching and Sorting In An Integer List File IntegerList contains a Java class representing...
IN JAVA Searching and Sorting In An Integer List File IntegerList contains a Java class representing a list of integers. The following public methods are provided: ? IntegerList(int size)—creates a new list of size elements. Elements are initialized to 0. ? void randomize()—fills the list with random integers between 1 and 100, inclusive. ? void print()—prints the array elements and indices ? int search(int target)—looks for value target in the list using a linear (also called sequential) search algorithm. Returns...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT