程序分析题,写出程序结果
1. 写出程序中列出的各数据成员和成员函数的访问属性。
class A
{public: int i;
protected: void f2( ); int j;
private: int k;
};
class B: public A
{public: void f3( );
protected: void f4( );
private: int m;
};
class C: protected B
{public: void f5( );
private: int n;
};
2. #include
using namespace std;
class goods {
private: static int totalweight;
int weight;
public: goods(int w) { weight=w; totalweight+=weight; }
goods(goods &gd) { weight=gd.weight; totalweight+=weight; }
~goods() { totalweight-=weight; }
static int gettotal() { return totalweight; }
};
int goods::totalweight=0;
void main() {
goods g1(50);
cout
goods g2(100);
cout
}
3. #include
class A
{
int x,y,z;
public:
A(int vx,int vy,int vz) { x=vx;y=vy;z=vz;}
A() { x=0;y=0;z=0;}
A operator+(A t)
{
A te;
te.x=x+t.z;
te.y=y+t.y;
te.z=z+t.z;
return te;
}
A operator-(A t)
{
A te;
te.x=x-t.x;
te.y=y-t.y;
te.z=z-t.z;
return te;
}
A operator=(A t)
{
x=t.x;
y=t.y;
z=t.z;
return *this;
}
void print() { cout
};
void main()
{
A t1(10,10,10),t2(20,20,20),t3;
t3=t1+t2;
t3.print();
t3=t2=t1;
t1.print();
t2.print();
t3.print();
}
4. #include
class Bclass
{ public:
Bclass( int i, int j ) { x = i; y = j; }
virtual int fun() { return 0 ; }
protected: int x, y ;
};
class Iclass:public Bclass
{ public :
Iclass(int i, int j, int k):Bclass(i, j) { z = k; }
int fun() { return ( x + y + z ) / 3; }
private : int z ;
};
void main()
{ Iclass obj( 2, 4, 10 );
}
5. #include
class BASE1
{ public: BASE1( int i ) { cout
};
class BASE2
{ public: BASE2( int j ) { cout
};
class A: public BASE1, public BASE2
{ public:
A( int a, int b, int c, int d ) : BASE2(b), BASE1(c), b2(a), b1(d)
{ cout
private:
} ;
void main()
{ A obj( 1, 2, 3, 4 ); }
6. #include
class T
{ public:
T(int x){ a=x; b+=x;}; static void display(T c) { cout fun()
private:
} ; int a; static int b;
int T::b=5;
void main()
{ T A(3), B(5);
}
7. #include
using namespace std;
class M
{
public:
M() { x=y=0; }
M(int i,int j) { x=i; y=j; }
void copy(M *m) { x=m->x; y=m->y; }
void setxy(int i,int j) { x=i; y=j; }
void print() { cout
private:
int x,y;
};
void fun(M m1,M *m2)
{
m1.setxy(12,15);
m2->setxy(22,25);
}
void main()
{
M p(5,7),q;
q.copy(&p);
fun(p,&q);
p.print();
q.print();
} T::display(A); T::display(B);
8. #include
class point
{
public:
point(int i=0,int j=0) { x0=i; y0=j; }
virtual void set()=0;
virtual void draw()=0;
protected:
int x0,y0;
};
class line:public point
{
public:
line(int i=0,int j=0,int m=0,int n=0):point(i,j) { x1=m; y1=n; }
void set() { cout
void draw() { cout
protected:
int x1,y1;
};
class ellipse:public point
{
public:
ellipse(int i=0,int j=0,int p=0,int q=0):point(i,j) { x2=p; y2=q; }
void set() { cout
void draw() { cout
protected:
int x2,y2;
};
void drawobj(point *p) { p->draw(); }
void setobj(point *p) { p->set(); }
void main(void)
{
point *pointObj;
line lineObj;
ellipse ellObj;
pointObj = &lineObj;
pointObj->draw();
cout
pointObj = &ellObj;
pointObj->set();
}
9. #include
class vehicle
{
int MaxSpeed;
int Weight;
public:
vehicle() { MaxSpeed=0; Weight=0; }
virtual ~vehicle() { Stop(); }
virtual void Stop() { cout
};
class bicycle:virtual public vehicle
{
int Height;
public:
~bicycle() { Stop(); }
void Stop() { cout
class motorcar:virtual public vehicle
{
int SeatNum;
public:
~motorcar() { Stop(); }
void Stop() { cout
};
class motorcycle:public bicycle,public motorcar
{
public:
~motorcycle() { Stop(); }
void Stop() { cout
};
void main()
{
vehicle *ptr;
motorcycle d;
ptr=&d;
ptr->Stop();
}
四 程序设计题
1. 从数据文件f1.dat (ASCII 文件)中读入10个整数放在数组中,找出并输出10个数中的最大者和它在数组中的序号。
2. 定义一个处理日期的类TDate ,它有3个私有数据成员:Month ,Day ,Y ear 和若干个公有成员函数,并实现如下要求:(1)成员函数设置缺省参数;(2)定义一个友元函数来打印日期。
3. 有一个Time 类,包含数据成员minute (分)和 sec (秒),模拟秒表,每次走一秒,满60秒进一分钟,此时秒又从0开始算。要求输出分和秒的值。(要对++进行重载)
4. 设计一个程序,定义抽象基类Shape ,由它派生出2个类:Circle (圆形)、Rectangle (矩形)。使用这2个类分别定义一个对象,用虚函数实现计算这2对象的图形面积,并计算它们的和。要求使用基类指针数组,使用它的每个元素指向一个派生类对象。
1. #include
int main( )
{int a[10],max,i,order;
ifstream infile(″f1.dat″,ios::in|ios::nocreate);
if(!infile) {cerr
}
for(i=0;i
{infile>>a[i]; coutcout
max=a[0];order=0;
for(i=1;i
if(a[i]>max) {max=a[i]; order=i; }
cout
infile.close();
return 0;
}
2. #include
using namespace std;
class Date //声明Date 类
{public:
Date(int m = 6,int d = 15,int y=2011);
friend void display(Date &); //声明Time 中的display 函数为友元成员函数
private:
int month;
int day;
int year;
};
Date::Date(int m,int d,int y) //类Date 的构造函数 {month=m;
day=d;
year=y;
}
void display(Date &date)
{
cout
}
void main()
{
Date d1(5,13,2010);
display(d1);
Date d2;
display(d2);
Date d3(4,15);
display(d3);
}
3. #include
using namespace std;
class Time
{public:
Time( ){minute=0;sec=0;} Time(int m,int s):minute(m),sec(s){ } Time operator++( ); void display( ){cout
int minute;
int sec;
};
Time Time::operator++( ) "
{ if(++sec>=60) {sec-=60; ++minute;}
return *this; }
int main( )
{
}
4. #include
#include
class Shape
{public:
virtual double area( ) const =0; //虚函数 };
class Circle:public Shape
{public:
Circle(float r=0){radius = r;}
double area( ) const;
protected:
float radius;
};
double Circle::area() const
{ return 3.14159*radius*radius; }
class Rectangle:public Shape
{
public:
Rectangle(float len, float wid){length = len; width = wid;} double area() const; Time time1(34,0); for (int i=0;i
protected:
float length;
float width;
};
double Rectangle::area() const
{ return length*width; }
void main()
{
Shape *pShape[3]; Circle cir(10); Rectangle rect(30,10); double allArea; pShape[0] = ○
} cout area()area()area() + pShape[1]->area() ; cout
程序分析题,写出程序结果
1. 写出程序中列出的各数据成员和成员函数的访问属性。
class A
{public: int i;
protected: void f2( ); int j;
private: int k;
};
class B: public A
{public: void f3( );
protected: void f4( );
private: int m;
};
class C: protected B
{public: void f5( );
private: int n;
};
2. #include
using namespace std;
class goods {
private: static int totalweight;
int weight;
public: goods(int w) { weight=w; totalweight+=weight; }
goods(goods &gd) { weight=gd.weight; totalweight+=weight; }
~goods() { totalweight-=weight; }
static int gettotal() { return totalweight; }
};
int goods::totalweight=0;
void main() {
goods g1(50);
cout
goods g2(100);
cout
}
3. #include
class A
{
int x,y,z;
public:
A(int vx,int vy,int vz) { x=vx;y=vy;z=vz;}
A() { x=0;y=0;z=0;}
A operator+(A t)
{
A te;
te.x=x+t.z;
te.y=y+t.y;
te.z=z+t.z;
return te;
}
A operator-(A t)
{
A te;
te.x=x-t.x;
te.y=y-t.y;
te.z=z-t.z;
return te;
}
A operator=(A t)
{
x=t.x;
y=t.y;
z=t.z;
return *this;
}
void print() { cout
};
void main()
{
A t1(10,10,10),t2(20,20,20),t3;
t3=t1+t2;
t3.print();
t3=t2=t1;
t1.print();
t2.print();
t3.print();
}
4. #include
class Bclass
{ public:
Bclass( int i, int j ) { x = i; y = j; }
virtual int fun() { return 0 ; }
protected: int x, y ;
};
class Iclass:public Bclass
{ public :
Iclass(int i, int j, int k):Bclass(i, j) { z = k; }
int fun() { return ( x + y + z ) / 3; }
private : int z ;
};
void main()
{ Iclass obj( 2, 4, 10 );
}
5. #include
class BASE1
{ public: BASE1( int i ) { cout
};
class BASE2
{ public: BASE2( int j ) { cout
};
class A: public BASE1, public BASE2
{ public:
A( int a, int b, int c, int d ) : BASE2(b), BASE1(c), b2(a), b1(d)
{ cout
private:
} ;
void main()
{ A obj( 1, 2, 3, 4 ); }
6. #include
class T
{ public:
T(int x){ a=x; b+=x;}; static void display(T c) { cout fun()
private:
} ; int a; static int b;
int T::b=5;
void main()
{ T A(3), B(5);
}
7. #include
using namespace std;
class M
{
public:
M() { x=y=0; }
M(int i,int j) { x=i; y=j; }
void copy(M *m) { x=m->x; y=m->y; }
void setxy(int i,int j) { x=i; y=j; }
void print() { cout
private:
int x,y;
};
void fun(M m1,M *m2)
{
m1.setxy(12,15);
m2->setxy(22,25);
}
void main()
{
M p(5,7),q;
q.copy(&p);
fun(p,&q);
p.print();
q.print();
} T::display(A); T::display(B);
8. #include
class point
{
public:
point(int i=0,int j=0) { x0=i; y0=j; }
virtual void set()=0;
virtual void draw()=0;
protected:
int x0,y0;
};
class line:public point
{
public:
line(int i=0,int j=0,int m=0,int n=0):point(i,j) { x1=m; y1=n; }
void set() { cout
void draw() { cout
protected:
int x1,y1;
};
class ellipse:public point
{
public:
ellipse(int i=0,int j=0,int p=0,int q=0):point(i,j) { x2=p; y2=q; }
void set() { cout
void draw() { cout
protected:
int x2,y2;
};
void drawobj(point *p) { p->draw(); }
void setobj(point *p) { p->set(); }
void main(void)
{
point *pointObj;
line lineObj;
ellipse ellObj;
pointObj = &lineObj;
pointObj->draw();
cout
pointObj = &ellObj;
pointObj->set();
}
9. #include
class vehicle
{
int MaxSpeed;
int Weight;
public:
vehicle() { MaxSpeed=0; Weight=0; }
virtual ~vehicle() { Stop(); }
virtual void Stop() { cout
};
class bicycle:virtual public vehicle
{
int Height;
public:
~bicycle() { Stop(); }
void Stop() { cout
class motorcar:virtual public vehicle
{
int SeatNum;
public:
~motorcar() { Stop(); }
void Stop() { cout
};
class motorcycle:public bicycle,public motorcar
{
public:
~motorcycle() { Stop(); }
void Stop() { cout
};
void main()
{
vehicle *ptr;
motorcycle d;
ptr=&d;
ptr->Stop();
}
四 程序设计题
1. 从数据文件f1.dat (ASCII 文件)中读入10个整数放在数组中,找出并输出10个数中的最大者和它在数组中的序号。
2. 定义一个处理日期的类TDate ,它有3个私有数据成员:Month ,Day ,Y ear 和若干个公有成员函数,并实现如下要求:(1)成员函数设置缺省参数;(2)定义一个友元函数来打印日期。
3. 有一个Time 类,包含数据成员minute (分)和 sec (秒),模拟秒表,每次走一秒,满60秒进一分钟,此时秒又从0开始算。要求输出分和秒的值。(要对++进行重载)
4. 设计一个程序,定义抽象基类Shape ,由它派生出2个类:Circle (圆形)、Rectangle (矩形)。使用这2个类分别定义一个对象,用虚函数实现计算这2对象的图形面积,并计算它们的和。要求使用基类指针数组,使用它的每个元素指向一个派生类对象。
1. #include
int main( )
{int a[10],max,i,order;
ifstream infile(″f1.dat″,ios::in|ios::nocreate);
if(!infile) {cerr
}
for(i=0;i
{infile>>a[i]; coutcout
max=a[0];order=0;
for(i=1;i
if(a[i]>max) {max=a[i]; order=i; }
cout
infile.close();
return 0;
}
2. #include
using namespace std;
class Date //声明Date 类
{public:
Date(int m = 6,int d = 15,int y=2011);
friend void display(Date &); //声明Time 中的display 函数为友元成员函数
private:
int month;
int day;
int year;
};
Date::Date(int m,int d,int y) //类Date 的构造函数 {month=m;
day=d;
year=y;
}
void display(Date &date)
{
cout
}
void main()
{
Date d1(5,13,2010);
display(d1);
Date d2;
display(d2);
Date d3(4,15);
display(d3);
}
3. #include
using namespace std;
class Time
{public:
Time( ){minute=0;sec=0;} Time(int m,int s):minute(m),sec(s){ } Time operator++( ); void display( ){cout
int minute;
int sec;
};
Time Time::operator++( ) "
{ if(++sec>=60) {sec-=60; ++minute;}
return *this; }
int main( )
{
}
4. #include
#include
class Shape
{public:
virtual double area( ) const =0; //虚函数 };
class Circle:public Shape
{public:
Circle(float r=0){radius = r;}
double area( ) const;
protected:
float radius;
};
double Circle::area() const
{ return 3.14159*radius*radius; }
class Rectangle:public Shape
{
public:
Rectangle(float len, float wid){length = len; width = wid;} double area() const; Time time1(34,0); for (int i=0;i
protected:
float length;
float width;
};
double Rectangle::area() const
{ return length*width; }
void main()
{
Shape *pShape[3]; Circle cir(10); Rectangle rect(30,10); double allArea; pShape[0] = ○
} cout area()area()area() + pShape[1]->area() ; cout