I am very new to programming, and am near the end of this program, but cannot quite finish the last detail, which I have been stuck on. I am attempting to switch what shape pointer *sp is pointing to, and it seems to me that what I am doing should work, since rectangle and circle both are shapes; however, when I compile, only the value of the color changes. The area of the circle prints instead of the area of the rectangle and the perimeter prints 0. Any help would be greatly appreciated!
#include <iostream>
#include <string>
using namespace std;
double const pi = 3.1519;
class shape {
public:
shape() {};
shape(string);
virtual double getCircumference() {
return 0;
};
virtual double getPerimeter() {
return 0;
};
virtual double getArea() {
return 0;
};
string getColor();
protected:
string color;
};
string shape::getColor() {
return color;
}
class circle : public shape {
public:
circle(double r, string c) {
radius = r;
color = c;
};
double getArea();
double getCircumference();
private:
double radius;
};
double circle::getCircumference() {
return pi * radius * 2;
}
double circle::getArea() {
return pi * radius * radius;
}
class rectangle:public shape {
public:
rectangle(double w, double l, string c) {
width = w;
length = l;
color = c;
};
double getArea();
double getPerimeter();
private:
double length;
double width;
};
double rectangle::getPerimeter() {
return width * 2 + length * 2;
}
double rectangle::getArea() {
return length * width;
}
void change(shape *sp, shape *sp1) {
*sp = *sp1;
}
int main() {
circle mary(3.2, "Green");
shape *sp = new circle(4.5, "Yellow");
cout << "Circle #1 is " << mary.getColor() << endl;
cout << "Circle #1 has an area of " << mary.getArea() << endl;
cout << "Circle #1 has a circumference of " << mary.getCircumference() << endl << endl;
cout << "Circle #2 is " << sp->getColor() << endl;
cout << "Circle #2 has an area of " << sp->getArea() << endl;
cout << "Circle #2 has a circumference of " << sp->getCircumference() << endl << endl;
shape *sp1 = new rectangle(1.0, 2.1, "Red");
change(sp, sp1);
cout << "Rectangle #1 is " << sp->getColor() << endl;
cout << "Rectangle #1 has an area of " << sp->getArea() << endl;
cout << "Rectangle #1 has a perimeter of " << sp->getPerimeter() <<endl<< endl;
}
It’s important to keep in mind what is meant by various different ways of using pointers. In your program, sp refers to the pointer itself—that is, a memory location telling the computer where to find an object. The asterisk in *sp is a ‘dereference’ operator; it takes a pointer and gives you the thing that it is pointing to.
With this in mind, your line *sp = *sp1; is saying, ‘take the thing that sp is pointing to, and set it to be equal to the thing that sp1 is pointing to.’ In other words, you are changing the value of the object pointed to by sp, not the value of sp itself. To point sp at the object pointed to by sp1, you need sp = sp1; with no asterisks.
The other thing to bear in mind is that C++ by default passes function arguments by value: when the function is called, the arguments are copied, and the function operates on the copies. This means that the original arguments themselves cannot be changed by a function that works like this. Adding an ampersand to the argument declaration, like void change(shape *&sp, shape *sp1) causes the first argument to be passed by reference: the object operated on by the function is the same object that was passed in by the calling code. This allows the function to change objects passed as arguments, and for those changes to remain after the function has returned.
Sorry for the long answer: I could have given you a few lines that did what you wanted, but I thought you might appreciate an explanation of the reason why things work the way they do.
If you are trying to change the address of pointers, you must pass pointers by reference. Try this:
void change(shape *&sp, shape *&sp1)
A pointer is passed by value even if it's a pointer.
This means that you are actually passing the address by value, so the argument is a copy of the original argument.
Think about
void sum(int a, int b, int result);
void foo() {
int result;
sum(5,10,result);
}
While you expect to be able to store the result into the variable passed to the sum argument you won't be able to do it since result is passed by value and hence copied. Every modification you do to result inside the method will be local to the method.
That's exactly the same thing, a pointer is nothing more than an address, if you pass it by value then a copy of the address is passed but every modification to the local variable is just local.
That's why you must use references if you want to be able to modify their values, exactly as every other variable, so you would have
void sum(int a, int b, int& result);
void change(shape*& shape1, shape*& shape2);
This, under the hood, will pass the address to the variable which stores the address (a sort of shape**) so the function is able to know where the original argument is located and modify it directly.
Related
I am trying to call a function in another class. I need to use the surface area function or the information that was stored in it from the previous class for another class. How would I go about that?
I have already tried HalfOpenCylinder::surfaceArea() and HalfOpenCylinder.surfaceArea() and neither worked.
//surface area function that I want to use for other class
double HalfOpenCylinder::surfaceArea(double height, double pi) {
double surfaceArea = (2 * pi * radius * height) + (pi * pow(radius, 2));
return surfaceArea;
}
To call a function from another class you need to first create an object (instance) of that class.
Using that object you can call a particular function
eg:
#include <iostream>
using namespace std;
class Student
{ // defining class
public:
int id;
void add(){
int x=1;
int y=2;
int z=x+y;
cout<<z<<endl;
}
};
int main() {
Student s1; // creating object of class
s1.id=20;
s1.add() // calling function of that class
return 0;
}
I have written a script that shows you how you can
"call a function in another class"
and
"use the surface area function or the information that was stored in it from the previous class for another class".
This is a script with many examples in it. It uses an updated version of your surfaceArea() method (method being the term since the function is defined from within a class). I have also included what output the script produces at the very bottom of the script.
You can copy and past this entire code segment into a C++ compiler and it should work for you. I compiled and tested it in Visual Studio 2015 Community. I went to new project, and created a "Win32 Console Application" in the C++ category.
// ConsoleApplication10.cpp : Defines the entry point for the console application.
//
// This class example was created to answer kittykoder's question on StackOverflow.
// Both of these need to be included
#include "stdafx.h"
#include <iostream>
// We need the std namespace
using namespace std;
// Here I am defining a struct so that you can easily take all the values out of the
// HalfOpenCylinder class at once, and even make a new Cylinder object with the values by
// using the struct in one of the two HalfOpenCylinder class constructors.
struct CylinderValues
{
public:
CylinderValues(double radius, double height, double surfaceArea) {
this->radius = radius;
this->height = height;
this->surfaceArea = surfaceArea;
}
__readonly double radius;
__readonly double height;
__readonly double surfaceArea;
};
// This is the class I saw in your example. Since it is named
// HalfOpenCylinder, I decided to treat it like an
// instantiatable object class, both because it makes sense name wise,
// and based on the context you provided in your question.
class HalfOpenCylinder
{
public:
// Pi is always 3.14, so there is no reason to make it a passable parameter
// like in your example. Thus I have made it a constant. It's a static constant because
// of the static function I've placed in this class to help in answering your question.
static const float pi;
// I have encapsulated the variables that make up this
// class's objects behind methods so that the surface area can be
// updated every time the radius or height values are changed.
double GetRadius() { return radius; }
void SetRadius(double value) { radius = value; UpdateSurfaceArea(); }
double GetHeight() { return height; }
void SetHeight(double value) { height = value; UpdateSurfaceArea(); }
double GetSurfaceArea() { return surfaceArea; }
// You can make a HalfOpenCylinder object with this constructor
HalfOpenCylinder(double radius, double height) {
this->radius = radius;
this->height = height;
UpdateSurfaceArea();
}
// You can use another HalfOpenCylinder object to make a new HalfOpenCylinder object using
// this constructor.
HalfOpenCylinder(CylinderValues values) {
radius = values.radius;
height = values.height;
surfaceArea = values.surfaceArea;
}
// This will return the struct needed to use the constructor just above this comment.
CylinderValues CopyValues() {
return CylinderValues(radius, height, surfaceArea);
}
// Here is your surface area calculation from your question
static double CalculateSurfaceArea(double radius, double height) {
return (2 * pi * radius * height) + (pi * pow(radius, 2));
}
private:
// Here are the values you wanted to be able to access from another class.
// You can access them using the methods above for getting and setting. The
// surfaceArea value is automatically recalculated if you change either the
// radius or height variable's values.
double radius;
double height;
double surfaceArea;
// This method is here so that HalfOpenCylinder objects can use the
// Surface area calculation. I could have copied and pasted the calculation
// code here to avoid calling the static method, but then I would be writing code
// more than need be. This way, you can update one and the other will be correct.
void UpdateSurfaceArea() {
surfaceArea = CalculateSurfaceArea(radius, height);
}
};
// This is honestly just here because the compiler yelled at me for defining a static
// constant inside a non-static class. Could'a gotten away with it in C#. Thank you compiler.
const float HalfOpenCylinder::pi = 3.141592;
// This is called a function since it is outside of any class (although,
// that is one of the few differences between functions and methods.
// Methods being, functions defined inside classes)
void ThisIsAFunction() {
cout << "This is the text from the function named: ThisIsAFunction";
}
// This class is just here to show you how to call functions and methods from inside classes
class CallFunctionAndMethodTester
{
public:
void MethodInsideTheClass() {
cout << "The below is printed from a function called in a class: \n";
// Here, I am calling a function from inside a class
ThisIsAFunction();
cout << "\n\nThe below is printed from a static method called in a class: \n";
// Here, I am calling a static method from inside a class
cout << HalfOpenCylinder::CalculateSurfaceArea(14.5, 50.5);
// Here, I am making an object instance from inside a class
HalfOpenCylinder bobTheCylinder(1.5, 5.4);
cout << "\n\nThe below is printed from an object's method called in a class: \n";
// Here, I am calling an object's method from inside a class
cout << bobTheCylinder.GetRadius();
}
};
// Ok. We made it. THIS main function is where we will use and
// test the classes we have made above.
int main() {
// Make a new cylinder object. No pointer, so it will be destroyed when the computer
// reads past main (which is the end of this program anyways).
cout << "Cylinder 1 Values: \n";
HalfOpenCylinder cylinder1(5.0, 10.0);
cout << cylinder1.GetRadius();
cout << "\n"; // <--just makin' a newline here
cout << cylinder1.GetHeight();
cout << "\n";
cout << cylinder1.GetSurfaceArea();
cout << "\n\n"; // <--just makin' two newlines here
// Change the object's height. The surface area updates automatically.
cout << "Cylinder 1 new surface area once Height is changed: \n";
cylinder1.SetHeight(20.5);
cout << cylinder1.GetSurfaceArea();
cout << "\n\n";
// Make a second Cylinder using the first cylinder's values.
cout << "Cylinder 2 Values: \n";
HalfOpenCylinder cylinder2(cylinder1.CopyValues());
cout << cylinder2.GetRadius();
cout << "\n";
cout << cylinder2.GetHeight();
cout << "\n";
cout << cylinder2.GetSurfaceArea();
cout << "\n\n";
// Here I'm using the static CalculateSurfaceArea function to use the surface area
// method without having to make a new HalfOpenCylinder object.
cout << HalfOpenCylinder::CalculateSurfaceArea(5.0, 10.0);
cout << "\n\n";
// Here I am making an object of type CallFunctionAndMethodTester so that I can call
// the method inside it that is using my example of how to call functions and methods
// from within classes.
CallFunctionAndMethodTester tester;
cout << "Everything printed to the console after this line is printed using functions and methods that are called from inside classes. \n\n";
tester.MethodInsideTheClass();
int meh;
cin >> meh;
return 0;
}
/* Here is the output of this code when the program runs:
Cylinder 1 Values:
5
10
392.699
Cylinder 1 new surface area once Height is changed:
722.566
Cylinder 2 Values:
5
20.5
722.566
392.699
Everything printed to the console after this line is printed using functions and methods that are called from inside classes.
The below is printed from a function called in a class:
This is the text from the function named: ThisIsAFunction
The below is printed from a static method called in a class:
5261.38
The below is printed from an object's method called in a class:
1.5
*/
I'm trying to get my head around pointers so I have been reading and trying things.
Currently I'm trying to put member functions outside the class, but be able to access a private variable from inside the main function. From what I've read this can't be done, unless I have a pointer inside the class.
So I have tried putting a function that returns a pointer to my private variable inside the class, with all other member functions outside the class.
However when I try to read the private variable I run into compile problems.
I've tried reading other posts and trying their solutions, but I'm just getting more confused.
Ideally, I would like to have a member function named "area" outside the class that is passed a pointer to the private variable "radius" and returnss the area.
This is what I currently have:
#include <iostream>
#define PI 3.14159;
using namespace std;
class Circle{
private:
float radius;
protected:
float *radPtr = &radius;
public:
float getRadiusPtr(){
return *radPtr;
}
void getRadius();
void showRadius();
};
void Circle::getRadius() {
cout << "Enter Radius: "<< endl;
cin >> radius;
}
void Circle::showRadius(){
cout << "Radius: " << endl;
}
float Circle::area(*radPtr){
float ar;
float r = this.getRadiusPtr();
ar = PI * r * r;
}
int main(){
Circle c1;
c1.getRadius();
c1.showRadius();
cout << "Area: " << a << endl;
return 0;
}
You can't use &radius outside a member function, since there's no specific object whose radius member it should get the address of.
Furthermore, your getRadiusPtr() function isn't even returning a pointer. It's just returning the value of radius.
Try:
float *getRadiusPtr() {
return &radius;
}
Then you can use it like:
int main() {
Circle c1;
c1.getRadius();
c1.showRadius();
float *r = c1.getRadiusPtr();
float a = PI * *r * *r;
cout << "Area: " << a << endl;
return 0;
}
DEMO
Hello I have two questions. First, here is some code. I am new to c++. I have to calculate the square of rectangle by the x,y coordinates of upper left and lower right corner - downRightx, upperLeftx, downRighty, upperLefty, the diagonal, and the sides of rectangle. I must make a function print() that calls other private functions only to show the result. Everything is defined inside the class.
class rectangle {
private:
double uLx, uLy, dRx, dRy;
public:
rectangle() {
cout << "enter x coordinate of upper left corner" << uLx;
cout << "enter y coordinate of upper left corner" << uLy;
cout << "enter x coordinate of down right corner" << dRx;
cout << "enter y coordinate of down right corner" << dRy;
}
~rectangle() {
cout << "Deleting object" << endl;
}
private:
void sides() {
double a, b;
a = sqrt(pow((dRx - uLx), 2));
b = sqrt(pow((dRy - uLy), 2));
}
void facediag() {
double s, d;
d = sqrt(pow((dRx - uLx), 2) + pow((dRy - uLy), 2));
---- 1. //here must be the calculation of square s = a*b
}
public:
void print() {
--- 2. //here I must print the results
}
};
so the question is: How to call a and b parameters from side in facediag() function to calculate s = a*b And how to print the results. Can I write cout << a; cout << d; cout << s, etc. in sides() and facediag() and just call them in print? Or can I print them in print() without writing cout << ... in other functions, but otherwise, another access method.
void facediag(){
//code
cout << s;
cout << d;
}
void sides(){
// code
cout << a;
cout << b;
}
void print()
{
sides();
facediag();
}// not like this, is there another way?
Second question I let Cygwin to be installed at its complete form and at some point I realized that I will run out of hdd and the installation hangs, so I interrupted the installation. How can I uninstall it - just delete the folder or to step through the FAQ in the Cygwin site?
You cannot access local variables from other functions. They only exist while that function executes.
What you can do is define more member functions that compute the values you need, like
double height() const
{ return /* something */; }
double width() const
{ return /* something else */; }
and use those functions where you need a or b.
You CAN call the private function in your OWN class.
But, the variable a and b are local variables, so they will not exist out of the function sides(), you can do the same thing in function facediag() to calculate a and b
Here is one program taken from a textbook featuring copy constructors:
#include <stdio.h>
#include <conio.h>
#include <iostream>
using namespace std;
class point
{
private:
int x,y;
public:
point(int ox =0, int oy =0)
{
cout << " Make object" << this << endl;
cout << " Using default constructor \n";
x = ox, y = oy;
}
point(const point &p)
{
cout << " Make object" << this << endl;
cout << " Using copy constructor \n";
x = p.x, y = p.y;
}
void move(int dx, int dy);
void display();
};
point fct(point a);
int main()
{
point a(5,2);
a.display();
point b = fct (a);
b.display();
getch();
return 0;
}
void point::move(int dx, int dy)
{
x += dx, y += dy;
}
void point::display()
{
cout << "Coordinates :" << x << " " << y << "\n";
}
point fct(point a)
{
point b=a;
//b.move(2,3);
return b;
}
It should be noted that the copy constructor is of the form : point (const point &p) instead of point (point &p) (the latter is what's in the textbook, but couldn't compile so I had to switch to the first one, but still I can't understand why :( )
The textbook said that there will be 3 lines of "Using copy constructor" , corresponding to 3 calls to the copy constructor. I think the reason for that is, when you call: b = fct(a)
The function "fct" makes a copy of a (pass-by-value), therefor one call
The line : point b = a : again, a copy constructor is invoked
The return value is then copied into b (the variable in main, not the one in fct) this is 3rd one.
However upon execution, there is only 2 calls. Can anyone give me a good explaination on this?
Two copies occur because Named Return Value Optimization (NRVO) elides one of the copies.
point fct(point a)
{
point b=a;
return b;
}
point b = fct (a);
The first copy is the one from the argument a in main to the parameter a in fct. This occurs because the point is taken by-value.
The second copy is from a to b inside func.
The copy which is elided is the one in returning b by value. In this case, b can be directly allocated into the b at the call site, so no copy takes place.
I'm currently working on my C++ assignment and I'm working through polymorphism and I keep getting error messages. Would appreciate any help. Thanks in advance!
The assignment uses a shape inheritance hierarchy that looks like this:
Circle
Two Dimensional Shape
Cylnder
Shape
Three Dimensional Shape
Below is the error messages I'm getting.
1>Circle.obj : error LNK2019: unresolved external symbol "public: __thiscall TwoDimensionalShapes::TwoDimensionalShapes(void)" (??0TwoDimensionalShapes##QAE#XZ) referenced in function "public: __thiscall Circle::Circle(void)" (??0Circle##QAE#XZ)
1>Cylinder.obj : error LNK2019: unresolved external symbol "public: __thiscall ThreeDimensionalShapes::ThreeDimensionalShapes(void)" (??0ThreeDimensionalShapes##QAE#XZ) referenced in function "public: __thiscall Cylinder::Cylinder(void)" (??0Cylinder##QAE#XZ)
I'm pretty much done with my program but I'm not sure where the problems are coming from.
Here's my source code:
#include "Cylinder.h"
#include "Circle.h"
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
// next line commented out because will now cause compile error
// Shape shpeObj; // instantiate a Shape object
cout << "The Shape object count is: " << Shape::getObjectCount() << '\n';
Circle crclObj; // instantiate a Circle object
Cylinder cyldObj; // instantiate a Cylinder object
// Count will be 2 in statement below because a Shape object is contained
// within each Circle and Cylinder object.
cout << "The Shape object count is: " << Shape::getObjectCount() << "\n";
Shape * shpePtr = &cyldObj; // declare a Shape pointer
// and have it point to the Cylinder object
Shape & shpeRef = cyldObj; // declare a Shape reference
// and have it reference the Cylinder object
// The above 2 statments are ok because a derived class object IsA base class object!
// No additional objects created, so the count is still the same.
cout << "The Shape object count is: " << Shape::getObjectCount() << "\n";
// Dynamically create 2 objects
Shape * shpePtr2 = new Circle;
Shape & shpeRef2 = *new Cylinder;
// The count should now be 4
cout << "The Shape object count is: " << Shape::getObjectCount() << "\n";
// Now destroy the 2 dynamically created objects!
delete shpePtr2;
delete &shpeRef2;
//The count should now be 2 again.
cout << "The Shape object count is: " << Shape::getObjectCount() << "\n\n";
/* Can no longer test Shape class, since it is now an Abstract Base Class (ABC)
// Test Shape class
shpeObj.setNoOfSides(0);
cout << "The number of sides is: " << shpeObj.getNoOfSides() << "\n\n";
cout << "The area of shpeObj is: " << shpeObj.Area() << '\n';
cout << "The volume of shpeObj is: " << shpeObj.Volume() << "\n\n\n";
*/
// Test Circle class
crclObj.setRadius(3.0);
cout << fixed << setprecision(4); // force use of decimal point and 4 digits of
// precision after the decimal place
cout << "The radius of crclObj is: " << crclObj.getRadius() << "\n\n";
cout << "The number of sides is: " << crclObj.getNoOfSides() << "\n\n";
cout << "The area of crclObj is: " << crclObj.Area() << '\n';
// next line no longer valid in my solution for this assignment
//cout << "The volume of crclObj is: " << crclObj.Volume() << "\n\n\n";
// Test Cylinder class
cyldObj.setRadius(5.5);
cyldObj.setHeight(2.5);
cout << "The radius of cyldObj is: " << cyldObj.getRadius() << '\n';
cout << "The height of cyldObj is: " << cyldObj.getHeight() << "\n\n";
cout << "The number of sides is: " << cyldObj.getNoOfSides() << "\n\n";
cout << "The area of cyldObj is: " << cyldObj.Area() << '\n';
cout << "The volume of cyldObj is: " << cyldObj.Volume() << "\n\n";
return 0;
}
#pragma once
class Shape
{
public:
Shape(void); //constructor
~Shape(void); //destructor
void setNoOfSides(const int &); // set the # of sides
int getNoOfSides() const; // get the # of sides
static int getObjectCount(); // get the object count
virtual double Area() const = 0; // calculate and return area
// now a pure virtual function
virtual double Volume() const; // calculate and return volume
protected:
int mNoOfSides; // represents # of sides in Shape object
static int mObjectCount; // a static member - counts the # of Shape
// objects currently instantiated
// Only one of these instantiated for the whole class!
};
#pragma once
#include "Shape.h"
class TwoDimensionalShapes :public Shape
{
public:
TwoDimensionalShapes(void); //constructor
virtual double Area() const = 0; // area of 2D shape
};
#pragma once
#include "Shape.h"
class ThreeDimensionalShapes :public Shape
{
public:
ThreeDimensionalShapes(void); // constructor
virtual double Area() const = 0; // area of 3D shape
virtual double Volume() const = 0; // volume of 3D shape
};
#pragma once
#include "TwoDimensionalShapes.h"
class Circle :public TwoDimensionalShapes
{
public:
Circle(void); // constructor
void setRadius(const double &); //set the radius
double getRadius() const; // get the radius
virtual double Area() const override; // overrides Area() method of TwoDimensionalShapes class
protected:
const static double pi; // Static member used in calculations
// Only one of these instantiated for the whole class!
double mRadius; // member used to represent radius
};
#pragma once
#include "ThreeDimensionalShapes.h"
class Cylinder :public ThreeDimensionalShapes
{
public:
Cylinder(void); // constructor
void setHeight(const double &); // set the height
double getHeight() const; // get the height
void setRadius(const double &); //set the radius
double getRadius() const; // get the radius
virtual double Area() const override; // overrides Area() method of ThreeDimensionalShapes class
virtual double Volume() const override; // overrides Volume() method of ThreeDimensionalShapes class
protected:
const static double pi; // Static member used in calculations
// Only one of these instantiated for the whole class!
double mHeight; // member used to represent height
double mRadius;
};
#include "Circle.h"
// init static data member
const double Circle::pi = 3.141592654; // init. static member
// constructor
Circle::Circle(void)
:mRadius(0.0)
{
setNoOfSides(0);
}
// used to set value for mRadius member
void Circle::setRadius(const double & setVal)
{
if (setVal > 0.0) // Make sure input is a valid value
{
this->mRadius = setVal;
}
// otherwise just leave set to original value
}
// used to return current value of mRadius member
double Circle::getRadius(void) const
{
return this->mRadius;
}
// used top calculate and return area.
double Circle::Area(void) const
{
return Circle::pi * this->mRadius * this->mRadius;
}
#include "Cylinder.h"
// init static data member
const double Cylinder::pi = 3.141592654; // init. static member
// constructor
Cylinder::Cylinder(void)
:mHeight(0.0)
{
this->setNoOfSides(3); // Why not init. this member in MIL ???
}
// used to set mHeight member
void Cylinder::setHeight(const double & setVal)
{
if (setVal > 0.0) // Make sure input is a valid value
{
this->mHeight = setVal;
}
// otherwise just leave set to original value
}
// used to return current value of mHeight member
double Cylinder::getHeight(void) const
{
return this->mHeight;
}
// used to set value for mRadius member
void Cylinder::setRadius(const double & setVal)
{
if (setVal > 0.0) // Make sure input is a valid value
{
this->mRadius = setVal;
}
// otherwise just leave set to original value
}
// used to return current value of mRadius member
double Cylinder::getRadius(void) const
{
return this->mRadius;
}
// used to caluclate and return area
double Cylinder::Area(void) const
{
double TwoPiR = 2.0 * Cylinder::pi * this->mRadius;
return (TwoPiR * this->mRadius) + (TwoPiR * this->mHeight);
}
// used to claculate and return volume
double Cylinder::Volume(void) const
{
return Cylinder::pi * this->mRadius * this->mRadius * this->mHeight;
}
#include "Shape.h"
// init static data memeber
int Shape::mObjectCount = 0;
// constructor
Shape::Shape(void)
:mNoOfSides(1)
{
++Shape::mObjectCount;
}
// desstructor
Shape::~Shape(void)
{
--Shape::mObjectCount;
}
// used to set mNoOfSides member
void Shape::setNoOfSides(const int & setVal)
{
if (setVal > 0)
{
this->mNoOfSides = setVal;
}
// otherwise just leave set to original value
}
// used to return current value of mNoOfSides member
int Shape::getNoOfSides() const
{
return this->mNoOfSides;
}
// used to return current value of mObjectCount static member
int Shape::getObjectCount()
{
return Shape::mObjectCount;
}
/* no longer required to be implemented now that it is a
pure virtual function
// used to calculate and return area
double Shape::Area(void) const
{
return 0.0;
}
*/
// used to calculate and return volume
double Shape::Volume(void) const
{
return 0.0;
}
OK I figured it out now. I forgot to provide a constructor implementation for my TwoDimensionalShapes and ThreeDimensionalShapes classes. Silly mistake.