im doing this project to work it prints out all information except the radius it just gives 0,0 but needs to give locations provided in code.

#ifndef SHAPE_H
#define SHAPE_H

#include <iostream>
using std::ostream;


class Shape
{
	friend ostream& operator<<(ostream &out,Shape &s);
public:
	Shape(double = 0.0,double = 0.0);//default constructor
	double getCenterX()const;//function prototype
	double getCenterY()const;//function prototype
	virtual void print() const = 0;//pure virtual function


protected:
	double centerX;//data member
	double centerY;//data member


};

#endif
#include "stdafx.h"
#include "Shape.h"


Shape::Shape(double x,double y)
{
	centerX = x;
	centerY = y;
}

double Shape::getCenterX()const
{
	return centerX;
}

double Shape::getCenterY()const
{
	return centerY;
}
#ifndef TWODIMENSIONALSHAPE_H
#define TWODIMENSIONALSHAPE_H

#include "Shape.h"

class TwoDimensionalShape: public Shape
   
{
public:
	TwoDimensionalShape(double x,double y)
	{
	double Shape(double x,double y);
	}
	virtual double getArea()const = 0;//pure virtual function
	
};
#endif
#ifndef THREEDIMENSIONALSHAPE_H
#define THREEDIMENSIONALSHAPE_H

#include "Shape.h"

class ThreeDimensionalShape:public Shape
	
{
public:
	ThreeDimensionalShape(double x,double y)
	{
		double Shape(double x,double y);
	}
	virtual double getArea()const = 0;//pure virtual function
	virtual double getVolume()const = 0;//pure virtual function
	
};

#endif
#ifndef CIRCLE_H
#define CIRCLE_H

#include "TwoDimensionalShape.h"
class Circle: public TwoDimensionalShape	
{
public:
	
	Circle(double = 0.0,double = 0.0,double = 0.0);//constructor
	double getRadius() const;// virtual function
    double getArea() const;// virtual function
	void print()const;//function prototype

private:
	double radius;//data member
};

#endif
#include "stdafx.h"
#include <iostream>
#include "Circle.h"
using namespace std;


Circle::Circle(double r,double x,double y): TwoDimensionalShape(x,y)
{
	radius = r;
}
	
double Circle::getArea()const
{
	return 3.14159 * radius * radius;
}


void Circle::print()const
{
	cout<<"Circle with radius "<<getRadius()<<"; "<<"center at ("<<getCenterX()<<", "<<getCenterY()<<")";
	
}

double Circle::getRadius() const
{
	return radius;
}
#ifndef SQUARE_H
#define SQUARE_H
#include "TwoDimensionalShape.h"

class Square: public TwoDimensionalShape
{
public:
	
	Square(double = 0.0,double = 0.0,double = 0.0);//constructor
	virtual double getSideLength()const;//virtual function
	virtual double getArea()const;//virtual function
	void print()const;//prototype for print function

private:
	double sideLength;//data member
};

#endif
#include "stdafx.h"
#include <iostream>
#include "Square.h"
using std::cout;
using namespace std;


Square::Square(double s,double x,double y) : TwoDimensionalShape(x,y)
{
	sideLength = s;
}

double Square::getSideLength()const
{
	return sideLength;
}
double Square::getArea()const
{
	return sideLength * sideLength;
}

	

void Square::print()const//print results
{
	cout<<"Square with side length "<<getSideLength()<<"; "<<"center at ("<<centerX<<", "<<centerY<<")";
	
}
#ifndef SPHERE_H
#define SPHERE_H
#include "ThreeDimensionalShape.h"

class Sphere: public ThreeDimensionalShape
{
public:
	Sphere(double = 0.0,double = 0.0,double = 0.0);//constructor
	virtual double getArea()const;//virtual function
	virtual double getVolume()const;//virtual function
	double getRadius();//function prototype
	void print()const;//function prototype

private:
	double radius;//data member

};

#endif
#include "stdafx.h"
#include <iostream>
#include "Sphere.h"
using namespace std;



