It 218 Week 6 Assignment

Only available on StudyMode
  • Topic: Subroutine, C++, Java
  • Pages : 3 (427 words )
  • Download(s) : 266
  • Published : April 17, 2011
Open Document
Text Preview
//Creating and using boxes
#include <iostream>
using std::cout;
using std::endl;

class CBox//Class definition
double m_Length;//Length of box in inches
double m_Width;//Width of box in inches
double m_Height;//Height of box in inches

//Function to calculate volume of boxes
double Volume();
Return m_Length*m_Width*m_Height;

int main()
CBox box1;//Declare box 1 of type CBox
CBox box2;//Declare box 2 of type CBox
CBox box3;//Declare box 3 of type CBox

double boxVolume=0.0;//Stores box volume

box1.m_Height=18.0;//Define values
box1.m_Length=78.0;//of of the members of
box1.m_Width=24.0;//the object box1

box2.m_Height= box1.m_Height-10;//Define
box2.m_Length= box1.m_Length/2.0;//members
box2.m_Width= 0.25*box1.m_Length;

box3.m_Height= box1.m_Height-15; //Define
box3.m_Length= box1.m_Length/2.5 //members
box3.m_Width= 0.35*box1.m_Length;

//Calculate volume of box1
boxVolume= box1.Volume();

<<”Volume of box1 =”<<boxVolume;

//Calculate volume of box2
<<”Volume of box2=”

//Calculate volume of box3
<<”Volume of box3=”

<<”box3 has sides which total”

cout<< endl
<<”A CBox object occupies”
<<sizeof box1<<”bytes”;

cout <<endl
return 0;


Under what circumstances must you use classes?

Classes are useful for length programs which require large amounts of similar code. Classes can accelerate program development by reducing redundant code, testing, and debugging. Using classes also makes the code easier to read and follow for other programmers.

Why do you use a class instead of a structure? Are they interchangeable? Explain your answer.

tracking img