zhiyinjixu

  博客园  :: 首页  ::  :: 联系 ::  :: 管理

一、有子对象的派生类的构造函数:

#include <iostream>
using namespace std;

class A
{
public:
A(int tam,int tai):am(tam),ai(tai) {}
public:
int am;
protected:
int ai;
};

class B:public A
{public:
B(int tam,int tai, int tob_Am,int tob_Ai, int tbm,int tbx) : A(tam,tai), ob_A(tob_Am,tob_Ai) { bm=tbm; bx=tbx; } //有子对象的派生类的构造函数
public:
int bm;
A ob_A;
void dispaly()
{
cout<<ob_A.am<<endl;
}
private:
int bx;
};

int main()
{
B ob1(45,5, 9,3, 5,8) ;
cout<<ob1.am<<endl;
cout<<endl;
ob1.dispaly();
}


 

二、多层派生是的构造函数的写法:

#include <iostream>
using namespace std;

class A
{
public:
A(){}
A(int tam,int tai,int tax) : am(tam),ai(tai),ax(tax) {}
public:
int am;
protected:
int ai;
private:
int ax;
public:
void a_dispaly()
{
cout<< am <<endl;
cout<< ai <<endl;
cout<< ax <<endl;
}
};


class B : public A
{
public:
B (){}
B (int tam,int tai,int tax, int tbm,int tbi,int tbx) : A(tam,tai,tbx), bm(tbm),bi(tbi),bx(tbx) {}
public:
int bm;
protected:
int bi;
private:
int bx;
public:
void b_dispaly()
{
a_dispaly();
cout<< bm <<endl;
cout<< bi <<endl;
cout<< bx <<endl;
}
};

class C : public B
{
public:
C (){}
C (int tam,int tai,int tax, int tbm,int tbi,int tbx, int tcm,int tci,int tcx) : B(tam,tai,tbx,tbm,tbi,tbx), cm(tcm),ci(tci),cx(tcx) {}
public:
int cm;
protected:
int ci;
private:
int cx;
public:
void c_dispaly()
{
b_dispaly();
cout<< cm <<endl;
cout<< ci <<endl;
cout<< cx <<endl;
}
};

int main()
{
C ob3(5,3,84, 8,1,84,9,5,9);
ob3.c_dispaly();
}



三、有子对象的多层派生类的构造函数:

#include <iostream>
using namespace std;

class A
{
public:
A(){}
A(int tam,int tai,int tax) : am(tam),ai(tai),ax(tax) {}
public:
int am;
protected:
int ai;
private:
int ax;
public:
void a_dispaly()
{
cout<< am <<endl;
cout<< ai <<endl;
cout<< ax <<endl;
}
};

class B : public A
{
public:
B (){}
B (int tam,int tai,int tax, int tbm,int tbi,int tbx) : A(tam,tai,tbx), bm(tbm),bi(tbi),bx(tbx) {}
B (int tam,int tai,int tax, int tob_Am,int tob_Ai,int tob_Ax, int tbm,int tbi,int tbx) : A(tam,tai,tax), ob_A(tob_Am,tob_Ai,tob_Ax) { bm=tbm; bi=tbi;bx=tbx; } //有子对象的派生类的构造函数
public:
int bm;
A ob_A;
protected:
int bi;
private:
int bx;
public:
void b_dispaly()
{
cout<<"子对象的public成员变量am的值是 "<<ob_A.am<<endl;
a_dispaly();
cout<< bm <<endl;
cout<< bi <<endl;
cout<< bx <<endl;
}
};

class C: public B
{
public:
C () {}
C (int tam,int tai,int tax, int tbm,int tbi,int tbx, int tcm,int tci,int tcx) : B(tam,tai,tbx,tbm,tbi,tbx), cm(tcm),ci(tci),cx(tcx) {}
C (int tam,int tai,int tax, int tob_Am,int tob_Ai,int tob_Ax, int tbm,int tbi,int tbx, int tcm,int tci,int tcx) : B(tam,tai,tbx,tob_Am,tob_Ai,tob_Ax,tbm,tbi,tbx), cm(tcm),ci(tci),cx(tcx) {}
public:
int cm;

protected:
int ci;
private:
int cx;
public:
void c_dispaly()
{
b_dispaly();
cout<< cm <<endl;
cout<< ci <<endl;
cout<< cx <<endl;
}
};

