码迷,mamicode.com
首页 > 编程语言 > 详细

C++类成员的访问权限

时间:2020-05-10 12:48:07      阅读:71      评论:0      收藏:0      [点我收藏+]

标签:程序员   小明   函数定义   class   访问权限   return   int   成员   new   

/*
时间: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++类成员的访问权限

标签:程序员   小明   函数定义   class   访问权限   return   int   成员   new   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!