zoukankan      html  css  js  c++  java
  • C++知识点案例 笔记-2

    1.友元函数
    2.友元类
    3.继承(公有继承)
    4.公有继承的访问权限
    5.私有继承的访问权限
    6.保护继承的访问权限(两次继承)

    ==友元函数==
    
    #include <iostream>
    using namespace std;
    class love
    {
    public:
        love(int you,int me);
        friend int baby(love& );//################
    private:
        int Me;
        int You;
    };
    love::love(int you,int me)
    {
        You = you;
        Me = me;
    }
    int baby(love& refnum)
    {
        return refnum.You + refnum.Me ;//###########
    }
    int main()
    {
        love aini(500,21);
        cout<<"我想对你说:"<<baby(aini)<<endl;
        return 0;
    }
    
    ==友元类==
    #include <iostream>
    using namespace std;
    class Date;                                              //声明Date类
    class Time                                               //定义Time类,描述时分秒
    {
    public:
        Time(int con_hour, int con_minute, int con_second) //定义构造函数
        {
            m_nHour = con_hour;
            m_nMinute = con_minute;
            m_nSecond = con_second;
    
        }
        friend class Date;                                 //声明Date为Time的友元类
    private:
        int m_nHour, m_nMinute, m_nSecond;
    };
    class Date                                               //定义Date类
    {
    public:
        Date(int con_year, int con_month, int con_day)//定义构造函数
        {
            m_nYear = con_year;
            m_nMonth = con_month;
            m_nDay = con_day;
        }
        void display_date_time(Time &ref_time);        //声明display_date_time()函数
    private:
        int m_nYear, m_nMonth, m_nDay;
    };
    /*
    * 定义display_date_time()函数,显示年月日、时分秒信息。由于Date为Time类的友元类,
    * 则Date中的函数为Time类的友元函数,可以访问Time类的私有成员,
    * 这里直接访问了私有成员:m_nHour、m_nMinute、m_nSecond
    */
    void Date::display_date_time(Time &ref_time)
    {
        cout << m_nYear << "-" << m_nMonth << "-" << m_nDay << " "
            << ref_time.m_nHour << ":" << ref_time.m_nMinute//################
            << ":" << ref_time.m_nSecond << endl;                          //###############
    }
    int main()
    {
        Time time(17, 30, 20);                          //定义Time对象
        Date date(2017, 2, 23);                         //定义Date对象
        date.display_date_time(time);                  //显示年月日、时分秒信息
        return 0;
    }
    
    
    
    
    ==继承(公有继承)==
    #include <iostream>
    #include <string>
    using namespace std;
    class Animal
    {
    public:
        void speak()
        {
            cout<<"animal language."<<endl;
    
        }
    };
    class Dog :public Animal    //继承Animal的属性
    {
    public:
        Dog (string con_name):m_Name(con_name){}
        void print_name()
        {
            cout<<"con_name:"<<m_Name<<endl;
        }
    private:
        string m_Name;
    
    };
    int main()
    {
        Dog dog("dogoo");
        dog.speak();              //调用其继承的Animal属性里的speak函数
        dog.print_name();
        return 0;
    }
    
    
    ==公有继承的访问权限==
    #include <iostream>
    #include <string>
    using namespace std;
    class Animal
    {
    public:
        void set_weight(int weight){m_Weight = weight;}
        int get_weight(){return m_Weight;}
        void set_age(int age){m_Age = age;}
    protected:                                                                            
        int m_Age;    //protected成员可以被派生类访问
    private:
        int m_Weight;     //私有成员被派生类不可访问
    };
    class Dog:public Animal
    {
    public:
        Dog (string con_name):m_Name(con_name){}
        void print_age(){ cout<<m_Name<<",age="<<m_Age<<endl; }    //这里直接访问了//#################
    private:
        string m_Name;
    };
    int main()
    {
        Dog dog("dogoo");
        dog.set_weight(6);
        dog.set_age(8);
        dog.print_age();
        cout<<"weight ="<<dog.get_weight()<<endl;          //这里通过继承基类成员函数输出weight值,不可直接访问
        return 0;
    
    }
    
    
    ==私有继承的访问权限==
    //把从基类继承的protected、public成员当做私有成员,主函数不能直接访问基类的成员
    #include <iostream>
    #include <string>
    using namespace std;
    class Animal
    {
    public:
        void set_weight(int weight){m_Weight = weight;}
        int get_weight(){return m_Weight;}
        void set_age(int age){ m_Age = age; }
    protected:
        int m_Age;
    private:
        int m_Weight;
    };
    class Dog:private Animal
    {
    public:
        Dog(string con_name):m_Name(con_name){}
        void set_print_weight()
        {
            set_weight(6);                               //################
            cout<<m_Name<<",weight = "<<get_weight()<<endl;
        }
        void set_print_age()
        {
            set_age(8);                                    //#################
            cout<<m_Name<<",age = "<<m_Age<<endl;
        }
    private:
        string m_Name;
    };
    int main()
    {
        Dog dog("dogoo");
        dog.set_print_weight();    //通过派生类的访问来访问基类成员
        dog.set_print_age();
        return 0;
    }
    
    
    
    ==保护继承的访问权限(多重继承:类Animal——>次类Dog——>再次类Dog_plus)==
    //把从基类继承的protected、public成员当做保护成员
    #include <iostream>
    #include <stdlib.h>
    #include <string>
    using namespace std;
    class Animal
    {
    public:
        void set_weight(int weight){m_Weight = weight;}
        int get_weight(){return m_Weight;}
        void set_age(int age){m_Age = age;}
    protected:
        int m_Age;
    private:
        int m_Weight;
    };
    class Dog:protected Animal                                         //#################
    {
    
    public:
        Dog(string con_name):m_Name(con_name){}
        void set_print_weight()
        {
            set_weight(8);
            cout<<m_Name<<",weight ="<<get_weight()<<endl;
        }
    private:
        string m_Name;
    };
    class plus_Dog:protected Dog                                     //###################
    {
    public:
        plus_Dog():Dog("dogoo"){ }
        void plus_set_print_age()
        {
            set_age(6);
            cout<<"The plus_dog age = "<<m_Age<<endl;
        }
    
    };
    int main()
    {
        plus_Dog plus_dog;
        plus_dog.plus_set_print_age();
        system("pause");
        return 0;
    }
    
    

    ________________________________________________________

    Every good deed you do will someday come back to you.

    Love you,love word !
  • 相关阅读:
    0508---字符串练习题
    0506--习题
    0503---练习题 punctuation isdigit() strip() upper()
    0505---练习题
    0504---习题str.swapcase() str. capitalize() str.title()
    0429---每日习题 菲薄纳西数列 正则ip匹配
    习题之---文件操作
    NOIP 模拟 $13; ext{工业题}$
    NOIP 模拟 $12; ext{简单的填数}$
    NOIP 模拟 $12; ext{简单的玄学}$
  • 原文地址:https://www.cnblogs.com/hugboy/p/12738149.html
Copyright © 2011-2022 走看看