int main()
{

C ob3(5,3,84, 8,1,89, 99,5,85,2,6,11);
ob3.c_dispaly();
}


四、多重继承的构造函数的书写 :

#include <iostream>
using namespace std;

class A
{
public:
A (){}
A (int tam): am(tam) {}
public:
int am;
};

class B
{
public:
B (){}
B (int tbm): bm(tbm) {}
public:
int bm;
};

class C
{
public:
C (){}
C (int tcm): cm(tcm) {}
public:
int cm;
};

class D : public A,public B,public C
{
public:
D (){}
D (int tm, int tdm) : A(tm),B(tm),C(tm) { dm=tdm;} //多重继承的构造函数的书写
public:
int dm;
};

int main ()
{
D ob1(5,3);
cout<<ob1.am<<endl;
cout<<ob1.bm<<endl;
cout<<ob1.cm<<endl;
cout<<ob1.dm<<endl;
}



五、基类和派生类有同名成员:

#include <iostream>
using namespace std;

class A
{
public:
A (){}
A (int tam): am(tam) {}
public:
int am;
double n;
};

class B
{
public:
B (){}
B (int tbm): bm(tbm) {}
public:
int bm;
double n;
};

class D : public A,public B
{
public:
D (){}
D (int tm, int tdm) : A(tm),B(tm) { dm=tdm;} //多重继承的构造函数的书写
public:
int dm;
double n;
};

int main ()
{
D ob1(5,3);
cout<<ob1.am<<endl;
cout<<ob1.bm<<endl;
cout<<ob1.dm<<endl;

ob1.n = 33.2; //基类和派生类有同名成员的时候,默认是派生类的成员;如果要访问基类的成员 ,则应该加上基类名 ;也就是说 ob1.n 和 ob1.D::n 是等价的。
cout<<ob1.n<<endl;
ob1.A::n = 55.2;
cout<<ob1.A::n<<endl;
}



六、无法判断是走那一条路线:

#include <iostream>
using namespace std;

class N
{
public:
double n;
};

class A : public N
{
};

class B : public N
{
};

class C : public A, public B
{
};

int main ()
{
C ob1;
ob1.B::n =5.9; //要访问的是 B 的基类成员
ob1.A::n =6.5; //要访问的是 A 的基类成员
}



七、虚基类:

#include <iostream>
using namespace std;

class N
{
public:
N () {}
N (int tnm): nm(tnm) {}
public:
int nm;
};

class A : virtual public N
{
public:
A () {}
A (int tnm): N(tnm) {}
};

class B : virtual public N
{
public:
B () {}
B (int tnm): N(tnm) {}
};

class C : public A, public B
{
public:
C () {}
C (int tnm) : N(tnm), A(tnm), B(tnm) {}
C (int tnm,int tnm1,int tnm2) : N(tnm), A(tnm1), B(tnm2) {} //在最后的派生类中不仅要负责对其直接基类进行初始化,还要负责对虚基类初始化
};

int main ()
{
C ob1(5,6,1);
cout<<ob1.nm<<endl;
}


八、指向基类对象的指针变量也可以指向派生类对象:

#include <iostream>
using namespace std;

class A
{
public:
A(int tm)
{
m=tm;
}
public:
int m;
void display1()
{
cout<<m<<endl;
}
};

class B :public A
{
public:
B(int tm,int tn) :A(tm)
{
n=tn;
}
public:
int n;
void display2()
{
A::display1();
cout<<n<<endl;
}
};

int main()
{
A ob1(4);
B ob2(3,6);
A *pa;
pa = &ob1;
pa->display1();
pa = &ob2;
pa->display1(); //输出结果是3,而不是3 6.因为ob1的类型是A,类A中没有n这个成员,只能赋值给m

B *pb;
//pb = &ob1;
//pb->display1(); //很显然,这样是不允许的、错误的
pb = &ob2;
pb->display2();
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


 

 

posted on 2011-11-14 16:14  zhiyinjixu  阅读(252)  评论(0)    收藏  举报