I have a 2 part problem.

1) In circleTypeImp.cpp I am being returned incorrect values for circum and area variables. I have tried using both float and double variable type with no luck. Any pointers?

2) I have read over how to derive a class a number of times and tried a bunch of code changes, but I must still be missing something. I want to "derive the class circleType from the class pointType". What I want to accomplish is to call inputPoint (found in pointTypeImp.cpp) from within circleTypeImp.cpp. Can someone show me how to do this? Once I can get one working I can finish implementing the rest of my code.

pointType.h code:

class pointType
{
#ifndef point_test
#define point_test
public:
 pointType();
  // Default constructor
 int inputPoint();
  // Input X & Y coordinates for a point
 void printXCoordinate(int x);
  // Print X coordinate
 void printYCoordinate(int Y);
  // Print Y coordinate
 void printCoordinates(int x, int y);
  // Display X & Y coordinates
 int x;   // Store X coordinate
 int y;   // Store Y coordinate
private:
 int display;  // Display method selection
#endif
};

pointTypeImp.cpp code:

#include <iostream>
#include "pointType.h"
using namespace std;
int pointType::inputPoint()
{
 cout<<endl<<"Enter the X coordinate: ";
 cin>>x;
 cout<<endl<<"Enter the Y coordinate: ";
 cin>>y;
 return x,y;
}
void pointType::printXCoordinate(int x)
{
 cout<<endl<<"The X coordinate is: "<<x;
}
void pointType::printYCoordinate(int y)
{
 cout<<endl<<"The Y coordinate is: "<<y;
}
void pointType::printCoordinates(int x, int y)
{
 cout<<endl<<"The coordinates of the point are: ("<<x<<","<<y<<").";
}
pointType::pointType()
{
}
int main()
{
 int display;
 pointType myPoint;
 myPoint.inputPoint();
 cout<<endl<<"Select the method of display.";
 cout<<endl<<"   1)  Display X coordinate";
 cout<<endl<<"   2)  Display Y coordinate";
 cout<<endl<<"   3)  Display BOTH coordinates";
 cout<<endl;
 cout<<endl<<"Enter your choice of display: ";
 cin>>display;
 switch(display)
 {
  case 1: myPoint.printXCoordinate(myPoint.x);
   break;
  case 2: myPoint.printYCoordinate(myPoint.y);
   break;
  case 3: myPoint.printCoordinates(myPoint.x,myPoint.y);
   break;
  default: cout<<endl<<"You must enter either a '1', '2' or a '3'..."<<endl;
 }
}

circleType.h code:

class circleType
//class circleType: public pointType
{
public:
 circleType();
  // Default constructor
 int inputRadius();
  // Input radius of a circle
 float calculateArea(float pi, int radius);
  // Calculate circle area
 float calculateCircumference(float pi, int radius);
  // Calculate circle circumference
 void printRadius(int radius);
  // Display radius
 void printArea(float area);
  // Display circle area
 void printCircumference(float circum);
  //Display circle circumference
 int radius;   // Store circle radius
 float area;   // Store circle area
 float circum;   // Store circle circumference
 float pi;   // Store Pi
private:
 int display;   // Display method selection
};

circleTypeImp.cpp code:

#include <iostream>
#include "pointType.h"
#include "circleType.h"
using namespace std;
int circleType::inputRadius()
{
 cout<<endl<<"Enter the circle RADIUS: ";
 cin>>radius;
 return radius;
}
float circleType::calculateArea(float pi, int radius)
{
 area = pi * 2 * radius;
 return area;
}
float circleType::calculateCircumference(float pi, int radius)
{
 circum = pi * radius * radius;
 return circum;
}
void circleType::printRadius(int radius)
{
 cout<<endl<<"The radius of the circle is: "<<radius;
}
void circleType::printArea(float area)
{
 cout<<endl<<"The area of the circle is: "<<area;
}
void circleType::printCircumference(float circum)
{
 cout<<endl<<"The circumference of the circle is: "<<circum;
}
circleType::circleType()
{
}
int main()
{
 float pi = 3.14;
cout<<pi;
 int display;
 circleType myCircle;
 myCircle.inputRadius();
 cout<<endl<<"Select the method of display.";
 cout<<endl<<"   1)  Display circle RADIUS";
 cout<<endl<<"   2)  Display circle AREA";
 cout<<endl<<"   3)  Display circle CIRCUMFERENCE";
 cout<<endl;
 cout<<endl<<"Enter your choice of display: ";
 cin>>display;
 switch(display)
 {
  case 1: myCircle.printRadius(myCircle.radius);
   break;
  case 2: myCircle.calculateArea(myCircle.pi, myCircle.radius);
   myCircle.printArea(myCircle.area);
   break;
  case 3: myCircle.calculateCircumference(myCircle.pi, myCircle.radius);
   myCircle.printCircumference(myCircle.circum);
   break;
  default: cout<<endl<<"You must enter either a '1', '2' or a '3'..."<<endl;
 }
}

