011_multiderivedwithVir.cc
#include
using std::cout;
using std::endl;
//------------------------------//
class A{
public:
virtual void a(){ cout << "A::a()" << endl; }
virtual void b(){ cout << "A::b()" << endl; }
virtual void c(){ cout << "A::c()" << endl; }
};
//------------------------------//
class B{
public:
virtual void a(){ cout << "B::a()" << endl; }
virtual void b(){ cout << "B::b()" << endl; }
void c(){ cout << "B::c()" << endl; }
void d(){ cout << "B::d()" << endl; }
};
//------------------------------//
class C
: public A
, public B
{
public:
virtual void a(){ cout << "C::a()" << endl; }
void c() override{ cout << "C::c()" << endl; }
//能加 覆盖
void d(){ cout << "C::d()" << endl; }
};
//2 表
//A C::a() C::c() B C::c()
//d()
//------------------------------//
//先不看D类
class D
: public C
{
public:
void c(){ cout << "D::c()" << endl; }
//如果 D::c()是虚函数,覆盖虚表C::c()
//那么C*通过虚表访问,只能访问到 D::c()
//
//如果D::c()是造成基类同名函数隐藏
//那么C*指针访问,会访问到基类的函数C::c()??(鬼知道
};
void test(){
/* D c; */
C c;
c.a();//C::a() 隐藏
c.c();//C::c() 隐藏
c.d();//C::d() 隐藏,d对于C是普通成员函数
/* c.b(); //成员名访问冲突二义性 */
cout<a();//C::a() 动态多态
pa->b();//A::b() 没有覆盖,会通过虚函数表
pa->c();//C::c() 动态多态
/* pa->d();//A类没有定义d函数,无法调用 */
cout << endl;
B* pb = &c;
pb->a(); //C::a() 多态
pb->b(); //B::b() 没有覆盖,虚表
pb->c(); //B::c() 普通函数c()
pb->d(); //B::d() 普通函数d()
cout << endl;
C * pc = &c;
pc->a(); //C::a() 通过虚表,没有触发动态多态
/* pc->b(); */ //访问二义性
pc->c(); //C::c() 本质是虚函数,通虚表,没有触发动态多态
pc->d(); //C::d() 隐藏 没有多态
cout << endl;
D d1;
C * pd = &d1;
pd->c(); //D::c()
}
int main(void)
{
test();
return 0;
}
heckVtableAddress.cc
#include
using std::cout;
using std::endl;
//-------------------------------//
class Base{
public:
virtual void print() {
cout << "Base::print()" << endl;
}
virtual void display() {
cout << "Base::display()" << endl;
}
virtual void show() {
cout << "Base::show()" << endl;
}
private:
long _base = 10;
};
//-------------------------------//
class Derived
: public Base
{
public:
virtual void print() {
cout << "Derived::print()" << endl;
}
virtual void show() {
cout << "Derived::show()" << endl;
}
virtual void display() {
cout << "Derived::display()" << endl;
}
private:
long _derived = 100;
};
//-------------------------------//
void test(){
cout<
DiamondConstructor.cc
#include
using std::cout;
using std::endl;
//-------------------------------------------//
class A
{
public:
A(double a)
: _a(a)
{cout << "A(double)" << endl;}
~A(){cout << "~A()" << endl;}
private:
double _a = 10;
};
//-------------------------------------------//
class B
: virtual public A
{
public:
B(double a, double b)
: A(a)
, _b(b)
{cout << "B(double,double)" << endl;}
~B(){ cout << "~B()" << endl; }
private:
double _b;
};
//-------------------------------------------//
class C
: virtual public A
{
public:
C(double a, double c)
: A(a)
, _c(c)
{
cout << "C(double,double)" << endl;
}
~C(){ cout << "~C()" << endl; }
private:
double _c;
};
//-------------------------------------------//
class D
: public B
, public C
{
public:
D(double a,double b,double c,double d)
: A(a)
, B(a,b)
, C(a,c)
, _d(d)
//虚拟继承的结构中,最底层需要显示调用中间层,最底层基类的构造函数
//A类构造压抑BC类构造调用A类构造
{
cout << "D(double * 4)" << endl;
}
~D(){ cout << "~D()" << endl; }
private:
double _d;
};
//-------------------------------------------//
void test(){
cout<
013_abab.cc
#include
using std::cout;
using std::endl;
//-------------------------------//
class A {
public:
virtual void foo() { std::cout << "A::foo()" << std::endl; }
virtual void bar() { std::cout << "A::bar()" << std::endl; }
virtual void baz() { std::cout << "A::baz()" << std::endl; }
virtual ~A() {}
double a=10;
};
//-------------------------------//
class B : public virtual A {
public:
virtual void foo() override { std::cout << "B::foo()" << std::endl; }
virtual void bar() override { std::cout << "B::bar()" << std::endl; }
virtual void qux() { std::cout << "B::qux()" << std::endl; }
double b=20;
};
//-------------------------------//
class C : public virtual A {
public:
virtual void baz() override { std::cout << "C::baz()" << std::endl; }
virtual void quux() { std::cout << "C::quux()" << std::endl; }
double c=30;
};
//-------------------------------//
class D : public B, public C {
public:
virtual void foo() override { std::cout << "D::foo()" << std::endl; }
virtual void bar() override { std::cout << "D::bar()" << std::endl; }
virtual void qux() override { std::cout << "D::qux()" << std::endl; }
virtual void quux() override { std::cout << "D::quux()" << std::endl; }
double d=40;
};
void test(){
cout<A 16
cout<B +vb->A 32
cout<C +vb->A 32
cout<D vb->B/C 56
cout<
作业
01 根据给定的程序,写出执行结果 (1,2)(6,9)5,6
#include
using std::endl;
using std::cout;
//------------------------------//
class A
{
public:
A(int i,int j){
a=i;
b=j;
}
void move(int i,int j) {
a += i;
b += j;
}
void disp() { cout << "(" << a << "," << b << ")" << endl;}
private:
int a,b;
};
//------------------------------//
class B
: public A
{
public:
B(int i,int j,int k,int l)
:A(i,j)
,x(k)
,y(l)
{ }
void disp(){cout << x << "," << y << endl;}
void fun() {move(3,5);}
private:
int x,y;
};
//------------------------------//
int main()
{
A m(1,2);
m.disp();//(1,2)
B d(3,4,5,6);
d.fun();//6,9,5,6
d.A::disp();//(6,9)
d.disp();//5,6
return 0;
}
02 根据给定的程序,写出执行结果 --Base1--Base2--Derived--Base2--
#include
using std::endl;
using std::cout;
//------------------------------//
class Base1
{
public:
virtual void fun(){cout<<"--Base1--\n"; }
};
//------------------------------//
class Base2
{
public:
void fun(){cout<<"--Base2--\n"; }
};
//------------------------------//
class Derived
:public Base1
,public Base2
{
public:
void fun(){cout<<"--Derived--\n";}
};
int main()
{
Base1 obj1, *ptr1;
Base2 obj2, *ptr2;
Derived obj3;
ptr1=&obj1;
ptr1->fun();//--Base1
ptr2=&obj2;
ptr2->fun(); //---Base2
ptr1=&obj3;
ptr1->fun(); //--Base3
ptr2=&obj3;
ptr2->fun(); //--Base2
return 0;
}
03 根据给定的程序,写出执行结果 ABBAB
#include
using std::cout;
using std::endl;
//------------------------------//
class A
{
public:
void FuncA(){printf( "FuncA called\n" );}
virtual void FuncB(){printf( "FuncB called\n" );}
};
//------------------------------//
class B
: public A
{
public:
void FuncA(){
A::FuncA();
printf( "FuncAB called\n" );
}
virtual void FuncB(){printf( "FuncBB called\n" );}
};
//------------------------------//
void test(){
B b;
A *pa;
pa = &b;//*A->B
A *pa2 = new A; //*A->A
pa->FuncA();// FuncA called //fugai
pa->FuncB();// FuncBB called
pa2->FuncA(); //FuncA called
pa2->FuncB(); //FuncB called
delete pa2;
}
int main(void)
{
test();
return 0;
}
04 根据给定的程序,写出执行结果 12
#include
using std::cout;
using std::endl;
//------------------------------//
class Base
{
public:
Base(int j)
: i(j)
{}
virtual ~Base() {}
void func1() {
i *= 10;
func2();
}
int getValue(){return i;}
protected:
virtual void func2(){i++;}
protected:
int i;
};
//------------------------------//
class Child
: public Base
{
public:
Child(int j)
: Base(j)
{}
void func1(){
i *= 100;
func2();
}
protected:
void func2(){i += 2;}
//覆盖
};
//------------------------------//
void test(){
Base * pb = new Child(1);
//Base *->Child
pb->func1();
//i*=10 +=2 12
cout << pb->getValue() << endl;
//12
delete pb;
}
int main(void)
{
test();
return 0;
}
05 实现C++ primer 15.9中的文本查询扩展的作业,可以查某个单词在某行出现、某个单词在某行没有出现、某两个单词在某行出现、某两个单词至少有一个出现、三个单词的查询等等。(即实现查询单词的与、或、非操作)