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

524

被折叠的 条评论
为什么被折叠?



