Assignments and GDBs
CS201 C++ Assignmnet Solution 1 (13-06-2011)
- Details
- Published on Tuesday, 02 August 2011 16:38
- Written by Gulshan Ali
Semester: Spring 2011
CS201: Introduction to Programming
Problem Statement: Circle Area & Circumference Calculation
You are required to write a program which will calculate the area and circumference of the circle by declaring a class. You must declare a class named Circle whose only data member will be radius which should be declared as public. You need to define a macro for the value of PI which will be used while calculating the area of the circle.
Detailed Description:
Data member radius should be of type double and it should be declared under public scope.
- Value for PI should be defined using #define macro.
Your class should have two type of constructors:
1. Default Constructor which should initialize radius of the circle with value 0.
2. User Defined Constructor which takes class data member radius as its parameter.
- Declare a public member function named CalculateArea() which calculates the area of the circle. The formula for calculating the area is.
Declare a public member function named CalculateCircum() which calculates the circumference of the circle. The formula for calculating the circumference is.
Declare a public member function named Display which will display the calculated area and circumference of the circle on the output screen.
In the main() function, declare two objects of type Circle:
One using default constructor
- Second using user-defined constructor.
It should be Display the area and circumference of the circle on the screen like the sample output.
Sample Output:
Radius of the Circle = 0
Area of the Circle = 0
Circumference of the Circle is = 0
Radius of the Circle = 3.5
Area of the Circle = 38.465
Circumference of the Circle is = 21.98
Solution
/* Solution Assignment No. 3, Spring 2011 */
// This program calculate the area and circumference of the circle by declaring a class
// Including header files in the program.
#include
using namespace std;
// defining a macro for the value of PI
#define PI 3.14
// Beginning of the class Circle
class Circle {
// declaring public members of the class
public:
double radius; // only data member of the class
// declaration of public member functions of the class
Circle(); // default constructor
Circle(double); // Parameterized constructor
double CalculateArea(); // calculates area of the circle and then returns
double CalculateCircum();// calculates circumference of the circle
void Display(); // displays the area and circumference of the screen
};
// end of class
// definition of default constructor of class
Circle::Circle()
{
radius = 0.0; // assignning default value to the data member radius
}
// definition of parameterized constructor of the class
Circle::Circle(double rad)
{
radius = rad;
}
// definition of the function for area calculation of the circle
double Circle::CalculateArea()
{
return (PI * radius * radius); // calculating area and then returning it the caller function
}
// definition of the function for calculating circumference of the circle
double Circle::CalculateCircum()
{
return ( 2 * PI * radius ); // calculating and returning the circumferencr to the caller function
}
// defintion of display() function
void Circle::Display()
{
cout
