有些疑问求大神解读

#include <iostream>

#include <string>

using namespace std;


/**

  * 定义类:Student

  * 数据成员:名字、年龄

  */

  class student

{

public:

    // 定义数据成员名字 m_strName 和年龄 m_iAge

    string m_strName;

            m_iAge;

};


int main()

{

    // 实例化一个Student对象stu

    student.stu;

    // 设置对象的数据成员

    stu.m_strName = "慕课网";

    stu.m_iAge = 2;

    

    // 通过cout打印stu对象的数据成员

    cout << stu.m_strName<< " " << stu.m_Age<< endl;

    return 0;

}

  

我没用int  为什么也行?

已天涯
浏览 1011回答 1
1回答

天将明96

你确定可以?同学,学习的时候带着疑惑是好的,可是你这样提出的根本就不是有意义的问题。C++中声明变量时也必须声明它的数据类型,没有 int,m_iAge就是错误的。而且实例化Student对象不是student.stu;而是Student stu;先把代码基础学好,然后才会对编程产生更深的思考,而不是现在这样,代码都不能完全理解,却去纠结为什么要这样。
打开App,查看更多内容
随时随地看视频慕课网APP