美文网首页
第二章 类与对象基础

第二章 类与对象基础

作者: DeepWeaver | 来源:发表于2017-10-07 21:09 被阅读51次

    学习内容

    • 掌握类的定义
    • 掌握对象的定义和方法调用
    • 熟悉构造函数和析构函数的定义和执行过程
    • 掌握复制构造函数和初始化列表的使用

    实习任务

    • 实习任务一

    注意全局对象的生命周期

    #include <iostream> 
    using namespace std; 
    class TestClass{ 
    public:
        TestClass(int a){ 
            aa=a;
            cout<<aa<<" Constructed!\n"; 
        }
        ~TestClass(){
            cout<<aa<<" Destructed!\n";
        } 
    private:
        int aa; 
    };
    TestClass AA(3); 
    int main(){
    //全局对象
        cout<<"In MainFuction."<<endl; 
        TestClass BB(5);
        return 0;
    }
    
    运行结果:
    3 Constructed!
    In MainFuction.
    5 Constructed!
    5 Destructed!
    3 Destructed!
    [Finished in 0.3s]
    
    #include <iostream> 
    using namespace std; 
    class TestClass{ 
    public:
        TestClass(){ 
            cout<<" Constructed!\n"; 
        }
        ~TestClass(){
            cout<<" Destructed!\n";
        } 
    };
    int main(){
        TestClass t1;
        TestClass *p; 
        p=new TestClass; 
        delete p;
        
        return 0;
    }
    
    运行结果:
     Constructed!
     Constructed!
     Destructed!
     Destructed!
    [Finished in 0.3s]
    
    #include <iostream> 
    using namespace std; 
    class TestClass{ 
    public:
        TestClass(){ 
            cout<<" Constructed!\n"; 
            value = 10;
        }
        ~TestClass(){
            cout<<" Destructed!\n";
        } 
        void setValue(int newValue){
            value = newValue;
        }
        int getValue()const{
            return value;
        }
    private:
        int  value;
    };
    int main(){
        TestClass t1; 
        cout<<t1.getValue()<<endl; 
        TestClass &rt1 = t1; 
        rt1.setValue(20); 
        cout<<t1.getValue()<<endl; 
        TestClass *pt=&t1; 
        pt->setValue(30); //a->b means (*a).b 
        // (*pt).setValue(30);//try uncomment this one
        cout<<t1.getValue()<<endl; 
        return 0;
    }
    
    运行结果:
     Constructed!
    10
    20
    30
     Destructed!
    [Finished in 0.3s]
    
    #include <iostream>
    #include <vector>
    using namespace std;
    class Test{
    public:
        Test():a(1){
            cout<<a<<endl;
        }
        Test(int a){
            cout<<this->a<<endl;
            this->a=a;
            cout<<this->a<<endl;
        }
    private:
        int a=3;
    };
    int main(){
        Test t1;
        Test t2(10);
        return 0;
    }
    
    运行结果:
    1
    3
    10
    [Finished in 0.4s]
    
    • 实习任务二

    要求:编写 Complex 类,封装复数的基本功能

    #include <iostream>
    #include <cmath>
    using namespace std;
    class Complex{
    public:
        Complex(){
            real = 0;
            image = 0;
        }
        Complex(double n){
            real = n;
            image = 0;
        }
        Complex(double n, double d){
            real = n;
            image = d;
        }
        void setValue(double n, double d){
            real = n;
            image = d;
        }
        double getReal(){
            return real;
        }
        double getImage(){
            return image;
        }
        double getDistance(){
            return sqrt(pow(real,2)-pow(image,2));
        }
        void output(){
            if(image == 0 && real != 0){
                cout<<real<<endl;
            }
            else if(image !=0 && real ==0){
                cout<<image<<'i'<<endl;
            }
            else if(image == 0 && real == 0){
                cout<<0<<endl;
            }
            else{
                if(image<0){
                    cout<<real<<image<<'i'<<endl;
                }
                else{
                    cout<<real<<'+'<<image<<'i'<<endl;
                }
            }
        }
    private:
        double real;
        double image;
    };
    int main(){
        Complex c1, c2(2), c3(3,4);
        c1.output();
        c2.output();
        c3.output();
        c1.setValue(6,4);
        c1.output();
        cout<<c1.getDistance()<<endl;
    
        return 0;
    }
    
    运行结果:
    0
    2
    3+4i
    6+4i
    4.47214
    [Finished in 0.3s]
    
    • 实习任务三

    要求:编写 Time 类,封装对时间的操作,Time 类处理 1 天内的时间,精确到分钟。

    #include <iostream>
    using namespace std;
    class Time{
    public:
        Time(){
            hour = 0;
            minute = 0;
        }
        Time(int h, int m){
            hour = h;
            minute = m;
        }
        void setTime(int h, int m){
            hour = h;
            minute = m;
        }
        void output(){
            this->normalizeTime();
            cout<<hour<<':'<<minute<<endl;
        }
        int getHour(){
            return hour;
        }
        int getMinute(){
            return minute;
        }
        int getTotalMinutes(){
            return hour*60 + minute;
        }
    private:
        int hour;
        int minute;
        void normalizeTime(){
            hour = ((60*hour + minute)/60)%24;
            minute = minute%60;
        }
    };
    int main(){
        Time t1(12, 75);
        t1.output();
        t1.setTime(8, 65);
        t1.output();
        cout<<"t1 Hour:"<<t1.getHour()<<endl;
        cout<<"t1 Minute:"<<t1.getMinute()<<endl;
        cout<<"t1 getTotalMinutes:"<<t1.getTotalMinutes()<<endl;
    
        return 0;
    }
    
    运行结果:
    13:15
    9:5
    t1 Hour:9
    t1 Minute:5
    t1 getTotalMinutes:545
    [Finished in 0.3s]
    
    • 实习任务四?

    要求:编写随机数类。请实现所要求的成员函数。

    #include <iostream>
    #include <ctime>
    using namespace std;
    class RandomNum{
    public:
        RandomNum(unsigned int seed){
            if(seed == 0){
                srand((unsigned int)time(NULL));
            }
            else{
                srand(seed);
            }
        }
        void setSeed(unsigned int seed){
            this->seed = seed;
        }
        int random(){
            return rand();
        }
        int random(int max){
            return rand()%max;
        }
        double frandom(){
            return rand()/(double)RAND_MAX;
        }
    private:
        unsigned int seed;
    };
    int main()
    {
        RandomNum randomGenerator(100);
        int i;
        for(int i=0; i<10; ++i){
            cout<<randomGenerator.frandom()<<' ';
        }
        cout<<endl;
    
        return 0;
    }
    
    运行结果:
    0.000782637 0.153779 0.560532 0.865013 0.276724 0.895919 0.704462 0.886472 0.929641 0.46929 
    [Finished in 0.3s]
    

    具体的有关c++随机数的内容可以看这里

    相关文章

      网友评论

          本文标题:第二章 类与对象基础

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