GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 25-Apr-2005, 18:05
Bravebird Bravebird is offline
New Member
 
Join Date: Apr 2005
Location: Ga, USA
Posts: 5
Bravebird is on a distinguished road
Question

Assistance with classes...


Hello all...

I was given the following project to complete:

Write the declarations and definitions for the geometric classes defined in Figure 12-21. Use the following hints:
a.Define Shape, Two-Dimensional and Three-Dimensional as abstract classes.
b.Define PI as static class member in the Shape class.
c.Define area calculation and print functions as pure virtual functions in the Shape class.
d.Use public inheritance.
e.Define all data or member functions common to two-dimensional shapes in the Two-dimensional class. Do the same for all common data or member functions for the three-dimensional shape.
Figure 12-21
Shape:
-Two Dimensional
Triangle
Rectangle
Circle
-Three Dimensional
Cone
Cube
Sphere

I have a clear understanding of what to do with the two dimensional class...its the three dimensional class I'm having trouble with. If there is anyone that can provide me with some direction I would really appreciate.

Thanks!
  #2  
Old 25-Apr-2005, 19:23
rdrast rdrast is offline
New Member
 
Join Date: Apr 2005
Posts: 22
rdrast is on a distinguished road
What's the difficulty?

Two dimensional is area, square inches, square feet, square meters...
Three dimensional is volume. Cubic inches, cubic centimeters, etc.

Maybe your first step would be to write out eh formulas and characteristics of each shape?

(example)
Triangle:
Sides - 3
Enclosed angle - 180 degrees
Primary measurements: Base * height
Area calculation: 1/2 * base * height
  #3  
Old 25-Apr-2005, 19:47
Bravebird Bravebird is offline
New Member
 
Join Date: Apr 2005
Location: Ga, USA
Posts: 5
Bravebird is on a distinguished road
mhmm...that makes alot more sense...create an algorithm.

Thanks!
  #4  
Old 27-Apr-2005, 11:24
Bravebird Bravebird is offline
New Member
 
Join Date: Apr 2005
Location: Ga, USA
Posts: 5
Bravebird is on a distinguished road
Question

Errors...


Ok, I am now running into three errors now that I have compiled the program...I don't know what they mean though. Heres the program:

CPP / C++ / C Code:
//___________

#include<iostream>
#include<math.h>
using namespace std;
class shape
{
protected:
	double area;
	virtual void calcArea() = 0;
	virtual void print() = 0;
public:
	static float PI;
};


class two_dim : public shape
{
protected:
	double perimeter;
	virtual void calcPeri() = 0;
};


class Rectangle : public two_dim
{
private:
	double sideA;
	double sideB;
public:
	Rectangle(double sideAIn, double sideBIn);
	virtual void calcPeri();
	virtual void print();
	virtual void calcArea();
};


class three_dim : public shape
{
protected:
	double volume;
	virtual void calcVol() = 0;
};


class Cube : public three_dim
{
private:
	double sideLen;
	double surfaceArea;
	int a;
public:
	Cube(double sideIn, int a);
	virtual void calcVol();
	virtual void print();
	virtual void calcsurfaceArea();
};

Rectangle :: Rectangle (double sideAIn, double sideBIn)
{
	sideA = sideAIn;
	sideB = sideBIn;
	calcPeri ();
	calcArea ();
}

void Rectangle::calcPeri()
{
	perimeter = 2 * + (sideA + sideB);
	return;
}

void Rectangle::calcArea()
{
	area = sideA * sideB;
	return;
}

void Rectangle :: print ()
{
	cout << "\nA rectangle with sides of " << sideA << ", and " << sideB << " will have:\n\nPerimeter = " << perimeter << "\nArea = " << area << "\n\n";
	return;
}

Cube :: Cube (double sideIn, int a)
{
	sideLen = sideIn;
}

void Cube::calcVol()
{
	volume = pow(sideLen,3); 
	return;
}

void Cube::calcsurfaceArea ()
{
	surfaceArea = 6 * a ^ 2;
	return;
}

void Cube :: print ()
{
	cout << "\n\nA cube with sides equal to " << sideLen << " will have:\n\nVolume = " 
		 << volume << "\n\n";
	cout << "\n\nThe surface area is equal to: "<< surfaceArea << "\n\n";
	return;
}

int main()
{
	Rectangle rec(4,6);
	rec.calcPeri();
	rec.calcArea();
	rec.print ();
	
	Cube cub(3,7);
	cub.calcVol();
	cub.calCsurfaceArea;
	cub.print();

	return 0;
}

I believe that it has something to do with the integer "a" that I implemented...but I don't know how do go about correcting it.

Can anyone help?
Last edited by LuciWiz : 28-Apr-2005 at 17:59. Reason: Please insert your C++ code between [c++] & [/c++] tags
  #5  
Old 27-Apr-2005, 12:33
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,335
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Read the sticky. Read the sticky. Read the sticky.

It's not hard to find. It's at the top of the forum with the useful title
Guidelines for posting requests for help
__________________

During the election they said Obama could only be elected when pigs fly. Well, we currently have an epidemic of Swine Flu. Coincidence?
  #6  
Old 27-Apr-2005, 13:28
Bravebird Bravebird is offline
New Member
 
Join Date: Apr 2005
Location: Ga, USA
Posts: 5
Bravebird is on a distinguished road

I apologize...


Quote:
Originally Posted by WaltP
Read the sticky. Read the sticky. Read the sticky.

It's not hard to find. It's at the top of the forum with the useful title
Guidelines for posting requests for help

