嘻唰唰第四批之学生信息的输入输出

代码

#include <stdio.h>
#include <iostream>
#include <string.h>
using namespace std;
class Student
{
private:
    const int maxSchoolPeriod;  //学制
    int no;  //学号
    char name[20];  //姓名
public:
//*****修改的起始行******/
    Student(const int Period,int no,char na[]):maxSchoolPeriod(Period)
    {

        int i=0;
        strcpy(name,na);
        (*this).no = no;
    }
//*****修改的终止行*****/
    void output()
    {
        cout<<maxSchoolPeriod<<" "<<no<<" "<<name<<endl;
    }
};
int  main()
{
    int no;
    char name[20];
    cin>>no>>name;
    Student stu1(4,no,name); //本科生4年
    cin>>no>>name;
    Student stu2(3,no,name); //专科生3年
    stu1.output();
    stu2.output();
    return 0;
}


总结:始终不明白,指针做数据成员的用法。。。。深复制之类的,指针和数组赋值的方法,,,回去慢慢看吧。

就不明白为什么不用深复制

Student(const int Period,int no,char na[]):maxSchoolPeriod(Period)
    {
       
name=new char[strlen(na)+1];

        int i=0;
        for(i=0; i<20||na[i]==0; i++)
            name[i]=na[i];
        (*this).no = no;
    }

数组和指针不是一家人嘛?用法相近?

反正不用深复制挨个儿赋值也对

Student(const int Period,int no,char na[]):maxSchoolPeriod(Period)
    {
        int i=0;
        for(i=0; i<20||na[i]==0; i++)
            name[i]=na[i];
        (*this).no = no;
    }

额,对了,这儿还有一个形参与实参同名的问题,,,

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值