Okay,
first when you write header file, you want to make protection from including this file more than once (inclusion guard). This is usually done like this:

#ifndef FILE_NAME_H
#define FIULE_NAME_H
//all code comes here
#endif

Your way can cause a lot of problem so please write it like this:

#ifndef point_test
#define point_test

class pointType
{
//blabla
};
#endif

I don't think it's a good idea to leave x and y variables to be public, it's better to be private and to have methods for accessing and modifying them, after all it's data encapsulation.
Also you wrote: int display and put comment method!?!. Method is function member of class and not variable.

As for part 1:
Area of circle is pi * radius * radius and
circumference of circle is 2*pi*radius. So you need to swap code.
There is no need to send pi to function since it's constant. So make it constant be using this:

#define pi 3.14159

or something like this:

const double pi = 3.14159;

As for part 2:
Theory behind inheritance is simple (at least on this level that is needed for solving this problem). Follow this example:

#include <iostream>
using namespace std;

class Base
{
    public:
        void say_hi()
        {
            cout <<"Hi, base class\n";
        }
};

class Derived : public Base
{
    public:
        void test()
        {
            cout <<"test, derived\n";
        }
        void test_call()
        {
            say_hi();
            cout <<"test_call\n";
        }
};

int main()
{
    Derived d;
    d.say_hi();
    d.test();
    d.test_call();
}

and try to solve this problem by yourself. If you have problems post again.
And one more thing. use at least 4 spaces for code indent, it's much better and easier for reading.

Cheers!

> I am being returned incorrect values for circum and area variables.
You got the calculations the wrong way round
area is pi*r*r
circumference is pi*2*r

> return x,y;
This does NOT return two values, it returns y

Also, you're using a class containing data, there is no need to keep passing results back and forward in parameters.
Your data should also be in the private section of the class.

The member class member functions have automatic access to class member data.
So you should be able to do this

