首页 > 编程语言 > 详细

C++类成员的访问权限

时间:2020-05-10 14:22:16      阅读:35      评论:0      收藏:0      [点我收藏+]
/*
时间:2020年5月10日10:57:26
地点:大石板
功能:C++类成员的访问权限以及类的封装*/
#include<iostream>
using namespace std;
class Student
{
private://私有数据成员
    const char* m_name;
    int m_age;
    long long int m_number;
    float m_score;
public:
    void setname(const char *name);
    void setage(int age);
    void setnumber(long long int number);
    void setscore(float score);
    void print();
};
//成员函数定义
void Student:: setname(const char *name)
{
    m_name = name;
}
void Student::setage(int age)
{
    m_age = age;
}
void Student::setnumber(long long int number)
{
    m_number = number;
}
void Student::setscore(float score)
{
    m_score = score;
}
void Student::print()
{
    cout << m_name << endl;
    cout <<m_age << endl;
    cout << m_number << endl;
    cout << m_score << endl;
}
int main()
{
    //在栈上创建对象(系统自定分配)
    Student stu;
    stu.setname("小明");
    stu.setage(15);
    stu.setnumber(6019203034);
    stu.setscore(88.5);
    stu.print();
    //在堆上创建对象(由程序员分配)
    Student* sTu = new Student;
    sTu->setname("小圆");
    sTu->setage(15);
    sTu->setnumber(6019203034);
    sTu->setscore(89);
    sTu->print();
    return 0;
}

注意const

C++类成员的访问权限

原文:https://www.cnblogs.com/qq1480040000/p/12862811.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!