Libs.h (libraries and namespaces)
#pragma once
#include <iostream>
using namespace std;
AreaCalc.cpp (main)
#include "Libs.h"
#include "Area.h"
int main()
{
Area obj1(2, 4), obj2(3, 4), obj3;
cout << "The first" << endl;
obj1.print();
cout << endl;
cout << "The second" << endl;
obj2.print();
cout << endl;
cout << "Adding" << endl;
if (!obj3.addarea(obj1, obj2))
{
cerr << "Sizes are not correct" << endl;
return 1;
}
obj3.print();
cout << endl;
}
Area.h (class)
#pragma once
#include "Libs.h"
class Area
{
double height;
double width;
public:
Area(double=0, double=0);
void setdata(double, double);
double areacal()const;
double addarea(const Area&, const Area&);
void print()const;
};
Area.cpp (class realization)
#include "Area.h"
Area::Area(double h, double w)
{
setdata(h, w);
}
void Area::setdata(double h, double w)
{
height = h;
width = w;
}
double Area::areacal()const
{
return height*width;
}
double Area::addarea(const Area& area1, const Area& area2)
{
if (area1.width != area2.width && area1.height != area2.height)
return 0;
else
if (area1.width == area2.width)
{
width = area1.width;
height = area1.height + area2.height;
}
else
{
width = area1.width + area2.width;
height = area1.height;
}
return area1.areacal() + area2.areacal();
}
void Area::print()const
{
cout << "Height: " << height << endl;
cout << "Width: " << width << endl;
cout << "Area: " << areacal() << endl;
}
Results:
Incorrect data (checking):
Comments
Leave a comment