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:...
#include <iostream> #include <string> #include <iomanip> #include <cstdlib> #include "Contact.h" using namespace std; class Contact {...
#include <iostream> #include <string> #include <iomanip> #include <cstdlib> #include "Contact.h" using namespace std; class Contact { public: Contact(string init_name = "", string init_phone = "000-000-0000"); void setName(string name); void setPhone(string phone); string getName()const; string getPhone()const; friend ostream& operator << (ostream& os, const Contact& c); friend bool operator == (const Contact& c1, const Contact& c2); friend bool operator != (const Contact& c1, const Contact& c2); private: string name, phone; }; Contact::Contact(string init_name, string init_phone) { name = init_name; phone = init_phone;...
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.  
Starting with the following C++ program: #include <iostream> using namespace std; void main () { unsigned...
Starting with the following C++ program: #include <iostream> using namespace std; void main () { unsigned char c1; unsigned char c2; unsigned char c3; unsigned char c4; unsigned long i1 (0xaabbccee); _asm { } cout.flags (ios::hex); cout << "results are " << (unsigned int) c1 << ", " << (unsigned int) c2 << ", " << (unsigned int) c3 << ", " << (unsigned int) c4 << endl; } Inside the block denoted by the _asm keyword, add code to...
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...
C++ Given Code: #include <iostream> #include <string> using namespace std; int main() { //declare variables to...
C++ Given Code: #include <iostream> #include <string> using namespace std; int main() { //declare variables to store user input bool cont = true; //implement a loop so that it will continue asking until the user provides a positive integer // the following provides ONLY part of the loop body, which you should complete { cout <<"How many words are in your message? \n"; cout <<"Enter value: "; // get user input integer here    cout <<"\nInvalid value. Please Re-enter a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT