c++ day3

1.定义一个Person类,包含私有成员,int *age,string &name,一个Stu类,包含私有成员double *score,Person p1,写出Person类和Stu类的特殊成员函数,并写一个Stu的show函数,显示所有信息。

代码实现:

cpp 复制代码
#include <iostream>

using namespace std;

class Person
{
    int *age;
    string &name;
public:
    Person(int *age,string &name):age(age),name(name){}

    void show()
    {
        cout << "age=" << *age << endl;
        cout << "name=" << name << endl;
    }
};

class Stu
{
    double *score;
    Person p1;
public:
    Stu(double *score,int *age,string &name):score(score),p1(age,name){}
    void show()
    {
        cout << "score=" << *score << endl;
        p1.show();
    }
};

int main()
{
    double s = 80.0;
    int a = 23;
    string n = "jsj";

    Stu res(&s, &a, n);
    res.show();

    return 0;
}

效果:

相关推荐
科技道人1 天前
记录 默认置灰/禁用 app ‘Search Engine Selector‘ 的disable按钮
开发语言·前端·javascript
逝水无殇1 天前
C# 异常处理详解
开发语言·后端·c#
旖-旎1 天前
《LeetCode647 回文子串 || LeetCode 5 最长回文子串》
c++·算法·leetcode·动态规划·哈希算法
玖玥拾1 天前
C# 语言进阶(十五)C# 游戏服务端 MySQL 数据库
服务器·开发语言·网络·数据库·mysql·c#
铅笔侠_小龙虾1 天前
Rust 学习目录
开发语言·学习·rust
Darkwanderor1 天前
对Linux的进程控制的研究
linux·运维·c++
云泽8081 天前
从零吃透 C++ 异常:抛出捕获、栈展开、异常重抛与编码规范详解
开发语言·c++·代码规范
灯澜忆梦1 天前
GO---可见性规则
开发语言·golang
REDcker1 天前
libdatachannel 快速入门
c++·webrtc·datachannel
逝水无殇1 天前
C# 文件的输入与输出详解
开发语言·数据库·后端·c#