12周:项目1-教师兼干部类

本文介绍了一个使用C++实现的多重继承示例,通过Teacher(教师)类和Cadre(干部)类派生出Teacher_Cadre(教师兼干部)类,并展示了如何在派生类中调用基类的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

【项目1 - 教师兼干部类】
分别定义Teacher(教师)类和Cadre(干部)类,采用多重继承方式由这两个类派生出新类Teacher_Cadre(教师兼干部)。要求: 
(1)在两个基类中都包含姓名、年龄、性别、地址、电话等数据成员。 
(2)在Teacher类中还包含数据成员title(职称),在Cadre类中还包含数据成员post(职务),在Teacher_Cadre类中还包含数据成员wages(工资)。 
(3)对两个基类中的姓名、年龄、性别、地址、电话等数据成员用相同的名字,在引用这些数据成员时,指定作用域。 
(4)在类体中声明成员函数,在类外定义成员函数。 
(5)在派生类Teacher_Cadre的成员函数show中调用Teacher类中的display函数,输出姓名、年龄、性别、职称、地址、电话,然后再用cout语句输出职务与工资
 
 
 
#include<string>
#include <iostream>
using namespace std;

class Teacher
{
public:
    Teacher(string nam,string tit,string ad,string tel,int a,char s);
    void display();
protected:
    string name;
    string title;
    string address;
    string telephone;
    int age;
    char sex;
};

Teacher::Teacher(string nam,string tit,string ad,string tel,int a,char s):
    name(nam),title(tit),address(ad),telephone(tel),age(a),sex(s){}

void Teacher::display()
{
    cout<<"name: "<<name<<endl;
    cout<<"title: "<<title<<endl;
    cout<<"address: "<<address<<endl;
    cout<<"telephone: "<<telephone<<endl;
    cout<<"age: "<<age<<endl;
    cout<<"sex: "<<sex<<endl;
}
class Cadre
{
public:
    Cadre(string nam,string p,string ad,string tel,int a,char s);
    void display();
protected:
    string name;
    string post;
    string address;
    string telephone;
    int age;
    char sex;
};
Cadre::Cadre(string nam,string p,string ad,string tel,int a,char s):
    name(nam),post(p),address(ad),telephone(tel),age(a),sex(s){}

void Cadre::display()
{
    cout<<"name: "<<name<<endl;
    cout<<"post: "<<post<<endl;
    cout<<"address: "<<address<<endl;
    cout<<"telephone: "<<telephone<<endl;
    cout<<"age: "<<age<<endl;
    cout<<"sex: "<<sex<<endl;
}
class Teacher_Cadre:public Teacher,public Cadre
{
public:
    Teacher_Cadre(string nam,string tit,string p,string ad,string t,int a,char s,float w);
    void show( );
private:
    float wage;
};

Teacher_Cadre::Teacher_Cadre(string nam,string t,string p,string ad,string tel,int a,char s,float w):
    Teacher(nam,t,ad,tel,a,s),Cadre(nam,p,ad,tel,a,s),wage(w) {}

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

int main( )
{
    Teacher_Cadre te_ca("Wang-li","prof.","president","135 Beijing Road,Shanghai","(021)61234567",50,'f',1534.5);
    te_ca.show( );
    return 0;
}

 


 

