Cuboid and Cylinder

chound 0 Tallied Votes 412 Views Share

This programme can find the dimensions of cuboid and cylinder. In cuboids it can find missing dimensions also. Eg. if length is missing and volume is given it will find the length.

#include<iostream.h>

#define PI 22/7
//#include"cube.cpp"
#include<stdlib.h>

class Cuboid {
	float length; //declaration of dimensions
	float breadth;
	float height;
	float volume;
	float lsa;
	float tsa;
public:
	float fvol(); //declaration of function for finding values
	float ftsa();
	float flsa();
	void setval(float ,float , float);
};

void Cuboid::setval(float d, float e, float f) //function to set values of the cuboid
{
	Cuboid::length = d;
	Cuboid::breadth = e;
	Cuboid::height = f;
}

float Cuboid::fvol() //function to find volume
{
	Cuboid::volume = Cuboid::breadth*Cuboid::height*Cuboid::length;
	return Cuboid::volume;
}

float Cuboid::ftsa() //function to find total surface area
{
	return Cuboid::tsa=2*((Cuboid::breadth*Cuboid::length)+(Cuboid::breadth*Cuboid::height)+(Cuboid::length*Cuboid::height));
}
float Cuboid::flsa() //function to find curved suface area
{
	return Cuboid::lsa=2*((Cuboid::breadth*Cuboid::height)+(Cuboid::height*Cuboid::length));
}

class Cylinder{
	float r, h;//declaring dimesions
	float volume;
	float tsa;
	float csa;
public:
	void csetval(float a, float b);
	float cfvol(){return Cylinder::volume = Cylinder::r*Cylinder::r* Cylinder::h * PI;};
	float cftsa(){return Cylinder::tsa = ((2*PI*Cylinder::r*Cylinder::h)+2*(2*PI*Cylinder::r*Cylinder::r));};
	float cfcsa(){return Cylinder::csa = 2*PI*Cylinder::r*Cylinder::h;};
};

void Cylinder::csetval(float a, float b)
{
	Cylinder::r = a;
	Cylinder::h = b;
}


void cubo();
void cyl();
float *data;
void main()
{
	int choice;
	cout<<"Menu:\n"<<"1)Cuboid\n"<<"2)Cylinder\n"<<endl<<"Enter your choice"<<endl;
	cin>>choice;
	switch(choice)
	{
	case 1:
		cubo();
		break;
	case 2:
		cyl();
		break;
	}
	delete[] data;
}
void cubo()
{
	data = new float[8];
	int d;
	int msg;
	cout<<endl<<"Menu:\n"<<"\n1)l, b, h are given\n2)l is not given\n3)b is not given\n4)h is not given\nEnter your choice: ";
	cin>>msg;
	switch(msg)
	{
	case 1: 
		cout<<"You have selected cube "<<endl;
		Cuboid cube;
		cout<<"Enter l, b, h: "<<endl;
		
		cin>>data[0]>>data[1]>>data[2];
		cube.setval(data[0], data[1], data[2]);
		cout<<"What do you want to find? \n1)Volume\n2)Curved Surface Area\n3)Total Surface Area"<<endl;
		cin>>d;
		switch(d)
		{
		case 1: 
			data[3] = cube.fvol();
			cout<<"The Volume is: "<<data[3]<<"\n";
			break;
		case 2: 
			data[4] = cube.flsa();
			cout<<"The Lateral Surface area is: "<<data[4]<<"\n";
			break;
		case 3: 
			data[5] = cube.ftsa();
			cout<<"The Total Surface Area is: "<<"\n";
			break;
		}
		break;
	case 2:
		cout<<"Enter b, h: ";
		cin>>data[2]>>data[1];
		cout<<"\nWhat is given?\n1)Volume\n2)LSA\n3)TSA\nEnter your choice: "<<endl;
		cin>>d;
		switch(d)
		{
		case 1: 
			cout<<"Enter the Volume: ";
			cin>>data[3];
			data[0]=data[3]/(data[2]*data[1]);
			cout<<endl<<"\nThe length is "<<data[0]<<endl;
			break;
		case 2:
			cout<<"Enter the LSA: ";
			cin>>data[4];
			data[0]=((2*data[2]*data[1])-data[4])/(2*data[1]);
			cout<<"\nThe length is "<<data[0]<<endl;
			break;
		case 3:
			cout<<"Enter the TSA: ";
			cin>>data[5];
			data[0]=((2*data[2]*data[1])-data[5])/(2*(data[2]+data[1]));
			cout<<"\nThe length is: "<<data[0]<<endl;
			break;
		}
	}
}

void cyl() //some more function to be added for cylinder
{
	int choice;
	data = new float[5];
	cout<<"You've selected cylinder"<<endl;
	Cylinder cyl;
	cout<<"Enter radius height: ";
	cin>>data[0]>>data[1];
	cyl.csetval(data[0], data[1]);
	cout<<"\n1)Find volume\n2)Find csa\n3)Find tsa\nEnter choice: ";
	cin>>choice;
	switch(choice)
	{
	case 1: 
		data[2]=cyl.cfvol();
		cout<<"\nThe volume is: "<<data[2]<<endl;
		break;
	case 2:
	data[3]=cyl.cfcsa();
	cout<<"\nThe curved surface area is: "<<data[3]<<endl;
	break;
	case 3:
		data[4]=cyl.cftsa();
		cout<<"\nThe Total Surface area is: "<<data[4]<<endl;
		break;
	}
}

//Please include code for other solides like cones, pyramids also. This is opensource so make modifications.
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.