switch(display)
 {
  case 1: myCircle.printRadius();
   break;
  case 2: myCircle.calculateArea();
   myCircle.printArea();
   break;
  case 3: myCircle.calculateCircumference();
   myCircle.printCircumference();
   break;

For your first problem, there are mistake in your calculation for area and circumference. It it another way round. There might be a problem in passing the value in "switch case" for the pi value. I think you can pass the value like this "myCircle.calculateArea(pi, myCircle.radius);" instead of "myCircle.calculateArea(myCircle.pi, myCircle.radius);" since pi is a constant number without asking user for the value.

For your second problem, there are mistake in "return x,y;". It only return y value. Maybe you can separate it like this "return x; return y; " OR you can change the function to type void so there is no necessary to have two return value.

As for the problem of deriving a class, here are the example of the syntax. Hope it can help you.

pointType.h
========
#ifdef ...
#define ...
#include <iostream>

class pointType
{
.......
};
#endif

pointType.cpp
=========
#include <iostream>
#include "pointType.h"

functionType functionName(parameterType parameterName)
{
....
}

circleType.h
========
#include "pointType.h"

class circleType : public pointType
{
......
};

circleType.cpp
=========
#include <iostream>
#include "circleType.h"

void circleType::inputRadius()
{
pointType::inputPoint();
cout<<endl<<"Enter the circle RADIUS: ";
cin>>radius;
}

int main()
{
...
}

Please make sure you can only have one main function on the calling class. You can also separate the main function in another files by itself to avoid clashes when running the program.

I have been able to fix most of the problems with the tips everyone has provided, except for one. I am still having some problems with the derived class. I can get everything to compile and run, as long as I don't try to use any functions from pointType.

I get the following error when I attempt to compile this code:

/cygdrive/c/DOCUME~1/Chad/LOCALS~1/Temp/ccDkEJUJ.o:circleTypeImp.cpp: (.text+0x2a5): undefined reference to pointType::inputPoint()'
collect2: ld returned 1 exit status

My new code is shown below with my problem wrapped by astericks...

pointType.h

#ifndef point_test
#define point_test
#include <iostream>
class pointType
{
public:
//  pointType();
        // Default constructor
    int inputPoint();
        // Input X & Y coordinates for a point
    void printXCoordinate(int x);
        // Print X coordinate
    void printYCoordinate(int Y);
        // Print Y coordinate
    void printCoordinates(int x, int y);
        // Display X & Y coordinates
    int x;   // Store X coordinate
    int y;   // Store Y coordinate
private:
    int display;  // Display method selection
};
#endif

pointTypeImp.cpp

#include <iostream>
#include "pointType.h"
using namespace std;
int pointType::inputPoint()
{
    cout<<endl<<"Enter the X coordinate: ";
    cin>>x;
    cout<<endl<<"Enter the Y coordinate: ";
    cin>>y;
    return x,y;
}
void pointType::printXCoordinate(int x)
{
    cout<<endl<<"The X coordinate is: "<<x;
}
void pointType::printYCoordinate(int y)
{
    cout<<endl<<"The Y coordinate is: "<<y;
}
void pointType::printCoordinates(int x, int y)
{
    cout<<endl<<"The coordinates of the point are: ("<<x<<","<<y<<").";
}
pointType::pointType()
{
}
int main()
{
    int display;
    pointType myPoint;
    myPoint.inputPoint();
    cout<<endl<<"Select the method of display.";
    cout<<endl<<"   1)  Display X coordinate";
    cout<<endl<<"   2)  Display Y coordinate";
    cout<<endl<<"   3)  Display BOTH coordinates";
    cout<<endl;
    cout<<endl<<"Enter your choice of display: ";
    cin>>display;
    switch(display)
    {
        case 1: myPoint.printXCoordinate(myPoint.x);
                break;
        case 2: myPoint.printYCoordinate(myPoint.y);
                break;
        case 3: myPoint.printCoordinates(myPoint.x,myPoint.y);
                break;
        default: cout<<endl<<"You must enter either a '1', '2' or a '3'..."<<endl;
    }
}

circleType.h

#include "pointType.h"
class circleType : public pointType
{
public:
    circleType();
        // Default constructor
    int inputRadius();
        // Input radius of a circle
    double calculateArea(double pi, int radius);
        // Calculate circle area
    double calculateCircumference(double pi, int radius);
        // Calculate circle circumference
    void printRadius(int radius);
        // Display radius
    void printArea(double area);
        // Display circle area
    void printCircumference(double circum);
        //Display circle circumference
    int radius;   // Store circle radius
    double area;   // Store circle area
    double circum;   // Store circle circumference
    double pi;   // Store Pi
private:
    int display;   // Display method selection
};

circleTypeImp.cpp

#include <iostream>
#include "circleType.h"
using namespace std;
int circleType::inputRadius()
{
    cout<<endl<<"Enter the circle RADIUS: ";
    cin>>radius;
    return radius;
}
double circleType::calculateArea(double pi, int radius)
{
    area = pi * radius * radius;
    return area;
}
double circleType::calculateCircumference(double pi, int radius)
{
    circum = pi * 2 * radius;
    return circum;
}
void circleType::printRadius(int radius)
{
    cout<<endl<<"The radius of the circle is: "<<radius;
}
void circleType::printArea(double area)
{
    cout<<endl<<"The area of the circle is: "<<area;
}
void circleType::printCircumference(double circum)
{
    cout<<endl<<"The circumference of the circle is: "<<circum;
}
circleType::circleType()
{
}
int main()
{
    const double pi = 3.14159;
    int display;
    circleType myCircle;
 
//******************************
    myCircle.inputPoint();
//******************************
 
    myCircle.inputRadius();
    cout<<endl<<"Select the method of display.";
    cout<<endl<<"   1)  Display circle RADIUS";
    cout<<endl<<"   2)  Display circle AREA";
    cout<<endl<<"   3)  Display circle CIRCUMFERENCE";
    cout<<endl;
    cout<<endl<<"Enter your choice of display: ";
    cin>>display;
    switch(display)
    {
        case 1: myCircle.printRadius(myCircle.radius);
                break;
        case 2: myCircle.calculateArea(pi, myCircle.radius);
                myCircle.printArea(myCircle.area);
                break;
        case 3: myCircle.calculateCircumference(pi, myCircle.radius);
                myCircle.printCircumference(myCircle.circum);
                break;
        default: cout<<endl<<"You must enter either a '1', '2' or a '3'..."<<endl;
    }
}

How exactly are you linking this? I see you have two entry points (in pointTypeImp.cpp and circleTypeImp.cpp) and circleTypeImp.cpp relies on code from pointTypeImp.cpp. Obviously, though, you aren't linking these two files or you would have a multiple definition error for main.

Generally, you should have separate files that contain class implementations (separate from where main() is defined, that is).

How exactly are you linking this? I see you have two entry points (in pointTypeImp.cpp and circleTypeImp.cpp) and circleTypeImp.cpp relies on code from pointTypeImp.cpp. Obviously, though, you aren't linking these two files or you would have a multiple definition error for main.

If I knew that answer I would probably have my problem figured out by now... :?:

Some others have posted examples, but they don't work as advertised... at least not for me they don't...

Maybe the answer is simple... and right in front of me... but I am missing it if it is (and probably about to get bit by a snake name "obvious"... :)

This article has been dead for over six months. Start a new discussion instead.