美文网首页
C艹之路 1.3c--

C艹之路 1.3c--

作者: 农家小升 | 来源:发表于2020-01-08 20:32 被阅读0次

    3.5

    例子3.1p70

    //;使用构造函数修改例子2.3
    #include<iostream>
    using namespace std;
    class Time
    {
    public:
        Time() {
            hour = 0;
            minute = 0;
            sec = 0;
        }
        void setTime();
        void showTime();
    private:
        int hour;
        int minute;
        int sec;
    };
    void Time::setTime()
    {
        cin >> hour >> minute >> sec;
    }
    void Time::showTime()
    {
        cout << hour << ":" << minute << ":" << sec << endl;
    }
    int main()
    {
        Time t1;
        t1.setTime();
        t1.showTime();
        Time t2;
        t2.showTime();
        return 0;
    }
    

    例子3.2p72

    //两个长方体,已知各自参数,求体积
    #include<iostream>
    using namespace std;
    class Box
    {
    public:
        Box(int, int, int);
        int volume();
    private:
        int height;
        int width;
        int length;
    };
    Box::Box(int h, int w, int len)
    {
        height = h;
        width = w;
        length = len;
    
    }
    int Box::volume()
    {
        return(height*width*length);
    }
    int main() {
        Box box(12, 25, 30);
        cout << "The volume of box is " << box.volume() << endl;
        Box box2(15, 30, 21);
        cout << "The volume of box2 is " << box2.volume() << endl;
        return 0;
    }
    

    例子3.3p74

    //;在例子3.2基础上,构建两个一个无参数,一个有参数
    #include<iostream>
    using namespace std;
    class Box
    {
    public:
        Box();
        Box(int h, int w, int len) :height(h), width(w), length(len) {}
        int volume();
    private:
        int height;
        int width;
        int length;
    };
    Box::Box()
    {
        height = 10;
        width = 10;
        length = 10;
    }
    int Box::volume()
    {
        return(height*width*length);
    
    }
    int main()
    {
        Box box1;
        cout << "The volume of box1 is " << box1.volume() << endl;
        Box box2(15,30,25);
        cout << "The volume of box2 is " << box2.volume() << endl;
        return 0;
    }
    

    例子3.4p

    //在例子3.3基础上,构造函数改用含默认参数的,且全为10
    #include<iostream>
    using namespace std;
    class Box
    {
    public:
        Box(int h = 10, int w = 10, int len = 10);
        int volume();
    private:
        int height;
        int width;
        int length;
    };
    Box::Box(int h, int w, int len)
    {
        height = h;
        width = w;
        length = len;
    }
    int Box::volume()
    {
        return (height*width*length);
    }
    int main()
    {
        Box box1;
        cout << "The volume of box1 is " << box1.volume() << endl;
        Box box2(15);
        cout << "The volume of box2 is " << box2.volume() << endl;
        Box box3(15, 30);
        cout << "The volume of box3 is " << box3.volume() << endl;
        Box box4(15, 30, 20);
        cout << "The volume of box4 is " << box4.volume() << endl;
    
        return 0;
    }
    

    例子3.5p79

    //;包含析构函数和构造函数//有问题
    #include<string>
    #include<iostream>
    using namespace std;
    class Student
    {
    public:
        Student(int n, string nam, char s)
        {
            num = n;
            name = nam;
            sex = s;
            cout << "Constructor called" << endl;
        }
        ~Student()
        {
            cout << "Destructor called" << endl;
        }
        void display()
        {
            cout << "num:" << num << endl;
            cout << "name:" << name << endl;
            cout << "sex:" << sex << endl << endl;
        }
    private:
        int num;
        string name[20];
        char sex;
    };
    int main()
    {
        Student stu1(10010, "Wang_li", 'f');
        stu1.display();
        Student stu2(10011, "Zhang_fan", 'm');
        stu2.display();
        return 0;
    }
    

    例子3.6p

    //对象数组:计算和输出3个立方体的体积//书上的错误的修改过来了
    #include<iostream>
    using namespace std;
    class Box
    {
    public:
        Box(int h = 10, int w = 12, int len = 15) :height(h), width(w), length(len) {}
        int volume();
    private :
        int height;
        int width;
        int length;
    };
    int Box::volume()
    {
        return (height*width*length);
    }
    int main()
    {
        Box a[3] = {
            Box(10,12,15),
            Box(15,18,20),
            Box(16,20,26)
            //return 0;
        };
        cout << "volume of a[0] is " << a[0].volume() << endl;
        cout << "volume of a[1] is " << a[1].volume() << endl;
        cout << "volume of a[2] is " << a[2].volume() << endl;
    
    }
    

    例子3.7p87

    这里很重要,要细心理解

    //适用对象指针输出时分秒//这里很重要,要细心理解
    #include<iostream>
    using namespace std;
    class Time
    {
    public:
        Time(int, int, int);
        int hour;
        int minute;
        int sec;
        void getTime();
    private:
    
    };
    Time::Time(int h, int m, int s)
    {
        hour = h;
        minute = m;
        sec = s;
    }
    void Time::getTime() {
        cout << hour << ":" << minute << ":" << sec << endl;
    }
    int main()
    {
        Time t1(10, 13, 56);
        int *p1 = &t1.hour;//为何这里有&
        cout << *p1 << endl;
    
        t1.getTime();
        Time *p2 = &t1;
        p2->getTime();
    
        void(Time:: *p3)();
        p3 = &Time::getTime;//为什么加了()之后就报错呢
        (t1.*p3)();
    
        return 0;
    
    }
    

    例子3.8p97

    //使用对象的引用,输出时分秒//可以通过类外函数(参数必须为引用,否则无效),修改相关参数
    #include<iostream>
    using namespace std;
    class Time {
    public:
        Time(int, int, int);
        int hour;
        int minute;
        int sec;
    };
    Time::Time(int h, int m, int s)
    {
        hour = h;
        minute = m;
        sec = s;
    }
    void fun(Time &t)//&必须要有,否则无效
    {
        t.hour = 18;
        //t.minute = 30;
    }
    int main()
    {
        Time t1(10, 13, 56);//此时里面参数10,已经被该对象的引用给覆盖了
        fun(t1);
        cout << t1.hour<<":"<<t1.minute<<":"<<t1.sec << endl;
        return 0;
    }
    

    例子3.9p100

    //对象赋值给另外个对象(必须同个类才行)
    #include<iostream>
    using namespace std;
    class Box
    {
    public:
        Box(int = 10, int = 10, int = 10);//这里居然不用写出变量名
        int volume();
    private:
        int height;
        int width;
        int length;
    };
    Box::Box(int h, int w, int len)
    {
        height = h;
        width = w;
        length = len;
    }
    int Box::volume()
    {
        return (height*width*length);
    }
    int main()
    {
        Box box1(15, 30, 25), box2;//即使没有括号()里面,也能运行
        cout << "The volume of box1 is " << box1.volume() << endl;
        box2 = box1;
        cout << "The volume of box2 is " << box2.volume() << endl;
    }
    

    例子3.10p105

    //使用静态数据成员输出立方体的体积
    #include<iostream>
    using namespace std;
    class Box
    {
    public:
        Box(int, int);
        int volume();
        static int height;
        int width;
        int length;
    };
    Box::Box(int w, int len)
    {
        width = w;
        length = len;
    }
    int Box::volume()
    {
        return (height*width*length);
    }
    int Box::height = 10;
    int main()
    {
        Box box1(15, 20), box2(20, 30);
        cout << box1.height << endl;
        cout << box2.height << endl;
        cout << Box::height << endl;
        cout << box1.volume()<< endl;
        return 0;
    }
    

    例子3.11p107

    //使用静态成员函数统计学生平均成绩//没有通过编译
    #include<iostream>
    using namespace std;
    class Student
    {
    public:
        Student(int n,int a,float s):num(n),age(a),score(s){}
        void total();
        static float average();
    private:
        int num;
        int age;
        float score;
        static float sum;
        static int count;
    };
    void Student::total()
    {
        sum += score;
        count++;
    }
    float Student::average()
    {
        return(sum / count);
    }
    int main()
    {
        Student stu[3] =
        {
            Student(1001,18,70),
            Student(1002,18,78),
            Student(1005,20,98)
        };//这里必须添加;因为这是数组
        int n;
        cout << "please input the number of students: ";
        cin >> n;
        for (int i = 0; i < n; i++)
            stu[i].total();
        cout<< "the average score of " << n << "students is " << Student::average() << endl;
        return 0;
    }
    

    例子3.12p10

    
    

    相关文章

      网友评论

          本文标题:C艹之路 1.3c--

          本文链接:https://www.haomeiwen.com/subject/tkteactx.html