Answer to Question #130602 in C++ for Ashok

Question #130602
Define class "Area" with members height and width. Also define functions to setdata() to initialize the members, print() to display values, areacal() to calculate area and addarea() to add two area; justify your asnwere that adding of two area is equal to area after adding total hight and width, using concept of passing object as argument.
1
Expert's answer
2020-08-25T15:16:11-0400

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):


Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS