) Rewrite the Circle Class previously defined in lectures, to overload all ecessary operators (+ , - , ++, <<, >> etc..) :) write a main function to define object of the classes and use the override perators.
#include <iostream>
#include "Class_Circle.h"
using namespace std;
static int x = 5;
void setRaduis(int r){
cout<<"I am a function outside the class "<<r<<endl;
}
int main()
{
Circle c1;
c1.setRaduis(10);
c1.printCircle();
setRaduis(20);
Circle c2;
c2.setRaduis(30);
//if (c1==c2) error
bool equals = c1.isEqual(c2);//c = &c2
if (equals)
cout<<"the circles are equals";
else
cout<<"the circles are not equals";
cout << "Hello world!" << endl;
Circle c3(40);
c3.printCircle();
Circle arrayCircle[5];
int r;
for (int i =0;i<5;i++){
cout<<"enter circle "<<i<<" radius: ";
cin>> r;
arrayCircle[i].setRaduis(r);
}
for(int i = 0;i<5;i++){
cout<<"the circle "<<i<<" info :"<<endl;
arrayCircle[i].printCircle();
}
x = 10;
cout<<"static x =" <<x<<endl;
cout<<"call static func from objct:"<<endl;
c1.printNumberOfObjects();
c3.printNumberOfObjects();
arrayCircle[0].printNumberOfObjects;
cout<<"call static fun from class name:"<<endl;
Circle::printNumberOfObjects();
return 0;
}
code circle.h
#include <iostream>
using namespace std;
class Circle{
private:
int raduis;
static int numberOfObjects;
public:
Circle();
Circle(int);
~Circle(){cout<<"destructor called"<<endl;}
int getRaduis() const { return raduis;}
void setRaduis(int);
void printCircle();
double getArea();
double getPerimeter();
bool isEqual(const Circle& );
void static printNumberOfObjects();
};
int Circle::numberOfObjects = 0;
void Circle::printNumberOfObjects(){
cout<<"number of Circle objects is "<<numberOfObjects<<endl;
}
Circle::Circle(){
cout<<"Default constructor called"<<endl;
// raduis = 0;
setRaduis(0);
numberOfObjects++;
}
Circle::Circle(int r){
//raduis = r; bad
setRaduis(r);
numberOfObjects++;
}
bool Circle::isEqual(const Circle& c){
// c.setRaduis(raduis); exposed problem
// Circle c3 = c; //no problem
if (raduis == c.getRaduis())
return true;
else
return false;
}
double Circle::getPerimeter(){
return 3.14 * 2 * raduis;
}
double Circle::getArea(){
double r = 3.14 * raduis * raduis;
return r;
//return 3.14 * pow(raduis,2);
}
void Circle::setRaduis(int r){
if(r>=0)
raduis = r;
else
raduis = 0;
}
void Circle::printCircle(){
cout<<"I am the function inside the class"<<endl;
cout<<"the radius of the class is "<< raduis<<endl;
}
Trending now
This is a popular solution!
Step by step
Solved in 2 steps