Define a Student class that contains a data member with a name, and use the get and set functions to encapsulate the data member with the name. In the main function, the object is instantiated by new and its related functions are printed.

1. Subject requirements:

Define a Student class that contains a data member with a name, and use the get and set functions to encapsulate the data member with the name. In the main function, the object is instantiated by new and its related functions are printed.

2. Come on, show:

#include <iostream>
#include <string>
using namespace std;

/**
  * 定义类:Student
  * 数据成员:m_strName
  * 数据成员的封装函数:setName()、getName()
  */
class Student
{

public:
    void setName(string str)
    {
        m_strName=str;
    }
    
    string getName()
    {
        return m_strName;
    }
    // 定义数据成员封装函数setName()
    
    
    
    // 定义数据成员封装函数getName()
    
    private:
    string m_strName;
    
//定义Student类私有数据成员m_strName


};

int main()
{
    // 使用new关键字,实例化对象
	Student *str = new Student;
    // 设置对象的数据成员
	str->setName("慕课网");
    // 使用cout打印对象str的数据成员
    cout<<str->getName()<<endl;
    // 将对象str的内存释放,并将其置空
	delete str;
	str=NULL;
	return 0;
}

3. Output result:

 

 4. This is considered successful

I hope I can help everyone, I ask you if you want a like, will you give it, thank you all.
Copyright statement: The copyright of this article belongs to the author (@攻城狮小关) and CSDN. Welcome to reprint, but you must keep this paragraph without the author’s consent Statement, and provide the original link in an obvious place on the article page, otherwise the right to pursue legal responsibility is reserved.
It is not easy for everyone to write articles, please respect the fruits of labor~ 
Communication plus Q: 1909561302
Blog Park Address https://www.cnblogs.com/guanguan-com/

Guess you like

Origin blog.csdn.net/Mumaren6/article/details/108544971