Hello,
I'm trying to understand some things about derived classes: how to add something "extra" to existing functions in base class.
My base class is "shape2D" like a rectangle, 3 functions: set values, calculate, show results. No problem here.
Then I create a derived class named "shape3D". Please note the private stuff in the base class.
Everything goes OK when I create one object of shape2D, do stuff, then another object of shape3D and do the same stuff. I get no errors for the private variables from the base class.
Then I want to add another dimension (height) to the derived class and add one more cin
for it in the first function (set third value), one more calculation in the second function (calculate volume), one more line in the third function (display volume). Here I get the errors that "cannot access private member". If I switch variables from private to protected works OK. See class shape2D.h.
I don't understand why it works with private vars if I don't modify the methods and why should I use protected in the other case.
Do I miss something? Can someone explain how this works (shortly, I couldn't find a proper explanation on goolge).
Is this "add 2 lines to the base function" (class "shape3D") OK?
Code below. I work with separate files just for "training" purposes.
Thank you!
shape2D.h:
#ifndef SHAPE2D_H
#define SHAPE2D_H
using namespace std;
class shape2D
{
public:
shape2D();
virtual ~shape2D();
void set_dimensions();
void dimension_calculations();
void show_results();
protected:
//int d_lenght,d_width,d_area, d_perimeter; //Here I switch
private:
int d_lenght,d_width,d_area, d_perimeter; // with this
};
#endif // SHAPE2D_H
shape2D.cpp:
#include <iostream>
#include ".\shape2D.h"
shape2D::shape2D()
{
}
shape2D::~shape2D()
{
}
void shape2D::set_dimensions()
{
cout << "Lenght : ";
cin >> d_lenght;
cout << "Width : ";
cin >> d_width;
}
void shape2D::dimension_calculations()
{
d_perimeter = 2 * (d_lenght + d_width);
d_area = d_lenght * d_width;
}
void shape2D::show_results()
{
cout << "\n\nPerimeter: \t" << d_perimeter << "\nArea: \t\t" << d_area << endl;
}
main:
#include <iostream>
#include "shape2D.h"
#include "shape3D.h"
using namespace std;
int main()
{
shape2D sh2d;
shape3D sh3d;
sh2d.set_dimensions();
sh2d.dimension_calculations();
sh2d.show_results();
cout << "/n/n/n/n";
sh3d.set_dimensions();
sh3d.dimension_calculations();
sh3d.show_results();
return 0;
}
shape3D.h:
#ifndef SHAPE3D_H
#define SHAPE3D_H
#include "shape2D.h"
#include <iostream>
class shape3D:public shape2D
{
public:
shape3D();
virtual ~shape3D();
/*void set_dimensions(); // I TRIED TO "IMPROVE" BASE CLASS FUNCTION
void dimension_calculations();
void show_results();*/
protected:
int d_height, d_volume;
private:
//int d_height, d_volume;
};
#endif // SHAPE3D_H
shape3D.cpp:
#include "shape3D.h"
shape3D::shape3D()
{
//constructor
}
shape3D::~shape3D()
{
//destructor
}
// I TRIED TO "IMPROVE" BASE CLASS FUNCTION
/*void shape3D::set_dimensions()
{
shape2D::set_dimensions(); //CALLING THE BASE FUNCTION THEN ADD ONE MORE INPUT
cout << "Height : ";
cin >> d_height;
}
void shape3D::dimension_calculations()
{
shape2D::dimension_calculations();
d_volume=d_height*d_lenght*d_width;
}
void shape3D::show_results()
{
shape2D::show_results();
cout << "Volume: \t" << d_volume;
}
*/