Showing posts with label Constructor. Show all posts
Showing posts with label Constructor. Show all posts

Saturday, May 31, 2014

C++ Program which displays the radius ,area and color of the circle using constructor

#include <iostream>   
#include<conio.h>
#include <string>      // using string
using namespace std;

class Circle {
private:
double radius;      // Data member (Variable)
string color;       // Data member (Variable)

public:
   // Constructor with default values for data members
Circle(double r , string c ) {
radius = r;
color = c;
   }

double getRadius() {  // Member function (Getter)
return radius;
   }

string getColor() {   // Member function (Getter)
return color;
   }

double getArea() {    // Member function
return radius*radius*3.1416;
   }
};   // need to end the class declaration with a semi-colon
 // Test driver function
int main() {
   // Construct a Circle instance
   Circle c1(1.2, "blue");
cout<< "Radius=" << c1.getRadius() << " Area=" << c1.getArea()
<< " Color=" <<c1.getColor() <<endl;

   // Construct another Circle instance
   Circle c2(3.4,"Red"); // default color
cout<< "Radius=" << c2.getRadius() << " Area=" << c2.getArea()
<< " Color=" <<c2.getColor() <<endl;

   // Construct a Circle instance using default no-arg constructor
   Circle c3(5,"Yellow");      // default radius and color
cout<< "Radius=" << c3.getRadius() << " Area=" << c3.getArea()
<< " Color=" <<c3.getColor() <<endl;
getch();
return 0;
}