为了账号安全,请及时绑定邮箱和手机立即绑定

用构造函数的话第46行这里要怎么写

#include <iostream>

#include <string>

using namespace std;

/**

 * 定义类:Student

 * 数据成员:m_strName

 * 无参构造函数:Student()

 * 有参构造函数:Student(string _name)

 * 拷贝构造函数:Student(const Student& stu)

 * 析构函数:~Student()

 * 数据成员函数:setName(string _name)、getName()

 */

class Student

{public:

  Student()

{  m_strName="";

  



}

  Student(string name):m_strName(name)

  {

  

  }

  Student(const Student& stu)

  {

  

  }

  ~Student(){};

   void setName(string name)

  {m_strName= name;

  }

 string getName()

  {return m_strName;

  

  }

private:

  string m_strName;

  

};

int main(void)

{

    // 通过new方式实例化对象*stu

    Student *stu=new Student();

    // 更改对象的数据;成员为“慕课网”

 new Student("muke");

    // 打印对象的数据成员

cout<<stu->getName()<<endl;

delete stu;

stu= NULL;

return 0;

}


正在回答

2 回答

#include <iostream>
#include <string>
using namespace std;
/**
 * 定义类:Student
 * 数据成员:m_strName
 * 无参构造函数:Student()
 * 有参构造函数:Student(string _name)
 * 拷贝构造函数:Student(const Student& stu)
 * 析构函数:~Student()
 * 数据成员函数:setName(string _name)、getName()
 */
class Student{
    public:
    Student();
    Student(string _name);
    Student(const Student &stu);
    ~Student();
    void setName(string _name);
    string getName();
    
    string m_strName;
};

Student::Student(){
    cout << "no argument" << endl;
}
Student::Student(string _name):m_strName(_name){
    cout << "one argument" << endl;
}
Student::Student(const Student &stu){
    cout << "copy constructor" << endl;
}
Student::~Student(){
    cout << "~ fun" << endl;
}
void Student::setName(string _name){
    m_strName = _name;
}
string Student::getName(){
    return m_strName;
}

int main(void)
{
    // 通过new方式实例化对象*stu
    Student *stu = new Student("111");
    // 更改对象的数据成员为“慕课网”
    cout << stu->m_strName << endl;
    stu->m_strName="慕课网0";
    // 打印对象的数据成员
    cout << stu->m_strName << endl;
    stu->setName("慕课网1");
    cout << stu->getName() << endl;
    delete stu;
    Student stu1("慕课网2");
    cout << stu1.getName() << endl;
    Student stu2(stu1);
    
    return 0;
}
0 回复 有任何疑惑可以回复我~
#include <iostream>
#include <string>
using namespace std;
/**
 * 定义类:Student
 * 数据成员:m_strName
 * 无参构造函数:Student()
 * 有参构造函数:Student(string _name)
 * 拷贝构造函数:Student(const Student& stu)
 * 析构函数:~Student()
 * 数据成员函数:setName(string _name)、getName()
 */

class Student {
public:
    Student() ;
    Student(string _name);
    Student(const Student&stu);
    ~Student();
    void setName(string _name){
         m_strName=_name;
    }
    string getName(){
        return m_strName;
    }
private:
     string  m_strName;

};
int main(void)
{
    // 通过new方式实例化对象*stu
    Student *stu = new Student();
    // 更改对象的数据成员为“慕课网”
    stu->setName("慕课网");
    // 打印对象的数据成员
    cout<<stu->getName()<<endl;
    delete stu;
    stu =NULL;
    return 0;
}
Student::Student(){
    cout<<"Student()"<<endl;
}

Student::Student(string _name){
cout<<"Student(string _name)"<<endl;

}
Student::Student(const Student& stu){
    cout<<"Student(const Student& stu"<<endl;
}
Student::~Student() {
    cout<<"~Student()"<<endl;
}


0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消
C++远征之封装篇(上)
  • 参与学习       103381    人
  • 解答问题       732    个

封装--面向对象的基石,本教程力求帮助小伙伴们即学即会

进入课程

用构造函数的话第46行这里要怎么写

我要回答 关注问题
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号