用这个方法A.什么的调用的都是自己
弗父类的指针可以指向儿子,儿子的指针不能调用老爸
如果不是虚函数
老爸的指针只能调用自己
如果有虚函数
老爸的指针指向谁就调用谁
#include <iostream>
using namespace std;
class A
{
private:
int x;
int y;
public:
A(int i, int j)
{
x = i;
y = j;
}
virtual void disp()
{
cout << "x=" << x << " " << "y=" << y << endl;
}
};
class B: public A
{
int z;
public:
B(int i, int j, int k): A(i, j)
{
z = k;
}
virtual void disp()
{
cout << "z=" << z << endl;
}
};
int main ()
{
A a(50, 100), *p;
B b(1, 2, 3);
a.disp();
cout << endl;
b.disp();
cout << endl;
p = &a;
p->disp();
cout << endl;
p = &b;
p->disp();
return 0;
}
#include <iostream>
using namespace std;
class A
{
private:
int x;
int y;
public:
A(int i, int j)
{
x = i;
y = j;
}
void disp()
{
cout << "x=" << x << " " << "y=" << y << endl;
}
};
class B: public A
{
int z;
public:
B(int i, int j, int k): A(i, j)
{
z = k;
}
void disp()
{
cout << "z=" << z << endl;
}
};
int main ()
{
A a(50, 100), *p;
B b(1, 2, 3);
a.disp();
cout << endl;
b.disp();
cout << endl;
p = &a;
p->disp();
cout << endl;
p = &b;
p->disp();
return 0;
}