20240206作业

2024-02-07 14:36
文章标签 作业 20240206

本文主要是介绍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;

}

 

这篇关于20240206作业的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/688027

相关文章

作业提交过程之HDFSMapReduce

作业提交全过程详解 (1)作业提交 第1步:Client调用job.waitForCompletion方法,向整个集群提交MapReduce作业。 第2步:Client向RM申请一个作业id。 第3步:RM给Client返回该job资源的提交路径和作业id。 第4步:Client提交jar包、切片信息和配置文件到指定的资源提交路径。 第5步:Client提交完资源后,向RM申请运行MrAp

Java高级Day38-网络编程作业

112.网络编程作业 //1.使用字符流的方式,编写一个客户端程序和服务器端程序//2.客户端发送"name",服务器端接收到后,返回"我是nova"//3.客户端发送"hobby",服务器端接收到后,返回"编写java程序"//4.不是这两个问题,回复"你说啥呢"​​===============//客户端//===============public class SocketT

0906作业+思维导图梳理

一、作业: 1、创捷一个类似于qq登录的界面 1)源代码 #include "widget.h"#include "ui_widget.h"Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget){ui->setupUi(this);//QPushbutton:登录、退出this->join = new QP

2024.9.6 作业

1> 手写unique_ptr指针指针 #include <iostream>using namespace std;template <typename T>class my_unique_ptr{public:explicit my_unique_ptr(T *p = nullptr) noexcept // 构造函数{ptr = p;}~my_unique_ptr() noexcep

9月6号作业

1:.h文件 #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QWidget> #include<QIcon> //图标类 #include<QLabel> //标签类 #include<QMovie> //动图类 #include<QLineEdit> //行编辑器类

Flink实例(六十九): flink 作业提交(四)总结

独立集群提交 # 启动集群bin/start-cluster.sh# 提交job./bin/flink run ./examples/batch/WordCount.jar --input hdfs:/user/yuan/input/wc.count --output hdfs:/user/yuan/swwwttt yarn session # 启动集群./bin/

【#第三期实战营闯关作业 ## 茴香豆:企业级知识库问答工具】

今天学习了《 茴香豆:企业级知识库问答工具》这一课,对大模型的应用有了更深得认识。以下是记录本课实操过程及截图: 搭建茴香豆虚拟环境: 输入以下命令 ``studio-conda -o internlm-base -t huixiangdou 成功安装虚拟环境截图 安装茴香豆 cd /root 克隆代码仓库 git clone https://github.com/internlm/h

Quartz 作业调度器

1、Quartz  java实现  注:这里使用的是Quartz1.6.5版本(包:quartz-1.6.5.jar)   [java]  view plain copy //测试main函数   //QuartzTest.java   package quartzPackage;         import java.text.SimpleDateFormat

清华MEM作业-利用管理运筹学的分析工具slover求解最优解的实现 及 通过使用文件或者套节字来识别进程的fuser命令

一、清华MEM作业-利用管理运筹学的分析工具slover求解最优解的实现         最近又接触了一些线性求解的问题,以前主要都是在高中数学里接触到,都是使用笔算,最后通过一些函数式得出最小或者最大值,最近的研究生学业上接触到了一个Excel solver分析工具,对这种线性求最优解的问题感觉使用起来真是得心应手。在使用这个工具前,EXCEL里需要先装上solver工具,装起来很也简单,网上

opencv作业

作业下载地址: 链接:http://pan.baidu.com/s/1qYQnbkw 密码:v7y9