C++笔记

作者: 萌面大叔2 | 来源:发表于2017-02-15 13:07 被阅读0次

    约分

    #include<iostream>
    using namespace std;
    class Fract
    {
        int m_m;
        int m_z;
    public:
         Fract(int z,int m)//没有返回值 (构造函数)
        {
            m_m=m;
            m_z=z;
        }
        Fract()//没有返回值 (构造函数)
        {
            m_m=100;
            m_z=10;
            cout<<"Fract construct"<<endl;
        }
        void set(int z,int m)
        {
            m_m=m;
            m_z=z;
        }
        void print()
        {
            redu();
            cout<<m_z<<"/"<<m_m<<endl;
        }
        void redu()
        {
            for(int i=m_m;i>0;i--)
            {
                if((m_m%i==0)&&(m_z%i==0))
                {
                    m_m/=i;
                    m_z/=i;
                    break;
                }
            }
        }
    };
    
    int main()
    {
        Fract f(10,20);
    //  f.set(16,48);   
        f.print();
        Fract f1(30,5);
    //  f1.set(16,48);  
        f1.print();
    }
    运行结果:
    1/2
    6/1
    
    int main()
    {
        
        //Fract f(10,20);
    //  f.set(16,48);   
        //f.print();
        Fract f1[5];//构造类对象数组,只能调用无参数构造函数,调用了5次 
    
    }
    运行结果:
    Fract construct
    Fract construct
    Fract construct
    Fract construct
    Fract construct
    
    

    时钟

    #include<stdio.h>
    #include<iostream>
    #include <stdlib.h>
    //#include<unistd.h>
    #include<windows.h>
    using namespace std;
    
    class Time
    {
        int m_iHour;//小时 
        int m_iMinute;//分钟 
        int m_iSecond;//秒 
    public:
        void set(int h,int m,int s)
        {
            m_iHour=h;
            m_iMinute=m;
            m_iSecond=s;
        }
        
        void show()
        {
            cout<<m_iHour<<":";
            cout<<m_iMinute<<":";
            cout<<m_iSecond<<endl;
        }
        void tick()
    {
    
        Sleep(1000);
        m_iSecond++;
        if(m_iSecond==60)
        {
            m_iMinute++;
            m_iSecond=0;
            if(m_iMinute==60)
            {
                m_iHour++;
                m_iMinute=0;
                if(m_iHour==24)
                {
                    m_iHour=0;
                }
            }
        }
    }
        void run()//死循环 
        {
            while(1)
            {
                show();
                tick();
                system("Cls");//清屏 
            }
        }
    };
    int main(void)
    {
        Time t;
        t.set(23,59,50);
        t.run();
    
        
    }
    

    时钟2

    #include<iostream>
    #include<windows.h>
    using namespace std;
    class Clock{
        public:
            Clock(short h=0,short m=0,short s=0):h(h),m(m),s(s){
            }
            void displayTime();
        private:
            short h;
            short m;
            short s;
    };
    void Clock::displayTime()
        {
            while(true)
            {
                cout<<h<<':'<<m<<':'<<s<<"   ";
                Sleep(1000);
                cout<<'\r'; 
                if(!(s=++s%60))
                if(!(m=++m%60))
                    h=++h%24;
            }
    } 
    int main()
    {
        Clock A(23,59,55);
        A.displayTime();
        return 0;
    }
    
    
    

    析构函数

    #include<iostream>
    using namespace std;
    class A
    {
        int m_a;
        public:
            A(int a)
            {
                m_a=a;
                cout<<"A construct"<<m_a<<endl; 
            }
            ~A()//析构函数 (有且只有一个) 
            {
                cout<<"~~~~~A"<<m_a<<endl;
            }
    };
    
    class Test
    {
        A m_t1;
        A m_t2; 
        public:
            Test():m_t2(222),m_t1(111)//方法一:函数的初始化 (先输出m_t1,在输出m_t2)
            {
                cout<<"Test construct"<<endl;
            }
            ~Test()//析构函数 (有且只有一个) 
            {
                cout<<"~~~~~Test"<<endl;
            } 
    };
    int main()
    {
        Test t;//先调A再调Test函数 
    }
    
    输出结果:
    A construct111
    A construct222
    Test construct
    ~~~~~Test            //构造函数输出顺序正好相反
    ~~~~~A222
    ~~~~~A111
    
    
    

    分配空间和释放空间

    #include<iostream>
    #include<malloc.h>
    using namespace std;
    class Test
    {
        int *p;
        public:
            Test()
            {
                p=(int*)malloc(sizeof(int));
                *p=4;
                cout<<"Test construct"<<endl;
                
            }
            ~Test()//析构函数最后运行,释放空间 
            {
                free(p);
                p=NULL;
                cout<<"~~~~~Test"<<endl;
            }
            void print()
            {
                cout<<*p<<endl;
            }
    };
    int main()
    {
        Test t;
        t.print();
        cout<<"===="<<endl;
        return 0;
    }
    运行结果:
    Test construct
    4
    ====
    ~~~~~Test
    

    字符

    #include<iostream>
    using namespace std;
    int main()
    {
        string s;
        // string s("weiwei");
        //string s1(5,'a');
        //string s(s1);
        s="hello shangqian";//赋值 
        s+="sq1409";//追加、连接 
        s="hello";
        //s.clear;                     //清空
        cout<<"size="<<s.size()<<endl;//求长度 
        cout<<"s是否为空"<<(s.empty()?"空":"非空")<<endl;
        cout<<"s="<<s<<endl;
        //string 类型和char*类型转换 
        const char *buf=s.c_str();
        cout<<"buf="<<buf<<endl;
        //字符串比较,可以直接使用比较运算符//== != >< ...... 
        if(s=="hello")
        {
            cout<<"s=hello"<<endl;
            
        }
        //查看某个位置上的字符
        //at()越界访问会抛出异常
        //[]不会 
        cout<<"s[3]="<<s[3]<<endl;
        cout<<"s[3]="<<s.at(3)<<endl;//可以通过异常捕捉 
        return 0;
    }
    

    字符2

    #include <iostream>
    #include <string.h>
    using namespace std;
    class MyString
    {
        char* m_str;
        int m_nLength;
    public:
        int size(){return m_nLength;}
        bool empty(){return m_nLength == 0;}
        MyString()
        {
        m_str=new char[1];
        m_str[0]=0;
        m_nLength=0;
        } 
        ~MyString()
        {
        if( NULL != m_str)
        delete []m_str;
        m_str=NULL;
        }
        //MyString s(5,'c');
        MyString( unsigned int length, const char& ch )
        {
        m_nLength=length;
        m_str=new char[m_nLength+1];
        for (int i=0;i<m_nLength;++i)
        {
            m_str[i]=ch;
        }
        m_str[m_nLength]='\0';
        }
        //MyString s(NULL);
        MyString(const char* str)
        {
        if (NULL==str)
        {
            m_str=new char[1];
            m_str[0]=0;
            m_nLength=0;
        }
        else
        {
            m_str=new char[strlen(str)+1];
            strcpy(m_str,str);
            m_nLength=strlen(str);
        }
        }
        MyString( const MyString& s )
        {
        m_str= new char[strlen(s.m_str)+1];
        strcpy(m_str,s.m_str);
        m_nLength=strlen(s.m_str);
        }
    };
    int main()
    {
        MyString a("niho");
        cout<<a.size()<<endl;
    }
    
    //
    //The larger of 10, 20 is 0 ,为什么不是
    //20呢?问题在于输出操作符的优先级高于条件操作符
    //所以输出 val1和 val2比较结果的 true/false 。
    //
    
    

    点菜

    
    #include<iostream>
    using namespace std;
    class Waiter
    {
        string m_name;//姓名 
        public:
            Waiter(string name):m_name(name)
            {           
            }
            ~Waiter()
            {
            }
        string getName()
        {
            return m_name;
        }
        void Serve(int iCount,string szFood);
        
    };
    void Waiter::Serve(int iCount,string szFood)
    {
        cout<<m_name<<"上了"<<iCount<<"份"<<szFood<<endl;
        return ;
    }
    class Guest
    {
        string m_name;
        public:
            Guest(string name):m_name(name)
            {
                
            }
            ~Guest()
            {
                
            }   
            void order(Waiter &w);
            void pay(Waiter &w);
    };
    
    void Guest:: order(Waiter &w)
    {
        cout<<"**********点菜过程********"<<endl;   
        int iCount=0;
        string szFood;
        cout<<"请输入菜名";
        cin>>szFood;
        cout<<"请输入菜的份数";
        cin>>iCount;
        cout<<"**************************"<<endl;   
        cout<<m_name<<"向服务员"<<w.getName()<<"点了"<<iCount<<"份数"<<szFood<<endl;
        w.Serve(iCount,szFood);
        return ;
        
         
    }
    void Guest::pay(Waiter &w)
    {
        cout<<"**********付款过程********"<<endl;   
        double money=0;
        cout<<"请输入付款金额:";
        cin>>money;
        cout<<"**************************"<<endl;   
        cout<<m_name<<"吃完中饭,向服务员"<<w.getName()<<"支付了" <<money<<"元餐费,离开了老盛兴"<<endl;
        return ;     
    }
    int main()
    {
        Waiter w("小张");
        Guest g("小肥");
        g.order(w);
        g.pay(w);
        return 0;
    
    运行结果:
    
    **************点菜过程****************
    请输入菜名:宫保鸡丁
    请输入菜的份数:2
    ****************************************
    小肥向服务员小张点了两份宫保鸡丁
    小张上了2份宫保鸡丁
    **************付款过程****************
    请输入付款金额:45
    ****************************************
    小肥吃完午饭,向服务员小张支付了45元餐费,离开了老盛兴
    

    运行的先后顺序

    #include<iostream>
    using namespace std;
    class Test
    {
        int m_data;
        public:
            Test(int data):m_data(data)
            {
                cout<<"test("<<m_data<<")"<<endl;
            }
            ~Test()
            {
                cout<<"test("<<m_data<<")"<<endl;
            }       
    };
    Test t1(10);
    void fun()
    {
        Test t4(40);
        return ;
    }
    void show()
    {
        static Test t5(50);
        return ;
    }
    int main()
    {
        Test t2(20);
        Test t3(30);
        fun();
        show(); 
    }
    Test t6(60);
    运行结果:
    test(10)
    test(60)
    test(20)
    test(30)
    test(40)
    test(40)
    test(50)
    test(30)
    test(20)
    test(50)
    test(60)
    test(10)
    

    true or false(甩锅还是美女)

    #include<iostream>
    using namespace std;
    int main()
    {
        bool bSet=true;
        cout<<"请输入你的性别:"<<endl;
        cin>>bSet;
        cout<<"我叫Jack,是一位"<<(bSet?"甩锅":"美女")<<endl;
        cout<<"ture="<<true<<endl;
        cout<<"false"<<false<<endl;
        return 0;
    }
    
    

    相关文章

      网友评论

          本文标题:C++笔记

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