看看这些C++程序运行的结果是什么?在线等。急!!!谢谢啦

1. 写出下面程序运行的结果。
#include <iostream.h>
#include <string.h>

class StudentID{
public:
StudentID(int id=0)
{
value=id;
cout<<"Assigning student id " <<value <<endl;
}
StudentID()
{
cout<<"Destructing id"<<value <<endl;
}
protected:
int value;
};

class Student {
public:
Student(char*pName="no name",int ssID=0):id(ssID)
{
cout<<"Constructing student "<<pName <<endl;
strncpy(name,pName,sizeof(name));
name[sizeof(name)-1]='\n';
}

,protected:
char name[20];
StudentID id;
};
void main()
{
Student s("Mary",2001);
Student t("John");
}

2. 写出下面程序运行的结果。
#include <iostream.h>
class Point
{
int x,y;
public:
Point(int x1,int y1)
{
x=x1;y=y1;
}
virtual int area() const
{
return ();
}
};
class Rect:public Point
{
int 1,w;
public:
Rect(int x1,int y1,int 11,int w1):Point(x1,y1)
{
1=11;w=w1;
}
virtual int area() const
{
return 1*w;
};
void fun(Point &p)
{
cout<<p.area() <<endl;
}
void main()
{
pret(3,4,10,7),rec2(5,10,8,16);
Point *base=&rec2;
fun(rec1);
fun(*base);
}

3. 写出下面程序运行的结果。
include <iostream.h>

template<class T>

void order(T& x, T& y, T& z)
{
T a;
if(x>y){a=x;x=y;y=a;}
if(y>z){a=y;y=z;z=a;}
if(x>y){a=x;x=y;y=a;}
}
main()
{
float x=30.6,y=20.7,z=22.3;
cout<<"x=<<x<<",y="<<y<<",z="<<z<<endl;
order(x,y,z);
cout<<"x=<<x<<",y="<<y<<",z="<<z<<endl;
}

第一个:
Assigning student id 2001
Constructing student Mary
Assigning student id 0
Constructing student John
第二个:
70
128
第三个,程序有点问题,改之后结果“
程序:#include <iostream.h>

template<class T>

void order(T& x, T& y, T& z)
{
T a;
if(x>y){a=x;x=y;y=a;}
if(y>z){a=y;y=z;z=a;}
if(x>y){a=x;x=y;y=a;}
}
main()
{
double x=30.6,y=20.7,z=22.3;
cout<<"x="<<x<<",y="<<y<<",z="<<z<<endl;
order(x,y,z);
cout<<"x="<<x<<",y="<<y<<",z="<<z<<endl;
}
运行结果:
x=30.6,y=20.7,z=22.3
x=20.7,y=22.3,z=30.6
温馨提示:答案为网友推荐,仅供参考
第1个回答  2012-03-04
这是构造函数,protected类的问题
第一个程序结果:
Assigning student id 2001
Constructing student Mary
Assigning student id 0
Constructing student John
第二个程序结果:
70
128
第三个程序结果:
x=30.6,y=20.7,z=22.3
x=20.7,y=22.3,z=30.6
第2个回答  2012-03-03
编译有错误追问

第2题中间应为以下程序:
int l,w;
public:
Rect(int x1,int y1,int l1,int w1):Point(x1,y1)
{
l=l1;w=w1;
}
virtual int area() const
{
return l*w;

相似回答