4.4 c++ 上机 写一个动态的数组类

写一个关于学生的动态数组类
其中还卡了一会儿,因为直接调用了封装好的动态数组类中的private成员不自知,卡了好久。。注意细节吧

#include <bits/stdc++.h>
using namespace std;

class student
{
private:
    int age;
    string name;
    string major;
public:
    student()
    {
        name="none";
        age=-1;
        major="none";
        printf("constructor called\n");
    }
    student(int sa,string sname,string smajor)
    {
         printf("constructor called\n");
        age=sa;
        name=sname;
        major=smajor;
    }
    void setinfo(int sa,string sname,string smajor)
    {
        age=sa;
        name=sname;
        major=smajor;
    }
    void display()
    {
        cout<<"Student's name: "<<name<<endl;
        printf("age: %d \n",age);
        printf("Major: ");
        cout<<major<<endl;
    }
    string getname()
    {
        return name;
    }
    void setname(string nn)
    {
        name=nn;
    }
    string getmajor()
    {
        return major;
    }
    void changemajor(string t)
    {
        major=t;
    }
    ~student()
    {
        printf("DESTRUCTOR CALLED!\n");
    }
};


class arrayofstu
{
public:
    arrayofstu(int ssize):_size(ssize)
    {
        stu1=new student[_size];
    }
    ~arrayofstu()
    {
        printf("deleting....\n");
        delete[] stu1;
    }
    student &element(int t)
    {
        assert(t>=0&&t<_size);
        return stu1[t];
    }

private:
    student *stu1;
    int _size;
};

int main()
{
    int count1;
    printf("please enter the number of student:\n");
    cin>>count1;
    arrayofstu _stu(count1);
    printf("please enter the info of each student\n");
    for(int i=0; i<count1; i++)
    {
        int age;
        string name;
        string major;
        cin>>name>>age>>major;
        _stu.element(i).setinfo(age,name,major);
    }
    printf("please enter the no. and the new major of student who want to change major\n");
    string t;
    int p;
    cin>>p>>t;
    _stu.element(p-1).changemajor(t);
    _stu.element(p-1).display();
    return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值