Question

In: Computer Science

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()
{
    //declaring and taking input of radius
    double r;
    cout<<"Enter radius: ";
    cin>>r;
  
    //making an object of class circle
    Circle c(r);
  
    //printing area of circle
    cout<<"Area = "<<c.getArea()<<endl;
}

Write a for loop that displays the radius and area of the circles represented by the array you defined in the source code above.

Solutions

Expert Solution

//----------- Circle.cpp -----------
#include <iostream>
#include<limits>
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;
}
};

//function that checks if cin is failed or not
//fail means invalid type is inputted for a variable.
//like giving string instead of number .
bool isInvalidInput()
{
   //if cin is fail
   if(cin.fail())
   {
       //clear the buffer.
       cin.clear();
       //ignore the characters.it need limits header.
       cin.ignore(numeric_limits<streamsize>::max(),'\n');
       return true;
   }
   //if not return false.
   return false;
}

//main method
int main()
{
   //get number of circles to be created from user as input.
   int n;
   cout<<"Enter Number of circles want to create: ";
   cin >> n;
   //if invalid input like entering string for n or n is <= 0;
   //print error msg and exit
   if(isInvalidInput() || n <= 0 )
   {
       cout<<"Invalid number of circles entered\n";
       exit(1);
   }
   //array of circle.
   Circle circles[n];
//radius
double r;
//get n number of circles from user.
for(int i =0;i<n;i++)
{
       cout<<"\nEnter radius: ";
       //get raidus
       cin>>r;
       //check for invalid input, if invalid set radisu to 1
       if(isInvalidInput())
       {
           cout<<"Invalid radius entered,setting radius to 1."<<endl;
           r = 1;
       }
       //create a neew circle object for the given radius.
       Circle circle(r);
       //insert in circles array
       circles[i] = circle;
   }
  
   //print the radius and the area of the circle in circles array.
   for(int i =0;i<n;i++)
   {
       cout<<"\nRadius: "<<circles[i].getRadius()<<" , Area: "<<circles[i].getArea()<<endl;
   }
   return 0;
}
//------------- SAMPLE OUTPUT -----------

//please like the answer.


Related Solutions

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 <<...
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:...
Please write variables and program plan(pseudocode) of this C++ programming code: #include <iostream> using namespace std;...
Please write variables and program plan(pseudocode) of this C++ programming code: #include <iostream> using namespace std; void leapYear(int x); int main() { int x; cout << "Enter a year: "; cin >> x; leapYear (x);   return 0; } void leapYear(int x ) {    if (x % 400 == 0)    {    cout << "This is a leap Year";}    else if    ((x % 4 == 0) && (x % 100 != 0))    {    cout <<...
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{...
Complete the C++ code #include <iostream> #include <stdlib.h> #include <time.h> using namespace std; struct Cell {...
Complete the C++ code #include <iostream> #include <stdlib.h> #include <time.h> using namespace std; struct Cell { int val; Cell *next; }; int main() { int MAX = 10; Cell *c = NULL; Cell *HEAD = NULL; srand (time(NULL)); for (int i=0; i<MAX; i++) { // Use dynamic memory allocation to create a new Cell then initialize the // cell value (val) to rand(). Set the next pointer to the HEAD and // then update HEAD. } print_cells(HEAD); }
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.  
In C++, assuming you have the following incomplete code: #include<iostream> #include <unistd.h> using namespace std; //...
In C++, assuming you have the following incomplete code: #include<iostream> #include <unistd.h> using namespace std; // Structure for storing the process data struct procData { char pname[5]; // Name of a process int arrivt; //Arrival time of a process int pburst; // Burst time of a process int endtime; // Exit time/ Leaving time of a process int remburst; // Remaining burst time of a process int readyFlag; // boolean, Flag for maintaining the process status }; // Global variable...
using only loops, no functions and no arrays. with the heading: #include <iostream> using namespace std;...
using only loops, no functions and no arrays. with the heading: #include <iostream> using namespace std; "Write a program that asks the user to enter an odd positive integer. The program reads a value (n) entered by the user and prints an n x n grid displaying a large letter X. The left half should be made up of pluses (+) and the right half should be made with the character "x" and the very center should be an asteric...
Using only loops, no functions and no arrays. with the heading: #include <iostream> using namespace std;...
Using only loops, no functions and no arrays. with the heading: #include <iostream> using namespace std; "Write a program that asks the user to enter an integer between 1 and 20. If the user enters an illegal number, the program repeatedly asks the user to enter the correct one. If the user has not entered a correct number after 10 attempts, the program chooses the number 10 as the user's number. The program prints the cube of the user's number.
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT