前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >计算机二级大题.

计算机二级大题.

作者头像
热心的社会主义接班人
发布2018-04-27 14:38:49
6270
发布2018-04-27 14:38:49
举报
文章被收录于专栏:cscs

1题

代码语言:javascript
复制
#include <iostream>
using namespace std;
class MyClass {
public:
    MyClass(int len)
    {
       array = new int[len];
        arraySize = len;
        for(int i = 0; i < arraySize; i++)
            array[i] = i+1;
    }

    ~MyClass()
    {
// ERROR   **********found**********
        delete []array;
    }

    void Print() const
    {
        for(int i = 0; i < arraySize; i++)
// ERROR   **********found**********
            cout<< array[i] << ' ';

        cout << endl;
    }
private:
    int *array;
    int arraySize;
};

int main()
{
// ERROR   **********found**********
    MyClass obj(10);

    obj.Print();
    return 0;
}
代码语言:javascript
复制
D:\untitled2\cmake-build-debug\untitled2.exe
1 2 3 4 5 6 7 8 9 10

2

代码语言:javascript
复制
#include <iostream>
using namespace std;
class vehicle
{
private:
    int MaxSpeed;
    int Weight;
public:
    //**********found**********
    vehicle(int maxspeed, int weight):MaxSpeed(maxspeed),Weight(weight)
    {
    };
    ~vehicle(){};
    int getMaxSpeed() { return MaxSpeed; }
    int getWeight() { return  Weight; }
};

//**********found**********
class bicycle : virtual public vehicle
{
private:
int Height;
public:
bicycle(int maxspeed, int weight, int height):vehicle(maxspeed, weight),Height(height){}
int getHeight(){ return Height; };
};

//**********found**********
class motorcar : virtual public vehicle
{
private:
int SeatNum;
public:
motorcar(int maxspeed, int weight, int seatnum):vehicle(maxspeed, weight),SeatNum(seatnum){}
int getSeatNum(){ return SeatNum; };
};

//**********found**********
class motorcycle : public motorcar,public bicycle
{
public:
    motorcycle(int maxspeed, int weight, int height):
            vehicle(maxspeed, weight),bicycle(maxspeed, weight,height),motorcar(maxspeed, weight, 1){}
};

int main()
{
    motorcycle a(80,150,100);
    cout<<a.getMaxSpeed()<<endl;
    cout<<a.getWeight()<<endl;
    cout<<a.getHeight()<<endl;
    cout<<a.getSeatNum()<<endl;
    return 0;
}
代码语言:javascript
复制
80
150
100
1

3

代码语言:javascript
复制
#include<iostream>
using namespace std;
class Xabc {
    int *a; int n;
public:
    Xabc(int aa[], int nn) {  //构造函数
        // ERROR **********found**********
        n=nn;
        // ERROR **********found**********
        a=new int[n];
        for(int i=0; i<n; i++) a[i]=aa[i];
    }
    int GetA(int i) {return a[i];}
    ~Xabc() { delete []a;}
};

int main() {
    int a[5]={2,3,4,5,6};
    Xabc x(a,5);
    int i, s=0;
    // ERROR **********found**********
    for(int i=0; i<5; i++) s+=x.GetA(i);
    cout<<"s="<<s<<endl;

    return 0;
}
代码语言:javascript
复制
D:\untitled4\cmake-build-debug\untitled4.exe
s=20

4

代码语言:javascript
复制
#include<iostream>
#include<string.h>
using namespace std;

class Date {            // 日期类
    int year, month, day;  // 年、月、日
public:
    Date(int year, int month, int day) :year(year), month(month), day(day) {}
    int getYear()const { return year; }
    int getMonth()const { return month; }
    int getDay()const { return day; }
};

class Person {       // 人员类
    char name[14];    // 姓名
    bool is_male;     // 性别,为 true 时表示男性
    Date birth_date;  // 出生日期
public:
    Person(char *name, bool is_male, Date birth_date)
        //**********found**********
        :is_male(is_male), birth_date(birth_date)
    {
        strcpy_s(this->name, name);
    }
    const char *getName()const { return name; }

    bool isMale()const { return is_male; }

    Date getBirthdate()const { return birth_date; }

    //利用strcmp()函数比较姓名,返回一个正数、0 或负数,分别表示大于、等于、小于
    int compareName(const Person &p)const {
        //**********found**********
        return strcmp(name, p.name);
    }

    void show() {
        cout << endl;
        cout << name << ' ' << (is_male ? "男" : "女") << ' ' << "出生日期:"
            << birth_date.getYear() << "年"       //显示出生年
                                                 //**********found**********
            << birth_date.getMonth() //显示出生月
            << birth_date.getDay() << "日";       //显示出生日
    }
};

void sortByName(Person ps[], int size) {  //将人员数组按姓名排列为升序
    for (int i = 0; i<size - 1; i++) {         //采用选择排序算法
        int m = i;
        for (int j = i + 1; j<size; j++)
            if (ps[j].compareName(ps[m])<0) m = j;
        if (m>i) {
            Person p = ps[m];
            ps[m] = ps[i];
            ps[i] = p;
        }
    }
}

int main() {
    Person staff[] = {
        Person("张三", true, Date(1978, 4, 20)),
        Person("王五", false, Date(1965,8,3)),
        Person("杨六", false, Date(1965,9,5)),
        Person("李四", true, Date(1973,5,30))
    };

    const int size = sizeof(staff) / sizeof(staff[0]);
    int i;
    cout << endl << "按姓名排序";
    cout << endl << "排序前:";
    for (i = 0; i<size; i++) staff[i].show();

    sortByName(staff, size);

    cout << endl << endl << "排序后:";
    for (i = 0; i<size; i++) staff[i].show();

    cout << endl;
    return 0;
}

结果

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018.03.22 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档