写出下面程序的执行结果:1)#include <ios
写出下面程序的执行结果:
1)
#include <iostream>
using namespace std;
class A
{
friend double count(A&);
public:
A(double t, double r):total(t),rate(r){}
private:
double total;
double rate;
};
double count(A& a)
{
a. total+=a.rate*a.total;
return a.total;
}
int main(void)
{
A x(80,0.5),y(100,0.2);
cout<<count(x)<<','<<count(y)<<'\n';
cout<<count(x)<<'\n';return 0;
}2)
#include<iostream>
using namespace std;
class Count
{
private:
static int counter;
int obj_id;
public:
Count(); //constructor
static void display_total(); //static function
void display();
~Count(); //destructor
};
int Count::counter; //definition of static data member
Count::Count() //constructor
{
counter++;
obj_id = counter;
}
Count::~Count() //destructor
{
counter--;
cout<<"Object number "<<obj_id<<" being destroyed\n";
}
void Count::display_total() //static function
{
cout <<"Number of objects created is = "<<counter<<endl;
}
void Count::display()
{
cout << "Object ID is "<<obj_id<<endl;
}
int main(void)
{
Count a1;
Count::display_total();
Count a2, a3,a4;
Count::display_total();
a2.display();
a4.display();
return 0;
}3)
#include <iostream >
using namespace std;
class BASE
{
char c;
public:
BASE(char n):c(n){}
virtual ~BASE(){cout<<c;}
};
class DERIVED:public BASE
{
char c;
public:
DERIVED(char n):BASE(n+1),c(n){}
~DERIVED(){cout<<c;}
};
int main(void)
{
DERIVED('X');
return 0;
}答案
第1空:120,120
180
第2空:Number of objects created is = 1
Number of objects created is = 4
Object ID is 2
Object ID is 4
Object number 4 being destroyed
Object number 3 being destroyed
Object number 2 being destroyed
Object number 1 being destroyed
第3空:XY