C++语言之继承、组合聚合作业详解

本文介绍了一个使用C++实现的简单类继承示例。基类为学生信息,派生类为大学生信息,包含了学号、姓名、院系及专业等属性,并演示了如何打印这些信息。

1 继承
CStudent


CStudent_college : public CStudent
{


};

大学生-----子类  派生类

学生-------父类  基类


不完整的代码,只是框架:
//main.cpp

#include <iostream>
#include <string.h>

using namespace std;

int main()
{

    CStudent_college college1(1001,"zhangsan","计算机系","软件专业");
    college1.print_student();

    return 0;
}

基类----CStudent
//Student.h
class CStudent  
{

public:
    CStudent();
    virtual ~CStudent();

};


//Student.cpp
CStudent::CStudent()
{

}

CStudent::~CStudent()
{

}


派生类----CStudent_college
//Student_college.h
#include "Student.h"

class CStudent_college : public CStudent  
{
    char *department;
    char *profe;

public:
    CStudent_college();
    virtual ~CStudent_college();
    CStudent_college(int number,char *name,char *department,char *profe);
    void print_student();

};


//Student_college.cpp
// Student_college.cpp: implementation of the CStudent_college class.
//
//////////////////////////////////////////////////////////////////////

#include "Student_college.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CStudent_college::CStudent_college()
{

}

CStudent_college::~CStudent_college()
{

}

CStudent_college::CStudent_college(int number,char *name,char *department,char *profe):CStudent(number,name)
{
    department=new char[strlen(department)+1];
    strcpy(this->department,department);

    profe=new char[strlen(profe)+1];
    strcpy(this->profe,profe);
}

void CStudent_college::print_student()
{
    cout<<department<<profe<<endl;
    CStudent::print_student();

    return;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Bryan Ding

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值