zoukankan      html  css  js  c++  java
  • C++语言 通过构造函数初始化学生信息

    //C++语言 通过构造函数初始化学生信息
    
    #include "stdafx.h"
    #include <iostream>
    #include <string>
    
    using namespace std;
    /*
    class CStudent
    {
    private:
        char m_Name[10];
        int m_Age;
        double m_Height;
    public:
        CStudent(char *name, int age, double height)
        {
            strcpy(m_Name, name);
            m_Age = age;
            m_Height = height;
        }
        void display()
        {
            cout << "name:" << m_Name << endl;
            cout << "age:" << m_Age << endl;
            cout << "height:"<< m_Height << endl;
        }
    };
    */
    
    /*
    //内联函数
    class CStudent
    {
    private:
        char m_Name[10];
        int m_Age;
        double m_Height;
    public:
        CStudent(char *name, int age, double height);
        inline void display();
    };
    
    CStudent::CStudent(char *name, int age, double height)
    {
        strcpy(m_Name, name);
        m_Age = age;
        m_Height = height;
    }
    
    inline void CStudent::display()
    {
        cout << "name:" << m_Name << endl;
        cout << "age:" << m_Age << endl;
        cout << "height:"<< m_Height << endl;
    }
    
    int main(int argc, int argv[])
    {
        CStudent student("格格", 18, 160);
        student.display(); //内联函数
    
        return 0;
    }
    */
    
    /*
    //友元函数
    class CStudent
    {
    private:
        char m_Name[10];
        int m_Age;
        double m_Height;
    public:
        CStudent(char *name, int age, double height);
        friend void display(CStudent &stu);
    };
    
    CStudent::CStudent(char *name, int age, double height)
    {
        strcpy(m_Name, name);
        m_Age = age;
        m_Height = height;
    }
    
    void display(CStudent &stu)
    {
        cout << "name:" << stu.m_Name << endl;
        cout << "age:" << stu.m_Age << endl;
        cout << "height:"<< stu.m_Height << endl;
    }
    int main(int argc, int argv[])
    {
        CStudent student("格格", 18, 160);
        display(student); //友元函数
    
        return 0;
    }
    */
    
    class CStudent
    {
    private:
        char m_Name[10];
        static int m_Age; //声明静态成员变量
        static double m_Height; //声明静态成员变量
    public:
        CStudent(char *name, int age, double height);
        static void SetStu(int age, int height); //声明静态成员函数
        static void display(); //声明静态成员函数
    };
    
    CStudent::CStudent(char *name, int age, double height)
    {
        strcpy(m_Name, name);
        m_Age = age;
        m_Height = height;
    }
    void CStudent::SetStu(int age, int height)
    {
        m_Age = age;
        m_Height = height;
    }
    void CStudent::display()
    {
        //cout << "name:" << m_Name << endl; //静态成员函数不可以访问普通成员变量
        cout << "age:" << m_Age << endl;
        cout << "height:"<< m_Height << endl;
    }
    
    //初始化静态成员变量 ##不能用参数初始化表,对静态成员变量进行初始化
    int CStudent::m_Age = 0;
    double CStudent::m_Height = 0;
    
    int main(int argc, int argv[])
    {
        CStudent::display(); //显示初始化的值
        CStudent student("格格", 18, 160); 
        student.display(); 
        CStudent::SetStu(24, 170);
        student.display();
    
        return 0;
    }
  • 相关阅读:
    jpa @onetomany 级联查询时会有重复数据,去重问题
    jpa/hibernate @onetomany 使用left join 添加多条件,可以使用过滤器filters (with-clause not allowed on fetched associations; use filters异常信息)
    如何使用多数据源,同时使用jpa和jdbctemplate
    mysql中使用enum,如何获取所有可能的值
    jpa返回List<Map<String, Object>>相当于jdbctemplate的queryForlist
    git bash各种乱码问题,已解决
    Kafka学习整理五(Consumer配置)
    创建Kafka0.8.2生产者与消费者
    Kafka消费组(consumer group)
    Kafka学习之四 Kafka常用命令
  • 原文地址:https://www.cnblogs.com/pythonschool/p/2762846.html
Copyright © 2011-2022 走看看