【C++】——继承和派生

🎃个人专栏:

🐬 算法设计与分析:算法设计与分析_IT闫的博客-CSDN博客

🐳Java基础:Java基础_IT闫的博客-CSDN博客

🐋c语言:c语言_IT闫的博客-CSDN博客

🐟MySQL:数据结构_IT闫的博客-CSDN博客

🐠数据结构:​​​​​​数据结构_IT闫的博客-CSDN博客

💎C++:C++_IT闫的博客-CSDN博客

🥽C51单片机:C51单片机(STC89C516)_IT闫的博客-CSDN博客

💻基于HTML5的网页设计及应用:基于HTML5的网页设计及应用_IT闫的博客-CSDN博客​​​​​​

🥏python:python_IT闫的博客-CSDN博客

欢迎收看,希望对大家有用!

目录

🎯第一题:

[🎯 第二题:](#🎯 第二题:)

[🎯 第三题:](#🎯 第三题:)

[🎯 答案:](#🎯 答案:)

💻第一题:

💻第二题:

💻第三题:

🎯第一题:

编写一个学生类,学生数据有编号、姓名、年龄、专业和成绩,要求将编号、姓名、年龄数据设计成一个人类Person,并作为学生类Student的基类。创建Student类对象,并使用显示show()函数对数据进行输出。

🎯 第二题:

设计职员类employee,它继承Person类并包含子对象Date类对象。要求将编号、姓名、性别设计成类Person,Date类包含年、月、日;职员类另有部门及职务成员数据。主程序创建职员类对象并输出信息。

🎯 第三题:

已有类Time和Date,要求设计一个派生类Birth,它继承类Time和类Date,并且增加一个数据成员childname用于表示小孩的名字,设计show函数显示信息;主程序创建Birth类对象并调用show()函数显示信息。

💻第一题:

cpp 复制代码
#include <iostream>

using namespace std;

class Person {

private:

string _id;

string _name;

int _age;

public:

Person(string id,string name,int age);

void show();

};

Person::Person(string id,string name,int age) {

_id=id;

_name=name;

_age=age;

}

void Person::show() {

cout<<"编号:"<<_id<<endl;

cout<<"姓名:"<<_name<<endl;

cout<<"年龄:"<<_age<<endl;

}

//派生类

class Student:public Person {

private:

string _major;

int _score;

public:

Student(string id,string name,int age,string major,int score);

void show();

};

Student::Student(string id,string name,int age,string major,int score)

:Person(id,name,age) {

_major=major;

_score=score;

}

void Student::show() {

Person::show();

cout<<"专业:"<<_major<<endl;

cout<<"成绩:"<<_score<<endl;

}

int main() {

Student stu1("s10001","zhangsan",20,"软件工程",95);

stu1.show();

return 0;

}

💻第二题:

cpp 复制代码
#include <iostream>

using namespace std;

class Person {

private:

string _id;

string _name;

string _sex;

public:

Person(string id,string name,string sex);

void show();

};

Person::Person(string id,string name,string sex) {

_id=id;

_name=name;

_sex=sex;

}

void Person::show() {

cout<<"编号:"<<_id<<endl;

cout<<"姓名:"<<_name<<endl;

cout<<"性别:"<<_sex<<endl;

}

class Date {

public:

Date(int y,int m,int d) {

_year=y;

_month=m;

_day=d;

}

void display() {

cout<<"出生日期:"<<_year<<"."<<_month<<"."<<_day<<endl;

}

protected:

int _month,_day,_year;

};

class Employee:public Person {

public:

Employee(string id,string name,string sex,int y,int m,int d,string dept,string duty);

void show();

private:

Date date;

string _dept;

string _duty;

};

/*************************************************************/

Employee::Employee(string id,string name,string sex,int y,int m,int d,string dept,string duty)

      :date(y,m,d),Person(id,name,sex) {

_dept=dept;

_duty=duty;

}

void Employee::show() {

Person::show();

date.display();

cout<<"部门:"<<_dept<<endl;

cout<<"职务:"<<_duty<<endl;

}

/*************************************************************/

int main() {

Employee employee("10001","杨萍","女",1990,9,29,"团委","团委书记");

employee.show();

return 0;

}

💻第三题:

cpp 复制代码
#include <iostream>

using namespace std;

class Time {

public:

Time(int h,int min,int s) {

hours=h;

minutes=min;

seconds=s;

}

void display() {

cout<<"出生时间:"<<hours<<"时"<<minutes<<"分"<<seconds<<"秒"<<endl;

}

protected:

int hours,minutes,seconds;

};

class Date {

public:

Date(int y,int mon,int d) {

year=y;

month=mon;

day=d;

}

void display() {

cout<<"出生日期:"<<year<<"年"<<month<<"月"<<day<<"日"<<endl;

}

protected:

int year,month,day;

};

class Birth:public Date,public Time{

public:

Birth(string name,int y,int mon,int d,int h,int min,int s);

void show();

private:

string childname;

};

/********************************************************************/

Birth::Birth(string name,int y,int mon,int d,int h,int min,int s):Date(y,mon,d),Time(h,min,s){

childname=name;

}

void Birth::show(){

cout<<"姓名:"<<childname<<endl;

Date::display();

Time::display();

}

/********************************************************************/

int main() {

    Birth birth("李雷",2003,7,1,13,10,10);

    birth.show();

return 0;

}
相关推荐
eggwyw30 分钟前
Anaconda安装卸载的详细流程指南
开发语言·python
码客-ygluu5 小时前
一个通用游戏数据服务器
c++·架构
another heaven5 小时前
【C/C++ Ranges 是零开销抽象吗?拆开管道、惰性求值和编译期膨胀】
c语言·开发语言·c++
SomeOtherTime6 小时前
Rust 闭包语法和原理(AI回答)
开发语言·后端·rust
apcipot_rain7 小时前
C++转Java日记(10)
java·开发语言
总要学点什么7 小时前
第105篇:三维特效-流动图片墙体效果——让箭头沿墙长滚,它俩竟是同一份代码
开发语言·javascript·ecmascript·gis·webgl·材质·cesium
YLCHUP7 小时前
【思维&图论建模】题解:债权(debt)_图论_建模_思路转化_思维题目_C++算法竞赛
c++·笔记·算法·数学建模·贪心算法·深度优先·图论
xiaoye-duck8 小时前
《C++ 项目:手写大模型接入 SDK》会话模块整合:内存 SessionManager 与 DataManager 持久化联动
开发语言·数据库·c++·语言模型
思麟呀8 小时前
背包DP(01背包+完全背包)
c++·算法
Funlang9 小时前
龙芯上的 Fun:从跑起来到机器码
开发语言·龙芯