C++程序分析题

程序分析题,写出程序结果

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


相关文章

  • C++课程设计任务书
  • <C++面向对象课程设计>任务书 一.课程设计目的与要求 1.课程设计目的 面向对象程序设计作为一门软件设计的课程,具有极强的实践性,必须使学生具备灵活应用理论知识的能力及面向对象程序设计技能.所以在<C++面向对象程序设 ...查看


  • 逆向 C -- 识别类及其构造函数 - FISH 的专栏 - CSDNBlog
  • 逆向 C++ 这些年来,逆向工程分析人员一直是凭借着汇编和 C 的知识对大多数软件进行逆向工程的,但是,现在随着越来越多的应用程序和恶意软件转而使用 C++语言进行开发,深入理解 C++ 面向对象方式开发的软件的反汇编技术就显得越发的必要. ...查看


  • 为何要学编程
  • 一.为何要学编程? 每个人的动机不一样.大致有: 1.为了找个好工作:或为了有更好的机会和更好的发展. 2.看到别人超厉害,所以也想学. 3.实际工作中很多场合需要. 4.从小就立志做个程序员,做软件工程师. 5.振兴中国的软件事业. .. ...查看


  • 高校学生综合测评管理系统的设计与实现说明书
  • 'i******************* 实践教学 ******************* 题 目:高校学生综合测评管理系统的设计与实现专业班级:姓 名:学 号:指导教师:成 绩:兰州理工大学 计算机与通信学院 2011年秋季学期 课程设 ...查看


  • 请不要做个浮躁的人
  • 1. 把C++当成一门新的语言学习(和C 没啥关系!真的.): 2. 看++><Thinking In C,不要看<C++变成死相>: 3. 看<The C++ Programming Language> ...查看


  • 机械管理系统说明书之系统开发技术的分析
  • 2 系统开发技术的分析 系统客户端界面采用Visual C++ 6.0,后台数据库使用SQL SERVER 2000作为数据库服务器,下面将对前台开发工具Visual C++ 6.0,后台数据库Microsoft SQL Server 20 ...查看


  • 中南大学c++实践报告
  • 中南大学 本科生课程设计(实践)任务书.设计报告 (<计算机程序设计基础>C++) 题 目 图书馆藏书基本信息管理软件设计 学生姓名 李泽洲 指导教师 朱从旭 老师 学 院 地球科学与物理信息学院 专业班级 地质工程1403 学 ...查看


  • 面向对象程序设计课程简介
  • 课程编码: 课程名称:面向对象程序设计 英文名称:Object-Oriented Programming and Design 学分:3 总学时:48 面向对象:软件工程专业本科生 先修课程:程序设计基础 考核形式:笔试 课程简介:(250 ...查看


  • C++同步练习册答案
  • C++同步练习册答案 (未完,待续-) 第一章 一.单项选择 ADBBDDCCAACDBDDCABCADDADB 二.简答题 1. 0 2. x==3||y!=5 3. x 4. 0 5. a==b的值为0,因为"==" ...查看


  • 计算机程序设计基础习题册答案
  • <计算机程序设计基础> 习题册 班级 学号 姓名 成绩 一. 单选题 1. C++源程序文件的默认扩展名为 A . A) cpp B) exe C) obj D) lik 2. 由C++源程序文件编译而成的目标文件的默认扩展名为 ...查看


热门内容