20240206作业

第五章  静态成员与友元

一、填空题

1、一个类的头文件如下所示,num初始化值为5,程序产生对象T,且修改num为10,并使用show()函数输出num的值10。

#include <iostream>

using namespace std;

class Test{

    private:

        static int num;

    public:

        Test(int);

        void show();

};

int Test::num=5;

Test::Test(int n){ num=n;}

void Test::show(){ cout<<num<<endl;}

int main(){

    Test t(10);

    t.show();

}

2、在下面程序的底画线处填上适当的字句,使该程序执行结果为40。

#include <iostream>

using namespace std;

class Test{

    public:

        static int x;

        Test(int i=0){ x=i+x;}

        int Getnum(){ return Test::x+7;}

};

int Test::x=33;

int main(){

    Test test;

    cout<<test.Getnum()<<endl;

}

3、下列程序运行的结果是___________

#include <iostream>

#include <string.h>

#include <iomanip>

using namespace std;

class student{

        char name[8];

        int deg;

        char level[7];

        friend class process// 说明友元类

    public:

        student(char na[],int d){

            strcpy(name,na);

            deg=d;

        }

};

class process{

    public:

        void trans(student &s){

            int i=s.deg/10;

            switch(i){

                case 9:

                    strcpy(s.level"优");break;

                case 8:

                    strcpy(s.level,"良");break;

                case 7:

                    strcpy(s.level,"中");break;

                case 6:

                    strcpy(s.level,"及格");break;

                default:

                    strcpy(s.level,"不及格");

            }

        }

        void show(student &s){

            cout<<setw(10)<<s.name<<setw(4)<<s.deg<<setw(8)<<s.level<<endl;

        }

};

int main(){

    student st[]={ student("张三",78),student("李四",92),student("王五",62),student("孙六",88)};

    process p;

    cout<<"结 果:"<<"姓名"<<setw(6)<<"成绩"<<setw(8)<<"等级"<<endl;

    for(int i=0;i<4;i++){

        p.trans(st[i]);

        p.show(st[i]);

    }

}

/*

结 果:姓名  成绩    等级

      张三  78      中

      李四  92      优

      王五  62    及格

      孙六  88      良

*/

二、编程题

1.

 (1) 编写一个类,声明一个数据成员和一个静态数据成员。让构造函数初始化数据成员,并把静态数据成员加1,让析构函数把静态数据成员减1。

 (2) 根据(1)编写一个应用程序,创建三个对象,然后显示它们的数据成员和静态数据成员,再析构每个对象,并显示它们对静态数据成员的影响。

 (3) 修改(2),让静态成员函数访问静态数据成员,并让静态数据成员是保户的。

#include <iostream>

using namespace std;

class Student{

private:

    string name;

    static int age;

public:

    Student(string n,int a):name(n){

        Student::age=a+1;

    }

    ~Student(){

        Student::age--;

        cout << "~Student name=" << name << " age=" << age << endl;

    }

    void show(){

        cout << "name=" << name << " age=" << age << endl;

    }

};

int Student::age=0;

int main(){

    Student s1("meili",18);

    s1.show();

    Student s2("fugui",19);

    s2.show();

    Student s3("zhaocai",20);

    s3.show();

}

2. 

 (1) 下述代码有何错误,改正它。

#include <iostream>

using namespace std;

class Animal;

void SetValue(Animal&, int);

void SetValue(Animal&, intint);

class Animal{

    public:

        friend void SetValue(Animal&, int);

        friend void SetValue(Animaltaint twint tn);

    protected:

        int itsWeight;

        int itsAge;

};

void SetValue(Animaltaint tw){

    ta.itsWeight = tw;

}

void SetValue(Animaltaint twint tn){

    ta.itsWeight = tw;

    ta.itsAge = tn;

}