CPP / C++ / C Code:
//___________

#include<iostream>
#include<math.h>
using namespace std;
class shape
{
protected:
double area;
virtual void calcArea() = 0;
virtual void print() = 0;
public:
static float PI;
};


class two_dim : public shape
{
protected:
double perimeter;
virtual void calcPeri() = 0;
};


class Rectangle : public two_dim
{
private:
double sideA;
double sideB;
public:
Rectangle(double sideAIn, double sideBIn);
virtual void calcPeri();
virtual void print();
virtual void calcArea();
};


class three_dim : public shape
{
protected:
double volume;
virtual void calcVol() = 0;
};


class Cube : public three_dim
{
private:
double sideLen;
double surfaceArea;
int a;
public:
Cube(double sideIn, int a);
virtual void calcVol();
virtual void print();
virtual void calcsurfaceArea();
};

Rectangle :: Rectangle (double sideAIn, double sideBIn)
{
sideA = sideAIn;
sideB = sideBIn;
calcPeri ();
calcArea ();
}

void Rectangle::calcPeri()
{
perimeter = 2 * + (sideA + sideB);
return;
}

void Rectangle::calcArea()
{
area = sideA * sideB;
return;
}

void Rectangle :: print ()
{
cout << "\nA rectangle with sides of " << sideA << ", and " << sideB << " will have:\n\nPerimeter = " << perimeter << "\nArea = " << area << "\n\n";
return;
}

Cube :: Cube (double sideIn, int a)
{
sideLen = sideIn;
}

void Cube::calcVol()
{
volume = pow(sideLen,3); 
return;
}

void Cube::calcsurfaceArea ()
{
surfaceArea = 6 * a ^ 2;
return;
}

void Cube :: print ()
{
cout << "\n\nA cube with sides equal to " << sideLen << " will have:\n\nVolume = " 
<< volume << "\n\n";
cout << "\n\nThe surface area is equal to: "<< surfaceArea << "\n\n";
return;
}

int main()
{
Rectangle rec(4,6);
rec.calcPeri();
rec.calcArea();
rec.print ();

Cube cub(3,7);
cub.calcVol();
cub.calcsurfaceArea;
cub.print();

return 0;
}
  #7  
Old 27-Apr-2005, 13:56
Dave Sinkula Dave Sinkula is offline
Member
 
Join Date: Apr 2005
Posts: 199
Dave Sinkula will become famous soon enough
This is not my strong suit, but I'll give it a whirl.

http://www.parashift.com/c++-faq-lit....html#faq-22.4

Your Cube is a public three_dim, which has a pure virtual function calcVol; so Cube must define one. You do that, okay.

Your three_dim is a public shape, which has pure virtual functions calcArea and print; so you must define them for Cube as well. You don't: problem.

So if you add these to Cube:
CPP / C++ / C Code:
virtual void print();
virtual void calcArea();
And change this...
CPP / C++ / C Code:
void Cube::calcsurfaceArea () // note name
{
surfaceArea = 6 * a ^ 2; // I doubt you are trying to XOR a with 2
return;
}
...to this
CPP / C++ / C Code:
void Cube::calcArea () // see the name
{
   surfaceArea = 6 * pow(sideLen, 2); // you want sideLen squared, right?
   return;
}
And then instead of attempting to call the function like this...
CPP / C++ / C Code:
cub.calcsurfaceArea;
...you call the function like this
CPP / C++ / C Code:
cub.calcArea();
Then I think some of the compile issues are taken care of.
  #8  
Old 27-Apr-2005, 14:17
Bravebird Bravebird is offline
New Member
 
Join Date: Apr 2005
Location: Ga, USA
Posts: 5
Bravebird is on a distinguished road

Thanks!


Thank you soo much! That helped alot...as well as the link.

I appreciate it!

Quote:
Originally Posted by Dave Sinkula
This is not my strong suit, but I'll give it a whirl.

http://www.parashift.com/c++-faq-lite/abcs.html#faq-22.4

Your Cube is a public three_dim, which has a pure virtual function calcVol; so Cube must define one. You do that, okay.

Your three_dim is a public shape, which has pure virtual functions calcArea and print; so you must define them for Cube as well. You don't: problem.

So if you add these to Cube:
CPP / C++ / C Code:
virtual void print();
virtual void calcArea();
And change this...
CPP / C++ / C Code:
void Cube::calcsurfaceArea () // note name
{
surfaceArea = 6 * a ^ 2; // I doubt you are trying to XOR a with 2
return;
}
...to this
CPP / C++ / C Code:
void Cube::calcArea () // see the name
{
   surfaceArea = 6 * pow(sideLen, 2); // you want sideLen squared, right?
   return;
}
And then instead of attempting to call the function like this...
CPP / C++ / C Code:
cub.calcsurfaceArea;
...you call the function like this
CPP / C++ / C Code:
cub.calcArea();
Then I think some of the compile issues are taken care of.
 
 

Recent GIDBlogProblems with the Navy (Chiefs) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Help using const arrays in c++ classes FlipNode C++ Forum 5 31-Mar-2005 02:16
bug w/ templated classes f15e MS Visual C++ / MFC Forum 4 07-Mar-2005 00:56
Fairly simple classes help please sammacs C++ Forum 0 30-Nov-2004 10:58
ClassView not showing my classes (VC++, namespace, headers) ?!?!? djovanov C++ Forum 1 13-Jan-2004 05:54
using windows WMI and CMI classes in Dev-c++ Dawis C++ Forum 0 26-Oct-2003 05:18

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 04:47.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.