Hi,

I am going to be attending university next year on a coding course, I am currently working through an exercise in the Deitel book How To Program and I have encountered some errors in my program. Any help would be much appreciated.

Bob

Here is my code

cube.h

#include "cube.h"

#include <iostream>
using std::cout;

//Write the constructor for Cube class
 
Cube::Cube(double s, double x, double y) 
   : ThreeDimensionalShape( x, y) { sideLength = s > 0 ? s : 0; }

double Cube::area() const { return 6 * sideLength * sideLength; }

double Cube::volume() const
   { return sideLength * sideLength * sideLength; }

double Cube::getSideLength() const { return sideLength; }

void Cube::print() const
{
   cout << "Cube with side length " << sideLength << "; center at ("
        << xCenter << ", " << yCenter << ");\narea of "
        << area() << "; volume of " << volume() << '\n';
}

shape.h

#ifndef SHAPE_H
#define SHAPE_H

#include <iostream>
using namespace std; 

class Shape {
   friend ostream & operator<<( ostream &, Shape & );
public:
   Shape( double = 0, double = 0 );
   double getCenterX() const;
   double getCenterY() const;

   //Write virtual pure print function
 
virtual void print() const = 0; 
 
protected:
   double xCenter;
   double yCenter;
};

#endif

square.h

#ifndef SQUARE_H
#define SQUARE_H

#include "twodim.h"

// write statement for square public inheritance from 2-dimensional class
 class Square : public TwoDimensionalShape {

    
public:
   Square( double = 0, double = 0, double = 0 );
   double getSideLength() const;
   double area() const;
   void print() const;
private:
   double sideLength;
};

#endif

threedim.h

#ifndef THREEDIM_H
#define THREEDIM_H

#include "shape.h"

class ThreeDimensionalShape : public Shape {
public:

          // Write the constructor for 3-dimensional shape 
ThreeDimensionalShape( double = 0, double = 0);
      
      // declare a pure virtual member function area
 
virtual double area() const = 0; 

      // declare a pure virtual member function volume
virtual double volume() const = 0; 
 

};

#endif

twodim.h

#ifndef TWODIM_H
#define TWODIM_H

#include "shape.h"

class TwoDimensionalShape : public Shape {
public:

   // Write the constructor for 2-dimensional shape and declare 
   // pure virtual member functions area
 
TwoDimensionalShape( double = 0.0, double = 0.0 );
    // declare pure virtual member functions area
virtual double area() const = 0; 
 

};

#endif

main.cpp

#include <iostream> 

using namespace std; 


#include "square.h"
#include "cube.h"

int main()
{
   Square sqr( 12, 2, 2 );
   Cube cub( 2.2 );
   Shape *ptr[ 2 ] = { &sqr, &cub };

   for ( int x = 0; x < 2; ++x )
      cout << *( ptr[ x ] ) << '\n';

   return 0;
}

cube.cpp

#include <iostream> 

using namespace std; 


#include "square.h"
#include "cube.h"

int main()
{
   Square sqr( 12, 2, 2 );
   Cube cub( 2.2 );
   Shape *ptr[ 2 ] = { &sqr, &cub };

   for ( int x = 0; x < 2; ++x )
      cout << *( ptr[ x ] ) << '\n';

   return 0;
}

shape.cpp

#include "shape.h"

Shape::Shape( double x, double y )
{
   xCenter = x;
   yCenter = y;
}

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

double Shape::getCenterY() const { return yCenter; }

ostream & operator<<( ostream &out, Shape &s )
{
   s.print();
   return out;
}

square.cpp

#include "square.h"

#include <iostream>
using std::cout;

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

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

double Square::area() const {

	// write the code for calculating the area of a square
	return sideLength * sideLength;

}

void Square::print() const
{
	cout << "Square with side length " << sideLength << "; center at ("
		<< xCenter << ", " << yCenter << ");\narea of " << area() << '\n';
}

Please post the nature of your errors ( syntax, logical, segfault, etc ) as well as your compiler/application output.

error LNK2019: unresolved external symbol "public: __thiscall TwoDimensionalShape::TwoDimensionalShape(double,double)"

error LNK2019: unresolved external symbol "public: __thiscall ThreeDimensionalShape::ThreeDimensionalShape(double,double)"

Did you define them? Also why does ThreeDimensionalShape only store 2
values. Also there is some serious problems with your inheritance diagram.
Take another look at the shape class. Look at what it stores. Look at
what you need for each class.

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.