• C++第三章习题


    3.1 类声明的一般格式是什么?

    class 类名
    {
        [private:]
            私有数据成员和成员函数
        public:
            公有数据成员和成员函数
    }

    3.2 构造函数和析构函数的主要作用是什么?它们各自有什么特性?

             构造函数是一种特殊的成员函数,它主要用于为对象分配空间,进行初始化。

    构造函数的名字必须与类名相同,而不能由用户任意命名。它可以有任意类型的参数,但不能具有返回值类型。

    析构函数通常用于执行一些清理任务,如释放分配给对象的内存空间等。

    析构函数名与类名相同,但它前面必须加一个波浪号。不能有返回值,也不能有参数。

    3.3 什么是对象数组?

             所谓对象数组,是指每一个数组元素都是对象的数组。

    3.4 什么是this指针?它的主要作用是什么?

             C++为成员函数提供了一个名为this的指针,这个指针称为自引用指针。每当创建一个对象时,系统就把this指针初始化为指向该对象。

             一个类的所有对象合用一份成员函数,this指针可以帮助对象辨别出当前调用的是自己的那个对象的数据成员和函数。

    3.5 友元函数有什么作用?

             友元函数可以在类的外部访问类的私有成员或保护成员。

    3.6

    (1)声明并定义了P2, P3, 并用默认无参构造函数初始化。

    (2)声明并定义了P2, 并调用Point类的拷贝构造函数用P1对P2进行初始化。

    (3)声明并定义了P2, 并调用Point类的拷贝构造函数用P1对P2进行初始化。

    (4)调用拷贝构造函数,将P1的成员值赋值给P4的成员。

    3.7-3.10 BCCB

    3.11-3.15 BAABA

    3.16-3.17 BB

    3.18

    10,20

    30,48

    50,68

    70,80

    90,16

    11,120

    3.19

             Constructing

             10

             100

             Destructing

    3.20

             3objects in existence

             4objects in existence after allocation

             3objects in existence after deletion

    3.21

             Counting at0

             Counting at9

    3.22

             Default constructor called.

             Default constructor called.

             Default constructor called.

             Construcotor:a=1,b=2

             Construcotor:a=3,b=4

             Construcotor:a=5,b=6

    3.23

             Con.

             Copy con.

             default.

             Copy con.

    3.24

             A=5

             B=14

             A=9

             B=14

    3.25

             5,7

             22.25

    3.26

             Constructing

             Constructing

             A=5

             B=15

             A=10

             B=15

             Destructing

             Destructing

    3.27

             void pintStu();函数只有声明,没有定义。

             age是私有成员,不能用对象直接调用。

    3.28

             void printStu() 和 void setSno(int s) 没有加限定符 Student::

             void setAge(int a)在类中没有声明

    3.29

             构造函数不能定义为私有。否则无法创建对象。

    3.30 下面是一个计算器类的定义,请完成该类成员函数的实现。

    class counter
    {
    public:
        counter(int number); 
        void increment();             //给原始值加1
        void decrement();             //给原始值减1
        int getvalue();               //取的计数器值
        int print();                  //显示计数
    private:
        int value;
    };
    
    counter::counter(int number)
    {
        value = number;
    }
    void counter::increment()
    {
        ++value;
    }
    void counter::decrement()
    {
        --value;
    }
    int counter::getvalue()
    {
        return value;
    }
    int counter::print()
    {
        cout << value <<endl;
        return value;
    }

    3.31 根据注释语句提示,实现类Date的成员函数

    #include <iostream>
    using namespace std;
    
    class Date
    {
    public:
        void printDate();
        void setDay(int d);
        void setMonth(int m);
        void setYear(int y);
    private:
        int day, month, year;
    };
    void Date::printDate()
    {
        cout << "今天是" << year << "" << month << "" << day << "" << endl;
    }
    void Date::setDay(int d)
    {
        day = d;
    }
    void Date::setMonth(int m)
    {
        month = m;
    }
    void Date::setYear(int y)
    {
        year = y;
    }
    int main()
    {
        Date testDay;
        testDay.setDay(5);
        testDay.setMonth(10);
        testDay.setYear(2003);
        testDay.printDate();
        return 0;
    }

    3.32 建立类cylinder, cylinder的构造函数被传递了两个double值,分别表示圆柱体的半径和高度。用类cylinder计算圆柱体的体积,并存储在一个double变量中。在类cylinder中包含一个成员函数vol,用来显示每个cylinder对象的体积。

    const int PI = 3.14;
    
    class cylinder
    {
    private:
        double radius, height, volume;
    public:
        cylinder(int rad, int hei);
        double getVolume();
        void vol();
    };
    
    cylinder::cylinder(int rad, int hei)
    {
        radius = rad;
        height = hei;
    }
    
    double cylinder::getVolume()
    {
        volume = PI * radius * radius *height;
        return volume;
    }
    
    void cylinder::vol()
    {
        cout << "圆柱体的体积是: " << volume <<endl;
    }

    3.33 构建一个类book其中包含有两个私有数据成员quprice,将qu初始化为1~5,将price初始化为qu10倍,建立一个有5个元素的数组对象。显示每个对象数组元素的qu*price值。

    class book
    {
    private:
        int qu, price;
    public:
        book(int qu);
        int mult();
    };
    
    book::book(int q)
    {
        if(q < 1 || q > 5)
        {
            qu = 1;
        }
        else
        {
            qu = q;
        }
        price = 10 * qu;
    }
    
    int book::mult()
    {
        return qu * price;
    }
    
    int main()
    {
        book books[5] = {1,2,3,4,5};
        for(int i = 0; i < 5; i++)
        {
            cout << books[i].mult() << " ";
        }
    }

    3.34 修改3.33,通过对象指针访问对象数组,使程序以相反的顺序显示每个对象数组元素的qu*price值。

    class book
    {
    private:
        int qu, price;
    public:
        book(int qu);
        int mult();
    };
    
    book::book(int q)
    {
        if(q < 1 || q > 5)
        {
            qu = 1;
        }
        else
        {
            qu = q;
        }
        price = 10 * qu;
    }
    
    int book::mult()
    {
        return qu * price;
    }
    
    int main()
    {
        book books[5] = {1,2,3,4,5};
        book *p = books;
        p += 4;
        for(int i = 0; i < 5; i++)
        {
            cout << p->mult() << " ";
            --p;
        }
        return 0;
    }

    3.35 构建一个类Stock,含字符数组stockcode[]及整型数组成员quan、双精度型数据成员price。构造函数含3个参数:字符数组na[]qp。当定义Stock的类对象时,将对象的第一个字符串参数赋给数据成员stockcode,第2和第3个参数分别赋给quanprice。未设置第2和第3个参数时,quan的值为1000price的值为8.98.成员函数print没有形参,需使用this指针,显示对象数据成员的内容。编写程序显示对象数据成员的值。

    #include <iostream>
    using namespace std;
    
    class Stock
    {
    private:
        char stockcode[25];
        int quan;
        double price;
    public:
        Stock(char na[], int q = 1000, double p = 8.98);
        Stock(char na[]);
        void print();
    };
    
    Stock::Stock(char na[], int q = 1000, double p = 8.98)
    {
        strcpy(stockcode, na);
        quan = q;
        price = p;
    }
    
    void Stock::print()
    {
        cout << "stockcode: " << this->stockcode << " quan: " << this->quan << " price: " << this->price << endl;
    }
    
    int main()
    {
        Stock stock1("600001", 3000, 5.67);
        Stock stock2("600002");
        stock1.print();
        stock2.print();
        return 0;
    }

    3.36 编写一个程序,已有若干学生的数据,包括学号、姓名、成绩,要求输出这些学生的数据并计算出学生人数和平均成绩(要求将学生人数和总成绩用静态数据成员表示)。

    #include <iostream>
    using namespace std;
    
    class student
    {
    private:
        char name[25], studentNo[10];
        int score;
        static int sum;
        static int totalScore;
    public:
        student(char na[], char stuNo[], int sc);
        void show();
        static void showTotal();
    };
    
    student::student(char na[], char stuNo[], int sc)
    {
        strcpy(name, na);
        strcpy(studentNo, stuNo);
        score = sc;
        ++sum;
        totalScore += sc;
    }
    
    void student::show()
    {
        cout << "姓名: " << name <<endl;
        cout << "学号: " << studentNo << endl;
        cout << "成绩: " << score << endl;
    }
    
    void student::showTotal()
    {
        cout << "总人数: " << sum << endl;
        cout << "平均成绩: " << (double)totalScore/sum <<endl;
    }
    
    int student::sum = 0;
    int student::totalScore = 0;
    
    int main()
    {
        student s1("张无忌", "111254", 75);
        student s2("李莫愁", "254114", 60);
        student s3("小龙女", "112587", 88);
        s1.show();
        s2.show();
        s3.show();
        student::showTotal();
    return 0;
    }
  • 相关阅读:
    关于Vuex的actions传入多个参数的方法:
    2019最新create-react-app创建的react中使用sass/scss,以及在react中使用sass/scss公共变量的方法
    vue中怎么使用vuex
    分享一个知乎答案 最详细易懂的 js闭包
    web前端 在react中使用移动端事件,学习笔记
    闭包
    原生ajax练习-post&xml
    Ajax中Get请求与Post请求的区别
    css文件编码
    template.js模板工具案例
  • 原文地址:https://www.cnblogs.com/tangzhengyue/p/2548980.html
Copyright © 2020-2023  润新知