改好了,自己对照:
#include
#include
class P
{
public:
P(int p1,int p2){pri1=p1;pri2=p2;}
int inc1(){return ++pri1;}
int inc2(){return ++pri2;}
void display(){cout<<"pri1="<
int pri1,pri2;
};
class D1:virtual private P
{
public:
D1(int p1,int p2,int p3):P(p1,p2){pri3=p3;}
int inc1(){return P::inc1();}
int inc3(){return ++pri3; }
void display(){P::display();cout<<"pri3="<
int pri3;
};
class D2:virtual public P
{public:
D2(int p1,int p2,int p4):P(p1,p2){pri4=p4;}
int inc1() {P::inc1();P::inc2();return P::inc1();}
int inc4(){return ++pri4;}
void display(){P::display();cout<<"pri4="<
int pri4;
};
class D12:private D1,public D2
{
public:
D12(int p11,int p12,int p13,int p21,int p22,int p23,int p):D1(p11,p12,p13),D2(p21,p22,p23),P(p11,p21)
{pri12=p;}
int inc1() {D2::inc1();return D2::inc1();}
int inc5() {return ++pri12;}
void display()
{
cout<<"D2::display()"<
cout<<"pri12="<
private:
int pri12;
};
void main()
{freopen("mynum.txt","w",stdout);
D12 d(1,2,3,4,5,6,7);
d.display();
d.inc1();
d.inc4();
d.inc5();
d.D12::inc1();
d.display();
}
运行:
D2::display()
pri1=1pri2=4
pri4=6
pri12=7
D2::display()
pri1=9pri2=8
pri4=7
pri12=8
#include
#include "stdafx.h"
using namespace std;
class P
{
public:
P(int p1,int p2){pri1=p1;pri2=p2;}
int inc1(){return ++pri1;}
int inc2(){return ++pri2;}
void display(){cout<<"pri1="<
int pri1;
int pri2;
};
class D1:virtual private P
{
public:
D1(int p1,int p2,int p3):P(p1,p2){pri3=p3;}
int inc1(){return P::inc1();}
int inc3(){return ++pri3;)
void display(){P::display();cout<<"pri3="<
int pri3;
};
class D2:virtual public P
{public:
D2(int p1,int p2,int p4):P(p1,p2){pri4=p4;}
int inc1() {P::inc1();P::inc2();return P::inc1();}
int inc4(){return ++pri4;}
void display(){P::display();cout<<"pri4="<
int pri4;
};
class D12:public D1,public D2
{
public:
D12(int p11,int p12,int p13,int p21,int p22,int p23,int p):D1(p11,p12,p13),D2(p21,p22,p23),P(p11,p21)
{pri12=p;}
int inc1() {D2::inc1();return D2::inc1();}
int inc5() {return ++pri12;}
void display()
{
cout<<"D2::display()"<
cout<<"pri12="<
private:
int pri12;
};
void main()
{
D12 d(1,2,3,4,5,6,7);
d.display();
d.inc1();
d.inc4();
d.inc5();
d.D12::inc1();
d.display();
}