<think>我们将在Vue3 + uniapp项目中,使用组合式API(Composition API)来实现页面加载时请求接口数据。 步骤: 1. 在页面组件的setup函数中(或者使用<script setup>)使用onMounted生命期钩子来触发数据加载。 2. 使用ref或reactive来创建响应式数据,用于存储接口返回的数据。 3. 使用uniapp提供的uni.request或者使用axios(需要配置)来发起HTTP请求。由于是移动端,注意跨域问题,在uniapp中可以通过配置代理解决,或者后端支持CORS。 4. 处理请求成功失败的情况,将数据存储到响应式变量中。 5. 在模板中展示数据。 注意:由于接口地址是内网地址,在真机调试时可能需要将手机电脑连在同一局域网,并修改地址为电脑的IP地址(或者使用内网穿透工具)。另外,如果接口不支持跨域,需要配置uniapp的代理或者让后端设置CORS。 以下是一个示例代码,假设我们使用<script setup>语法糖。 由于uniapp中不支持直接使用axios(需要额外引入,且可能涉及跨平台问题),我们使用uni.request。</think>以下是在 Vue3 + Uniapp 项目中加载接口数据的完整解决方案: ### 解决方案代码 ```vue <script setup> import { ref, onMounted } from 'vue'; // 响应式数据 const surveyList = ref([]); // 问卷列表数据 const total = ref(0); // 总条数 const loading = ref(false); // 加载状态 // 获取问卷数据 const fetchSurveyData = async () => { try { loading.value = true; const baseUrl = 'http://172.26.26.43/dev-api'; const res = await uni.request({ url: `${baseUrl}/wjdc/wj/listTx`, method: 'GET', data: { pageNum: 1, pageSize: 10, dcWjTitle: '', dcId: '', dcDept: '' } }); // uniapp返回结构: [err, res] if (res[0]) throw res[0]; // 请求错误 const { data } = res[1]; if (data.code === 200) { surveyList.value = data.rows; total.value = data.total; } else { throw new Error(data.msg || '请求失败'); } } catch (error) { uni.showToast({ title: error.message || '加载失败', icon: 'none' }); } finally { loading.value = false; } }; // 页面加载时执行 onMounted(() => { fetchSurveyData(); }); </script> <template> <view class="container"> <!-- 加载状态 --> <uni-load-more v-if="loading" status="loading" /> <!-- 数据展示 --> <view v-else> <text class="total">共 {{ total }} 条记录</text> <view v-for="(item, index) in surveyList" :key="index" class="card"> <view class="title">{{ item.dcWjTitle }}</view> <view class="info"> <text>部门: {{ item.dcDept }}</text> <text>创建人: {{ item.createBy }}</text> </view> <view class="score">得: {{ item.score }}</view> </view> </view> </view> </template> <style scoped> .container { padding: 20rpx; } .total { display: block; margin-bottom: 20rpx; color: #888; } .card { padding: 30rpx; margin-bottom: 30rpx; border-radius: 16rpx; background-color: #fff; box-shadow: 0 4rpx 12rpx rgba(0,0,0,0.05); } .title { font-size: 32rpx; font-weight: bold; margin-bottom: 20rpx; } .info { display: flex; justify-content: space-between; color: #666; margin-bottom: 15rpx; } .score { color: #e74c3c; font-weight: bold; } </style> ``` ### 代码解释 1. **响应式数据管理**: - `surveyList`:存储接口返回的问卷列表数据 - `total`:存储数据总数 - `loading`:控制加载状态显示 2. **数据获取流程**: - 使用 `uni.request` 发起 GET 请求 - 请求参数通过 `data` 字段传递 - 处理 uniapp 特有的响应结构 `[err, res]` - 根据接口返回的 `code` 判断请求状态 3. **错误处理**: - 使用 `try/catch` 捕获请求异常 - 通过 `uni.showToast` 显示错误信息 - 网络错误业务错误统一处理 4. **页面生命期**: - 在 `onMounted` 钩子中调用数据获取方法 - 确保页面加载时自动请求数据 5. **UI 展示**: - 使用 `uni-load-more` 组件显示加载状态 - 卡片式布局展示问卷数据 - 关键信息高亮显示(标题、数等) ### 注意事项 1. **跨域问题**: - 开发阶段需在 `manifest.json` 配置代理: ```json "h5": { "devServer": { "proxy": { "/dev-api": { "target": "http://172.26.26.43", "changeOrigin": true } } } } ``` - 真机调试需确保手机API服务器在同一局域网 2. **安全优化**: - 生产环境应将API地址提取到环境变量中 - 添加请求超时处理(示例中未体现,可添加 `timeout: 10000`) 3. **性能优化**: - 大数据量时使用页加载 - 添加下拉刷新功能(可使用 `uni.onPullDownRefresh`)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值