类的多重继承

/* (程序头部注释开始)

* 程序的版权和版本声明部分

* Copyright (c) 2011, 烟台大学计算机学院学生

* All rights reserved.

* 文件名称:

* 作 者: 张传新

* 完成日期: 2012 年 05 月 08 日

* 版 本 号: 1.0

* 对任务及求解方法的描述部分

* 输入描述:

* 问题描述:

* 程序输出:

* 程序头部的注释结束

*/

#include<iostream>
#include<string>
using namespace std;

class Teacher//定义Teacher类
{
public:
	void display();
	Teacher(string n, int a, char s, string d, int m, string t)
	{
        name = n;
		age = a;
		sex = s;
		addr = d;
		num = m;
		title = t;
	}
	
protected:
	string  name;
	int age;
	char sex;
	string addr;
	int num;
	string title;
};

class Cadre//定义Cadre类
{
public:
	void display();
	Cadre(string n, int a, char s, string d, int m, string p)
	{
		name = n;
		age = a;
		sex = s;
		addr = d;
		num = m;
		post = p;
	}
	
protected:
	string name;
	int age;
	char sex;
	string addr;
	int num;
	string post;
};

class Teacher_Cadre:public Teacher,public Cadre
{
public:
	Teacher_Cadre(string n, int a, char s, string d, int m, string t,string p, float w)
		:Teacher(n,a,s,d,m,t),Cadre(n,a,s,d,m,p),wages(w){}
	void show();
protected:
	float wages;
};

void Teacher::display()
{
	cout << "name:" << name << endl;
	cout << "age:" << age << endl;
	cout << "sex:" << sex << endl;
	cout << "addr:" << addr << endl;
	cout << "num:" << num << endl;
	cout << "title:" << title << endl;
}

void Cadre::display()
{
	cout << "name:" << name << endl;
	cout << "age:" << age << endl;
	cout << "sex:" << sex << endl;
	cout << "addr:" << addr << endl;
	cout << "num:" << num << endl;
	cout << "post:" << post << endl;
}

void Teacher_Cadre::show()
{
    Teacher::display();
    cout << "post:" << post << endl;
	cout << "wages:" << wages << endl;
}

int main()
{
	Teacher Teacher1("小王", 20, 'f', "烟台大学", 110, "老师");
	
	Cadre Cadre1("小野", 19, 'm', "烟台大学", 119, "指导");
	
    Teacher_Cadre Teacher_Cadre1("小张", 20, 'f', "烟台大学", 120, "老师","指导", 18888);
	
	Teacher1.display();
	cout << endl;
	
	Cadre1.display();
	cout << endl;
	
	Teacher_Cadre1.show();
	cout << endl;
	
	system("pause");
    return 0;
}


运行结果:

name:小王
age:20
sex:f
addr:烟台大学
num:110
title:老师

name:小野
age:19
sex:m
addr:烟台大学
num:119
post:指导

name:小张
age:20
sex:f
addr:烟台大学
num:120
title:老师
post:指导
wages:18888

请按任意键继续. . .

 

体会:

不容易啊~~~~但把五一放假忘的东西捡起来不少,明天接着奋斗!!!

### SystemVerilog 中多重继承实现方式 SystemVerilog 并未直接支持 C++ 风格的多重继承机制,而是借鉴了 Java 的接口(interface)概念来间接实现多重继承的功能。这种方式通过让一个实现多个接口并提供具体实现的方式完成功能扩展[^1]。 #### 接口的概念及其作用 在 SystemVerilog 中,“`virtual interface`”并不是指传统意义上的接口,而是一种硬件建模工具。真正的接口抽象由 `virtual class` 提供。虚拟允许声明纯虚函数(pure virtual functions),这些函数必须被派生覆盖以提供具体的实现逻辑。这种设计使得一个可以通过组合不同接口的行为来模拟多重继承的效果[^3]。 以下是基于接口实现的简单示例: ```systemverilog // 定义第一个接口 A virtual class InterfaceA; pure virtual function void methodA(); endclass // 定义第二个接口 B virtual class InterfaceB; pure virtual function void methodB(); endclass // 创建实现了两个接口的具体 DerivedClass class DerivedClass extends InterfaceA implements InterfaceB; function void methodA(); $display("Method A is called."); endfunction function void methodB(); $display("Method B is called."); endfunction endclass module testbench; initial begin DerivedClass obj = new(); // 调用来自 InterfaceA 的方法 obj.methodA(); // 调用来自 InterfaceB 的方法 obj.methodB(); end endmodule ``` 在这个例子中,`DerivedClass` 同时实现了 `InterfaceA` 和 `InterfaceB` 所定义的方法,从而达到了似于多重继承的目的[^4]。 #### 构造函数 (Constructor) 在多重继承中的角色 当涉及到复杂对象初始化时,`new()` 函数作为构造器起着至关重要的作用。如果基中有参数化的构造器,则子也需显式调用它们来进行必要的设置[^2]。例如: ```systemverilog class BaseWithParams; int param; function new(int p); this.param = p; endfunction endclass class Child extends BaseWithParams; string name; function new(string n, int p); super.new(p); // 显式调用父构造器 this.name = n; endfunction endclass ``` 这里展示了如何利用 `super` 关键字访问直系祖先资源的同时保持各自独立存储空间分配策略[^1]。 #### 使用回调模式增强灵活性 对于更复杂的场景比如动态行为修改需求下,可以采用回调技术进一步提升系统的可配置性和适应能力。如下所示: ```systemverilog virtual class CallbackBase; virtual task pre_action(); $display("Default Pre Action"); endtask endclass class CustomCallback extends CallbackBase; virtual task pre_action(); $display("Customized Pre Action"); endtask endclass program automatic callback_demo(CustomCallback cb_obj); initial begin cb_obj.pre_action(); // 输出自定义动作消息 end endprogram :callback_demo ``` 此片段说明即使是在运行期间也可以轻松替换默认处理流程而不改变原有框架结构。 --- ###
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值