本题要求定义一个简单的学生类,数据成员仅需要定义学号和姓名,函数成员的原型见给出的代码,请给出函数成员的类外完整实现。函数接口定义:

本文介绍了如何在C++中定义一个名为Student的类,包含私有成员变量m_id和m_name,以及构造函数、析构函数和print()方法。示例展示了如何创建并操作stu_array学生对象。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. 24. 6-49.自定义的学生类
    #include <iostream>
    #include <cstring>
    using namespace std;
    class  Student
    {
    private:
            int  m_id;
            char  m_name[10];
    public:
            Student(int  id=0,char  *name="");
            ~Student();
            void  print();
            
    };
    Student::Student(int  id,char  *name){
    	m_id=id;
    	strcpy(m_name,name);
    	if (strcmp(m_name,"")==0){
    	cout<<"Hi! "<<id<<" "<<"NULL"<<endl;
    	}
    	else{
    		cout<<"Hi! "<<id<<" "<<m_name<<endl;
    	}
    }
    Student::~Student(){
    	if(strcmp(m_name,"")==0){
    		cout<<"Bye! "<<m_id<<" "<<"NULL"<<endl;
    	}
    	else{
    		cout<<"Bye! "<<m_id<<" "<<m_name<<endl;
    	}
    }
    void Student::print(){
    	cout<<m_id<<" "<<m_name<<endl;
    }
    int  main()
    {
            Student  stu_array[3]={Student(1,"Zhang"),Student(2,"Wang")};
            return  0;
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值