0

if I have a derived class inherit from a base class (protected or private inheritance) can I make a pointer of base class to point to derived object? that's my c++ code:

#include<iostream.h>
#include<conio.h>
class Geoshape
{
protected:
    int dim1;
    int dim2;

public:
    Geoshape()
        {dim1=0;dim2=0;}
    Geoshape(int x, int y)
        {dim1=x ; dim2=y;}
    void setDim1(int x)
        {dim1=x;}
    int getDim1()
        {return dim1;}
    void setDim2(int y)
        {dim2=y;}
    int getDim2()
        {return dim2;}
    int calculateArea()
        {return dim1*dim2;}
};
class Circle:protected Geoshape
{
public:
    Circle()
        {}
    Circle(int r):Geoshape(r,r)
        {dim1=r;dim2=r;}
    void setR(int r)
        {dim1=dim2=r;}
    int getR()
        {return dim1;}
    float calculateArea()
        {return 22.0/7*dim1*dim2;}
};
class Triangle:public Geoshape
{
public:
    Triangle()
        {}
    Triangle(int x, int y):Geoshape(x,y)
        {}
    void setH(int h)
        {dim2=h;}
    int getH()
        {return dim2;}
    void setB(int b)
        {dim1=b;}
    int getB()
        {return dim1;}
    float calculateArea()
        {return .5*dim1*dim2;}
};
class Rectangle:public Geoshape
{
public:
    Rectangle()
        {}
    Rectangle(int x, int y):Geoshape(x,y)
        {}
    void setL(int l)
        {dim1=l;}
    int getL()
        {return dim1;}
    void setH(int h)
        {dim2=h;}
    int getH()
        {return dim2;}
};
class Square:protected Rectangle
{
public:
    Square()
        {}
    Square(int l):Rectangle(l,l)
        {dim1=l;dim2=l;}
    void setL(int l)
        {dim1=dim2=l;}
    int getL()
        {return dim1;}
    float calculateArea()
        {return dim1*dim1;}
};

void main()
{
clrscr();
cout<<"enter circle raduis: ";
int raduis;
cin>>raduis;
Circle c1(raduis);
cout<< "this is area of Circle: "<<c1.calculateArea();
getch();
cout<<"\n\nenter base of triangle: ";
int base;
cin>>base;
cout<<"enter height of triangle: ";
int height;
cin>>height;
Triangle t1(base,height);
cout<< "this is area of Triangle: "<<t1.calculateArea();
getch();
cout<<"\n\nenter length of rectangle: ";
int length;
cin>>length;
cout<<"enter height of rectangle: ";
int height1;
cin>>height1;
Rectangle r1(length,height1);
cout<< "this is area of Rectangle: "<<r1.calculateArea();

getch();
cout<<"\n\nenter length of square: ";
int len;
cin>>len;
Square s1(len);
cout<< "this is area of Square: "<<s1.calculateArea();
Geoshape *p1;Geoshape *p2;Geoshape *p3;Geoshape *p4;
p2=&t1;
p3=&r1;
getch();
}

I want to add those two lines in main:

p1=&c1;
p4=&s1; but this gives errors!

Mouneer
  • 12,827
  • 2
  • 35
  • 45
  • The compiler said "no", don't you believe it? :) – jrok Nov 21 '14 at 17:15
  • Protected inheritance? Why would you use that? (Public -> *is a*, Private -> *implemented in terms of*, Protected -> *!?!?*) – Borgleader Nov 21 '14 at 17:15
  • [For future reference, you'll want to provide a **minimal**, complete, and verifiable example](http://stackoverflow.com/help/mcve) (emphasis on minimal; you got complete and verifiable though) – Cornstalks Nov 21 '14 at 17:15
  • Having the exact error text would help.. – Maor Nov 21 '14 at 17:16
  • @Borgleader It's useful if you want to put the data members of a class into a separate struct, perhaps for communication with C. Protected inherit your class from the struct to get that data, but also make it available to derived classes. A friend helper function can extract a pointer to the basic struct to pass to C function. – Neil Kirk Nov 21 '14 at 17:19
  • @NeilKirk Couldn't you achieve the same result by having the struct as a member instead? – Borgleader Nov 21 '14 at 17:21
  • @Borgleader You could but possibly less convenient syntax. Anyway I don't use it really, just thought you might be interested in an example I read about. – Neil Kirk Nov 21 '14 at 19:35
  • @NeilKirk Sure, I was actually surprised someone had a use case for this. – Borgleader Nov 21 '14 at 20:24
  • @Borgleader Actually I think it was Stroustrup I read it from! http://www.stroustrup.com/bs_faq2.html search for "protected Common" – Neil Kirk Nov 22 '14 at 02:03

1 Answers1

1

You can do that (cast to "base" type) only from within the class (from member functions and static member functions), from derived classes (only in case of public or protected inheritance) or from friend functions. To have unrestricted ability to do that, you can:

  • use public inheritance,
  • provide user-specified conversion operator,
  • provide a function that would return such pointer (essentialy the same as above, but not in the form of "operator T()", but "T* getBase()" or sth like that) - member function, static member function of free-standing friend function.
Freddie Chopin
  • 8,440
  • 2
  • 28
  • 58
  • 1
    thanks all of you. Here is an answer for my question http://stackoverflow.com/questions/9661936/inheritance-a-is-an-inaccessible-base-of-b – Mouneer Nov 21 '14 at 18:14