3个C++题 输出结果是什么??
1.classSample{intx,y;public:Sample(){x=y=0;}Sample(inta,intb){x=a;y=b;}~Sample(){if(x...
1.class Sample
{
int x,y;
public:
Sample(){x=y=0;}
Sample(int a,int b){x=a;y=b;}
~Sample()
{
if(x==y) cout<<"x=y"<<endl;
else cout<<"x!=y"<<endl;
}
void disp()
{
cout<<"x="<<x<<",y="<<y<<endl;
}
};
int main()
{
Sample s1;
Sample *ps2=new Sample(2,3);
s1.disp();
ps2->disp();
delete ps2;
return 0;
}
2.class A
{
private:
int a , b ;
public :
A( )
{
a=b=0;
cout <<"a="<<a<<","<<"b="<<b<<endl;
}
A(int aa, int bb): a(aa),b(bb)
{
cout <<"a="<<a<<","<<"b="<<b<<endl;
}
~A( )
{
cout<<"Destructor "<<a<<" "<<b<<endl;
}
};
int main(){
A x, y(2, 3);
return 0;
3.class A
{
private:
int a;
public:
A():a(5){}
virtual void print()const { cout<<a;}
};
class B: public A
{
private:
char b;
public:
B() { b=‘E'; }
void print() const { cout<<b; }
};
void show(A &x) { x.print(); }
int main()
{
A d1,*p;
B d2;
p=&d2;
d1.print();
d2.print();
p->print();
show(d1);
show(d2);
return 0;
} 展开
{
int x,y;
public:
Sample(){x=y=0;}
Sample(int a,int b){x=a;y=b;}
~Sample()
{
if(x==y) cout<<"x=y"<<endl;
else cout<<"x!=y"<<endl;
}
void disp()
{
cout<<"x="<<x<<",y="<<y<<endl;
}
};
int main()
{
Sample s1;
Sample *ps2=new Sample(2,3);
s1.disp();
ps2->disp();
delete ps2;
return 0;
}
2.class A
{
private:
int a , b ;
public :
A( )
{
a=b=0;
cout <<"a="<<a<<","<<"b="<<b<<endl;
}
A(int aa, int bb): a(aa),b(bb)
{
cout <<"a="<<a<<","<<"b="<<b<<endl;
}
~A( )
{
cout<<"Destructor "<<a<<" "<<b<<endl;
}
};
int main(){
A x, y(2, 3);
return 0;
3.class A
{
private:
int a;
public:
A():a(5){}
virtual void print()const { cout<<a;}
};
class B: public A
{
private:
char b;
public:
B() { b=‘E'; }
void print() const { cout<<b; }
};
void show(A &x) { x.print(); }
int main()
{
A d1,*p;
B d2;
p=&d2;
d1.print();
d2.print();
p->print();
show(d1);
show(d2);
return 0;
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询