Categories
Uncategorised

Constructors – Circle

/* Copyright © 2007 Ankur Banerjee. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License version 2.1 as published by the Free Software Foundation. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. http://www.fsf.org/licensing/licenses/lgpl.html /

/ Constructors – Circle /

#include <iostream.h>
#include <conio.h>

class circle
{
float r, a;
public:
circle(float rad) // parameterized constructor
{
r = rad;
a = 3.14 * r * r;
}
circle(circle &c1) // copy constructor
{
r = c1.r + 1;
a = 3.14 * r * r;
}
void disp()
{
cout<<“Radius of circle : “<<r<<endl
<<“Area of circle : “<<a<<endl;
}
} ;

void main()
{
clrscr();
int rad;
cout<<endl<<“Enter radius of circle : “;
cin>>rad;
circle c1(rad); // use of parameterized constructor
c1.disp();
cout<<endl<<“Increasing radius of circle by 1 unit”<<endl;
circle c2(c1); // invoking copy constructor
c2.disp();
getch();
}

/ Output */

Enter radius of circle : 6
Radius of circle : 6
Area of circle : 113.04

Increasing radius of circle by 1 unit
Radius of circle : 7
Area of circle : 153.86

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.