int main(){

    Animal peppy;

    SetValue(peppy, 5);

    SetValue(peppy, 7,9);

  return 0;

}

 

  1.  将上面程序中的友员改成普通函数,为此增加访问类中保护数据的成员函数。

#include <iostream>

using namespace std;

class Animal;

void SetValue(Animal&, int);

void SetValue(Animal&, intint);

class Animal{

    public:

        void SetValue(Animal&, int);

        void SetValue(Animaltaint twint tn);

    protected:

        int itsWeight;

        int itsAge;

};

void Animal::SetValue(Animaltaint tw){

    ta.itsWeight = tw;

}

void Animal::SetValue(Animaltaint twint tn){

    ta.itsWeight = tw;

    ta.itsAge = tn;

}

int main(){

    Animal peppy;

    SetValue(peppy, 5);

    SetValue(peppy, 7,9);

    return 0;

}

3. 重新编写下述程序,使函数Leisure()成为类Car和类Boat的函数。作为重新编程,在类Car和类Boat中,增加函数set()。

#include <iostream>

using namespace std;

class Boat;

class Car{

    public:

        Car(int j){ size = j;}

        friend int Leisure(int timeCaraobjBoatbobi);

    protected:

        int size;

};

class Boat{

    public:

        Boat(int j){ size = j;}

        friend int Leisure(int timeCaraobjBoatbobj);

    protected:

        int size;

};

int Leisure(int timeCaraobjBoatbobi){

    return time * aobj.size * bobi.size;

}

int main(){

    Car c1(2);

    Boat b1(3);

    int time = 4;

    cout << Leisure(time,c1,b1<<endl;

    return 0;

}

 

相关推荐

  1. 20240206作业

    2024-02-08 07:44:05       19 阅读
  2. 20240203作业

    2024-02-08 07:44:05       29 阅读
  3. 20240208作业

    2024-02-08 07:44:05       28 阅读
  4. redis20240306

    2024-02-08 07:44:05       16 阅读
  5. 20240202 大模型快讯

    2024-02-08 07:44:05       27 阅读
  6. 20240204进程间通信

    2024-02-08 07:44:05       32 阅读
  7. 20240205 大模型快讯

    2024-02-08 07:44:05       34 阅读
  8. 20240208问题解决

    2024-02-08 07:44:05       31 阅读
  9. PMP考试之20240209

    2024-02-08 07:44:05       29 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-02-08 07:44:05       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-02-08 07:44:05       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-02-08 07:44:05       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-02-08 07:44:05       20 阅读

热门阅读

  1. QT学习(五)C++函数重载

    2024-02-08 07:44:05       31 阅读
  2. Redis——面试+思想+应用

    2024-02-08 07:44:05       32 阅读
  3. 【Eclipse插件开发】3工作台workbench探索【下篇】

    2024-02-08 07:44:05       34 阅读
  4. 算法学习系列(三十二):背包问题

    2024-02-08 07:44:05       27 阅读
  5. 隐私计算技术创新赋能金融数字化转型

    2024-02-08 07:44:05       37 阅读
  6. C++之多线程(multi-thread)

    2024-02-08 07:44:05       42 阅读
  7. PostgreSQL不停机迁移数据

    2024-02-08 07:44:05       26 阅读
  8. sklearn.preprocessing 特征编码汇总

    2024-02-08 07:44:05       31 阅读
  9. Rust语言入门小结(第1篇)

    2024-02-08 07:44:05       30 阅读
  10. 设计模式(行为型模式)中介者模式

    2024-02-08 07:44:05       32 阅读
  11. LeetCode动态规划的解题思路

    2024-02-08 07:44:05       30 阅读
  12. HTML系列 -> <meta charset=“utf-8“>

    2024-02-08 07:44:05       33 阅读
  13. Spark的timestamp 数据时间问题

    2024-02-08 07:44:05       41 阅读
  14. ORACLE的 软 软 软 解析!

    2024-02-08 07:44:05       36 阅读