继承

#include<iostream>
#include<string.h>
#include<stdlib.h>
using namespace std;
class Student
{
public:
    Student()
    {
        num=capacity=0;
        name=new char [capacity];
    }
    Student(int n)
    {
        capacity=n;
        num=0;
        name=new char [capacity];
    }
    ~Student()
    {
        delete [] name;
    }
    void push(int n,char s[])
    {
        num=n;
        strcpy(name,s);
    }
    void pop()
    {
        cout<<"姓名:"<<name<<endl;
        cout<<"年龄:"<<num<<endl;
    }
private:
    int num;
    char *name;
    int capacity;
};
class GraduateStudent:public Student
{
public:
    GraduateStudent(int n,int a):Student(n)//用列表的方式继承基类中的构造函数
    {
         capacity=a;
         sub=new char [capacity];
    }
    ~GraduateStudent()
    {
        delete [] sub;
    }
    void push1(char s[])
    {
        strcpy(sub,s);
    }
    void pop1()
    {
        cout<<"专业:"<<sub<<endl;
    }
private:
    char *sub;
    int capacity;
};
int main()
{
    GraduateStudent p(10,10);
    p.push(23,"liming");
    p.push1("jisuanji");
    p.pop();
    p.pop1();
}
阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页