如下C++程序中,类D中的私有成员为类C的对象,但是类C中并未声明D是C的友元类,为什么程序依旧运行正常?
#include<iostream.h>classA{public:A(inti){a=i;cout<<"Constructorcalled.A\n";}~A(){cou...
#include <iostream.h>
class A
{
public:
A(int i)
{
a=i;
cout<<"Constructor called.A\n";
}
~A()
{ cout<<"Destructor called.A\n"; }
void Print()
{ cout<<a<<endl; }
private:
int a;
};
class B
{
public:
B(int i)
{
b=i;
cout<<"Constructor called.B\n";
}
~B()
{ cout<<"Destructor called.B\n"; }
void Print()
{ cout<<b<<endl; }
private:
int b;
};
class C
{
public:
C(int i)
{
c=i;
cout<<"Constructor called.C\n";
}
~C()
{ cout<<"Destructor called.C\n"; }
int Getc()
{ return c; }
private:
int c;
};
class D:public A,public B
{
public:
D(int i,int j,int k,int l):B(i),A(j),c(l)
{
d=k;
cout<<"Constructor called.D\n";
}
~D()
{ cout<<"Destructor called.D\n"; }
void Print()
{
A::Print();
B::Print();
cout<<d<<','<<c.Getc()<<endl;
}
private:
int d;
C c;
};
void main()
{
D d(5,6,7,8);
d.Print();
B b(2);
b=d;
b.Print();
} 展开
class A
{
public:
A(int i)
{
a=i;
cout<<"Constructor called.A\n";
}
~A()
{ cout<<"Destructor called.A\n"; }
void Print()
{ cout<<a<<endl; }
private:
int a;
};
class B
{
public:
B(int i)
{
b=i;
cout<<"Constructor called.B\n";
}
~B()
{ cout<<"Destructor called.B\n"; }
void Print()
{ cout<<b<<endl; }
private:
int b;
};
class C
{
public:
C(int i)
{
c=i;
cout<<"Constructor called.C\n";
}
~C()
{ cout<<"Destructor called.C\n"; }
int Getc()
{ return c; }
private:
int c;
};
class D:public A,public B
{
public:
D(int i,int j,int k,int l):B(i),A(j),c(l)
{
d=k;
cout<<"Constructor called.D\n";
}
~D()
{ cout<<"Destructor called.D\n"; }
void Print()
{
A::Print();
B::Print();
cout<<d<<','<<c.Getc()<<endl;
}
private:
int d;
C c;
};
void main()
{
D d(5,6,7,8);
d.Print();
B b(2);
b=d;
b.Print();
} 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询