Ohhh I don't know about that. Ever heard of a "struct hierarchy"? Neither have I... structures don't facilitate object oriented concepts like inheritance and polymorphism like classes do.
Well, I have to admit I've never tried using a "struct hierarchy" in C++, but if the following code works, then in C++ there's no difference between classes and structs accept for the default access:
Code:
include <iostream>
using namespace std;
struct StructA
{
private:
int a_number;
public:
StructA(int num): a_number(num){};
int Get_a()
{
return a_number;
}
virtual void print()
{
cout<<"This is a."<<endl;
}
};
struct StructB: public StructA
{
private:
double b_number;
public:
StructB(double num1, int num2 ): StructA(num2)
{
b_number = num1;
}
double Get_b()
{
return b_number;
}
virtual void print()
{
cout<<"This is b."<<endl;
}
};
int main()
{
StructB my_b(5.1, 1);
cout<<my_b.Get_a()<<endl
<<my_b.Get_b()<<endl;
StructA my_a(10);
StructA* pStructA = &my_a;
pStructA->print();
pStructA = &my_b;
pStructA->print();
return 0;
}