当多态遇见对象数组会发生什么?
#include <cstdlib>
#include <iostream>
using namespace std;
//4字节VPTR + 4字节的int i = 8字节
class Parent
{
protected:
int i;
public:
virtual void f()
{
cout<<"Parent::f"<<endl;
}
};
//4字节VPTR + 4字节的int i + 4字节的int j = 12字节
class Child : public Parent
{
protected:
int j;
public:
Child(int i, int j)
{
this->i = i;
this->j = j;
}
void f()
{
cout<<"i = "<<i<<" "<<"j = "<<j<<endl;
}
};
int main(int argc, char *argv[])
{
Parent* p = NULL;
Child* c = NULL;
Child ca[3] = {Child(1, 2), Child(3, 4), Child(5, 6)};
cout<<"sizeof(Parent) = "<<sizeof(Parent)<<endl;
cout<<"sizeof(Child) = "<<sizeof(Child)<<endl;
p = ca;
c = ca;
cout<<hex<<&ca[0]<<endl;
cout<<hex<<&ca[1]<<endl;
cout<<hex<<p<<endl;
cout<<hex<<p+1<<endl;
p->f();
c->f();
p++;
c++;
//p->f();//Error
c->f();
cout << "Press the enter key to continue ...";
cin.get();
return EXIT_SUCCESS;
}
编译运行结果如下:
不要将多态应用于数组
1. 指针运算是通过指针的类型进行的,即是编译器编译的时候决定的;
2. 多态通过虚函数表实现的,是程序运行的时候决定的;
为什么没有讲解多重继承?
C++在语法上直接支持多重继承
#include <cstdlib>
#include <iostream>
using namespace std;
class P1
{
protected:
int i;
};
class P2
{
protected:
int j;
};
class Child : public P1,public P2
{
protected:
int j;
public:
Child(int i, int j)
{
this->i = i;
this->j = j;
}
void printf()
{
cout<<"i = "<<i<<" "<<"j = "<<j<<endl;
}
};
int main(int argc, char *argv[])
{
Child c(1, 2);
c.printf();
cout << "Press the enter key to continue ...";
cin.get();
return EXIT_SUCCESS;
}
编译运行结果如下:
被实际开发经验抛弃的多继承
1. 工程开发中真正意义上的多继承是几乎不被使用的;
2. 多重继承带来的代码复杂性远多于其带来的便利;
3. 多重继承对代码维护性上的影响是灾难性的;
4. 在设计方法上,任何多继承都可以用单继承代替;
#include <cstdlib>
#include <iostream>
using namespace std;
class Object
{
protected:
int d;
public:
void f()
{
cout<<"Object::f"<<endl;
}
};
class P1 : public Object
{
protected:
int i;
};
class P2 : public Object
{
protected:
int j;
};
class Child : public P1, public P2
{
public:
Child(int i, int j)
{
//this->d = 0; //Error 不知道d是P1还是P2的,存在二义性
this->i = i;
this->j = j;
}
void print()
{
cout<<"i = "<<i<<" "<<"j = "<<j<<endl;
}
};
int main(int argc, char *argv[])
{
Child c(1, 2);
c.print();
//c.f(); //Error 不知道f是P1还是P2的,存在二义性
cout << "Press the enter key to continue ...";
cin.get();
return EXIT_SUCCESS;
}
在只有单继承的系统中,类之间的继承关系为一颗树。在引入多重继承的系统中,类之间的继承关系呈现为一张图。
C++中对多继承二义性的解决方案
虚继承:
为了解决从不同途径继承来的同名数据成员造成的二义性问题,可以将共同基类设置为虚基类。这时从不同的路径继承过来的同名数据成员在内存中就只有一个拷贝。
#include <cstdlib>
#include <iostream>
using namespace std;
class Object
{
protected:
int d;
public:
void f()
{
cout<<"Object::f"<<endl;
}
};
class P1 : virtual public Object
{
protected:
int i;
};
class P2 : virtual public Object
{
protected:
int j;
};
class Child : public P1, public P2
{
public:
Child(int i, int j)
{
this->d = 0;
this->i = i;
this->j = j;
}
void print()
{
cout<<"i = "<<i<<" "<<"j = "<<j<<endl;
}
};
int main(int argc, char *argv[])
{
Child c(1, 2);
c.print();
c.f();
cout << "Press the enter key to continue ...";
cin.get();
return EXIT_SUCCESS;
}
编译运行结果如下:
C++是否有Java中接口的概念?
绝大多数面向对象语言都不支持多继承;
绝大多数面向对象语言都支持接口的概念;
C++中没有接口的概念;
C++中可以使用纯虚函数实现接口;
#include <cstdlib>
#include <iostream>
using namespace std;
class Interface1
{
public:
virtual void print() = 0;
virtual int add(int i, int j) = 0;
};
struct Interface2
{
virtual int add(int i, int j) = 0;
virtual int minus(int i, int j) = 0;
};
class Child : public Interface1, public Interface2
{
public:
void print()
{
cout<<"Child::print"<<endl;
}
int add(int i, int j)
{
return i + j;
}
int minus(int i, int j)
{
return i - j;
}
};
int main(int argc, char *argv[])
{
Child c;
c.print();
cout<<c.add(3, 5)<<endl;
cout<<c.minus(4, 6)<<endl;
Interface1* i1 = &c;
Interface2* i2 = &c;
cout<<i1->add(7, 8)<<endl;
cout<<i2->add(7, 8)<<endl;
cout << "Press the enter key to continue ...";
cin.get();
return EXIT_SUCCESS;
}
编译运行结果如下: