C++语言 通过构造函数初始化学生信息

这篇博客介绍了如何在C++中通过构造函数初始化学生信息,包括使用构造函数、内联函数、友元函数以及如何处理静态成员变量的初始化。示例代码展示了不同方法的实现方式,并在main函数中进行了测试。
//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;
}

http://www.pythonschool.com/python/96.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

刘华世(Moments)

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值