class A
{
        public:
                int a;
                A() : a(100) { }
               
};

class B : public A
{
        public:
                int b;
                B() : b(200){ }
};

int main()
{
        A a;
        B *ptrB =(B*)&a;

        cout<<ptrB->b<<endl; // Is there a way to get this to print 200??
}

the pointer is pointing to an object of class A which has no relation with class B...
using base class object cannot print value of b.

First use code tags. Your code is not formatted.Formatting makes your code easier to read.

class A
{
public:
     int a;
     A() : a(100) { }
};

class B : public A
{
public:
    int b;
     B() : b(200){ }
};

int main()
{
    A a; (B*)&a;

    cout << ptrB -> b << endl; // Is there a way to get this to print 200??
}

OK. Thanks for the info. Will have it in mind for future posts.

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.