阅读以下程序并给出执行结果
1、
#include<iostream>
using namespace std;
class Add
{
private:
int x,y;
public:
Add(int a,int b)
{
x=a; y=b;
cout<<"调用构造函数1"<<endl;
}
Add(Add &p)
{
x=p.x; y=p.y;
cout<<"调用构造函数2"<<endl;
}
~Add()
{
cout<<"调用析构函数"<<endl;
}
int add(){ return x+y; }
};
void main()
{
Add p1(2,3);
Add p2(p1);
cout<<p2.add()<<endl;
}
2、
#include <iostream>
using namespace std;
class Box
{
public:
Box(int,int,int);
int volume( );
private:
int height;
int width;
int length;
};
Box∷Box(int h,int w,int len)
{
height=h;
width=w;
length=len;
}
int Box∷volume( )
{return(height*width*length);
}
int main( )
{Box box1(12,25,30);
cout<<″The volume of box1 is ″<<box1.volume( )<<endl;
Box box2(15,30,21);
cout<<″The volume of box2 is ″<<box2.volume( )<<endl;
return 0;
}
3、
#include<iostream.h>
class Point
{
private:
int X,Y;
public:
Point(int a=0,int b=0){
X=a;Y=b;
cout<<"initializing X="<<X<<",Y="<<Y<<endl;
}
Point(Point &p);
int GetX(){return X;}
int GetY(){return Y;}
void Show(){
cout<<"X="<<X<<",Y="<<Y<<endl;
}
~Point(){
cout<<"delete… "<<X<<","<<Y<<endl;
}
};
Point::Point(Point &p){
cout<<"Copy Initializing "<<p.X<<","<<p.Y<<endl;
X=p.X;
Y=p.Y;
}
void display(Point p){
p.Show();
}
void main(void)
{
Point A(24,116);
cout<<"called display(A)"<<endl;
display(A);
cout<<"out…"<<endl;
}
4、
#include <iostream.h>
class Base
{
private:
int x;
public:
Base(int a)
{
cout<<"constructing Base..."<<endl;
x=a;
}
~Base()
{
cout<<"destructing Base..."<<endl;
}
};
class Myclass
{
private:
int n;
public:
Myclass(int num)
{
n=num;
cout<<"constructing Myclass..."<<endl;
}
~Myclass()
{
cout<<"destructing Myclass..."<<endl;
}
};
class Derive:public Base
{
private:
int y;
Myclass bobj;
public:
Derive(int a,int b,int c):bobj(c),Base(a)
{
cout<<"constructing Derive..."<<endl;
y=b;
}
~Derive()
{
cout<<"destructing Derive..."<<endl;
}
};
void main()
{
Derive dobj(1,2,3);
}
阅读以下程序并填空(填上正确的语法成分),使其成为完整的程序
1、下列程序在构造函数和析构函数中申请和释放类的私有成员,请完成该类的实现。
class MyClass
{
public:
MyClass(int a);
~MyClass();
private:
int* X;
};
MyClass::MyClass(int a)
{
____(1)_____;
}
MyClass::~MyClassO
{
____(2)______;
}
2、在下面程序的横线处填上适当的语句,使该程序执行结果为10。
#include <iostream.h>
class MyClass
{
public:
___(3)____ //为x置值
___(4)____ //取x值
private:
int x;
};
void main()
{
MyClass my(10);
cout << my.GetNum() << endl;
}
3、完成下面的类定义。
class MyClass
{
public:
MyClass( ) { x = 0; }
friend int GetNum(__(5)__ my);
private:
int x;
};
int GetNum(__(5)__ my)
{
return my.x;
}
编程题
1、定义一个Point类来处理三维点points(x,y,z).该类有构造函数,拷贝构造函数,negate()成员函数将point的x,y和z值各乘-1, norm()成员函数返回该点到原点(0,0,0)的距离,一个print()成员函数显示x,y,和z的值。
2. 定义一个Person类,它的每个对象表示一个人。数据成员必须包含姓名、出生年份、死亡年份,一个构造函数,一个析构函数,读取数据的成员函数,一个print()成员函数显示所有数据。
答案
阅读以下程序并给出执行结果
1、
调用构造函数1
调用构造函数2
5
调用析构函数
调用析构函数
2、
The volume of box1 is 9000
The volume of box2 is 9450
3、
initializing X=24,Y=116
called display(A)
Copy Initializing 24,116
X=24,Y=116
delete… 24,116
out…
delete… 24,116
4、
constructing Base...
constructing Myclass...
constructing Derive...
destructing Derive...
destructing Myclass...
destructing Base...
阅读以下程序并填空(填上正确的语法成分),使其成为完整的程序
(1) X=new int(a)
(2) delete X
(3) MyClass(int a){ x=a; };
(4) int GetNum( ){ return x; }
(5) MyClass;
编程题
1、定义一个Point类来处理三维点points(x,y,z).该类有构造函数,拷贝构造函数,negate()成员函数将point的x,y和z值各乘-1, norm()成员函数返回该点到原点(0,0,0)的距离,一个print()成员函数显示x,y,和z的值。
#include <math.h>
#include <iostream.h>
class Point
{ public:
Point(float x=0, float y=0, float z=0): x(x), y(y), z(z) { }
Point(const Point& p) : x(p.x), y(p.y), z(p.z) { }
void negate() { x *= -1; y *= -1; z *= -1; }
double norm() { return sqrt(x*x + y*y + z*z); }
void print()
{ cout << '(' << x << "," << y << "," << z << ")";
}
private:
float x, y, z;
};
void main()
{ Point p(12,-3,4);
cout << "p = ";
p.print();
cout << ", p.norm() = " << p.norm() << endl;
p.negate();
cout << "p = ";
p.print();
cout << ", p.norm() = " << p.norm() << endl;
}
2. 定义一个Person类,它的每个对象表示一个人。数据成员必须包含姓名、出生年份、死亡年份,一个构造函数,一析构函数,读取数据的成员函数,一个print()成员函数显示所有数据。
#include <iostream.h>
class Person
{ public:
Person(char* =0, int =0, int =0);
~Person() { delete [] name; }
char* name() { return name; }
int born() { return yob; }
int died() { return yod; }
void print();
private:
int len;
char* name;
int yob, yod;
};
void main()
{ Person cb("Charles Babbage",1792,1871);
cb.print();
}
Person::Person(char* name, int yob, int yod)
: len(strlen(name)),
name(new char[len+1]),
yob(yob),
yod(yod)
{ memcpy(name, name, len+1);
}
void Person::print()
{ cout << "\tName: " << name << endl;
if (yob) cout << "\tBorn: " << yob << endl;
if (yod) cout << "\tDied: " << yod << endl;
}