Sphere::Sphere(double r,double x, double y) : ThreeDimensionalShape(x,y)
{
	radius = r;
}
double Sphere::getArea()const
{
	return 4.0 * 3.14159 * radius * radius;
}
double Sphere::getVolume()const
{
	return (4.0/3.0) * 3.14159 * radius * radius * radius;
}
double Sphere::getRadius()
{
	return radius;
}



void Sphere::print()const//print results
{
	cout<<"Sphere with radius "<<radius<<"; "<<"center at ("<<centerX<<", "<<centerY<<")";
	
}
#ifndef CUBE_H
#define CUBE_H
#include "ThreeDimensionalShape.h"

class Cube: public ThreeDimensionalShape
{
public:
	Cube(double = 0.0,double = 0.0,double = 0.0);
	virtual double getArea()const;// virtual function
	virtual double getVolume()const;//virtual function
	double getSideLength()const;//function prototype
	void print()const;//function prototype

private:
	double sideLength;//data member

};

#endif
#include "stdafx.h"
#include <iostream>
#include "Cube.h"
using namespace std;

Cube::Cube(double s,double x, double y) : ThreeDimensionalShape(x,y)
{
	sideLength = s;
}

double Cube::getSideLength()const
{
	return sideLength;
}
double Cube::getArea ()const
{
	return 6 * sideLength * sideLength;
}
double Cube::getVolume()const
{
	return sideLength * sideLength * sideLength;
}


void Cube::print()const
{
	cout<<"Cube with side legnth "<<sideLength<<" ; center at ("<<centerX<<", "<<centerY<<")";
    
}
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <typeinfo>
#include "Shape.h"
#include "TwoDimensionalShape.h"
#include "ThreeDimensionalShape.h"
#include "Circle.h"
#include "Square.h"
#include "Sphere.h"
#include "Cube.h"
using std::cout;
using std::endl;
using std::vector;



int _tmain(int argc, _TCHAR* argv[])
{
	vector<Shape*>shapes(4);
	shapes[0] = new Circle(3.5,6.0,9.0);
	shapes[1] = new Square(12,2.0,2.0);
	shapes[2] = new Sphere(5,1.5,4.5);
	shapes[3] = new Cube(2.2);

	for(int i = 0; i < 4; i++)
	{
		cout<< *(shapes[i])<<endl;
		 
	//downcast pointer
	TwoDimensionalShape *twoDimensionalShapePtr = dynamic_cast<TwoDimensionalShape *> (shapes[i]);

	//if shape is a TwoDimensionalShape display its area
	if(twoDimensionalShapePtr != 0)
		cout<<"Area: "<<twoDimensionalShapePtr->getArea()<<endl<<endl;;
	//downcast pointer
	ThreeDimensionalShape *threeDimensionalShapePtr = dynamic_cast <ThreeDimensionalShape *> (shapes[i]);
	//if Shape is a ThreeDimensionalShape display its area and volume
	if(threeDimensionalShapePtr != 0)
		cout<<"Area: "<<threeDimensionalShapePtr->getArea()<<"\nVolume: "<<threeDimensionalShapePtr->getVolume()<<endl<<endl;
    }//end for

	for(int i = 0; i < 4; i++)
	{
		delete shapes[i];
	}
	
	return 0;
}

Recommended Answers

All 5 Replies

Where have you defined the overloaded operator << ?

in shape.h it compiles and runs fine it just dont print the radius out

sorry not radius but xcenter and ycenter

Your problem is that the constructors for TwoDimensionalShape and ThreeDimensionalShape do not carry the parameters over to the base class Shape:

//This is what you had:
        TwoDimensionalShape(double x,double y)
	{
	double Shape(double x,double y);
	}
//This is what it should be:
        TwoDimensionalShape(double x,double y) : Shape(x,y) { };
//and similarly for the ThreeDimensionalShape

What was happening is that the default constructor of Shape was called, setting each coordinate to 0.

Thank you that solved the problem you guys are the best

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.