Question: Even though x is private, what can I do to make x available in the derived functions cylinderType?

When I compile the cylinderType program I get the following message:

Line Message

18 'int circleType::x' is private

94 within this context

This message is repeated several time in the following functions:

float cylinderType::getTotalArea()

{

return 2 * x * x * PI * (x + y);

}

float cylinderType::getVolume()

{

return PI * x * x + y;

}

cylinderType::cylinderType(int a, int b):circleType(a + b)

{

x = a;

y = b;

}

Entire code:

#include <iostream>

using namespace std;

const float PI = 3.14;

class circleType

{

public:

void print()const;

int getX();

float getArea();

float getCircumference();

circleType();

circleType(int a);

private:

int x;

};

class cylinderType: public circleType

{

public:

void print()const;

int getY();

float getTotalArea();

float getVolume();

cylinderType();

cylinderType(int a, int b = 0);

private:

int y;

};

int main()

{

circleType radiusObject(5);

cylinderType heightObject(3);

radiusObject.print();

heightObject.print();

cout<<"Circle area = " <<radiusObject.getArea()<<endl;

cout<<"Circle circumference = " <<radiusObject.getCircumference()<<endl;

cout<<"Cylinder total area = "<<heightObject.getTotalArea()<<endl;

cout<<"Cylinder volume = " <<heightObject.getVolume()<<endl;

cin.get();

return 0;

}

int circleType::getX()

{

return x;

}

void circleType::print()const

{

cout<<"r = radius of a circle. r = "<<x<<", PI = "<<PI<<endl;

cout<<"Formulas for area of circle = (2 * PI * r * r)"<<endl;

cout<<"Formulas for circumferencea of circle = (2 * r * PI)"<<endl;

}

void cylinderType::print()const

{

cout<<"Formula for total area of a cylinder = 2 * r * PI * (r + h)"<<endl;

cout<<"Formula for volume of a cylinder = PI * r * r * h"<<endl;

}

float circleType::getArea()

{

return 2 * PI * x * x;

}

float circleType::getCircumference()

{

return 2 * x * PI;

}

int cylinderType::getY()

{

return y;

}

float cylinderType::getTotalArea()

{

return 2 * x * x * PI * (x + y);

}

float cylinderType::getVolume()

{

return PI * x * x + y;

}

circleType::circleType()

{

x = 0;

}

circleType::circleType(int a)

{

x = a;

}

cylinderType::cylinderType(int a, int b):circleType(a + b)

{

x = a;

y = b;

}

a) make it a protected member, if applicable

b) Add an accessor member t othe base class, e.g. -- ops, you already have an accessor, so use it:

float cylinderType::getTotalArea

{

float x = getX();

return 2 * x * x * PI * (x + y);

}

float cylinderType::getVolume()

{

float x = getX();

return PI * x * x + y;

}

If you want to be able to change the value also, use

class circleType

{

public:

void print()const;

int getX() const { return x;}

int& getX() { return x;};

//...

};

Be careful, the above just illustrates the idea, you probably have another 'getX()' somewhere else around, so remove one of these.