• C++语言之组合、聚合类间关系、访问权限、多模块作业详解


    1 类间的关系
    组合    -----   组装         学生----成绩
    聚合    -----   聚集         主窗口-----  子窗口


    组合 的使用:
    给学生类  里  放1个  成绩
    成绩-----变量     特殊的变量(类)

    在定义 学生的构造函数时,如何给 chengji变量 赋值?
        利用 CScore的构造函数 来给 chengji 赋值。
        引出了  初始化表-------  :chengji(chinese,math,englis)   放置在 函数原型后面(cpp中的,.h不用变)

    CStudent::CStudent(int number,char *name,int chinese,int math,int english):chengji(chinese,math,english)//初始化表
    {
        this->number=number;
        this->name=new char[strlen(name)+1];
        strcpy(this->name,name);
        //this->chinese=chinese;
        //this->math=math;
        //this->english=english;
        //chengji.CScore(chinese,math,english);   因为CScore()是构造函数,不能这么用
    }    


    小技巧:
    调用CStudent的print函数时,可以调用 CScore的print函数

    组合代码:
    //main.cpp

    #include "Student.h"

    int main()
    {
        CStudent stu1(1001,"zhangsan",61,62,63);
        stu1.print_student();

        return 0;
    }


    //Student.h
    // Student.h: interface for the CStudent class.
    //
    //

    #if !defined(AFX_STUDENT_H__6C8339B9_6432_4E34_A210_A960F4A095CC__INCLUDED_)
    #define AFX_STUDENT_H__6C8339B9_6432_4E34_A210_A960F4A095CC__INCLUDED_

    #if _MSC_VER > 1000
    #pragma once
    #endif // _MSC_VER > 1000

    #include "Score.h"

    class CStudent  
    {
    public:
        int number;
        char *name;
        //int chinese;
        //int math;
        //int english;
        CScore chengji;
        CStudent();
        virtual ~CStudent();
        CStudent(int number,char *name,int chinese,int math,int english);
        void print_student();

    };

    #endif // !defined(AFX_STUDENT_H__6C8339B9_6432_4E34_A210_A960F4A095CC__INCLUDED_)


    //Student.cpp
    // Student.cpp: implementation of the CStudent class.
    //
    //
    #include
    #include "Student.h"

    using namespace std;
    //
    // Construction/Destruction
    //

    CStudent::CStudent()
    {

    }

    CStudent::~CStudent()
    {

    }

    CStudent::CStudent(int number,char *name,int chinese,int math,int english):chengji(chinese,math,english)//初始化表
    {
        this->number=number;
        this->name=new char[strlen(name)+1];
        strcpy(this->name,name);
        //this->chinese=chinese;
        //this->math=math;
        //this->english=english;
        //chengji.CScore(chinese,math,english);   因为CScore()是构造函数,不能这么用
    }

    void CStudent::print_student()
    {
        cout<<"number is "<     //cout<<"chines is "<     chengji.print_chengji();
        return;
    }


    //Score.h
    class CScore  
    {
    public:
        int chinese;
        int math;
        int english;
        CScore();
        CScore(int chinese,int math,int english);
        virtual ~CScore();
        void print_chengji();

    };


    //Score.cpp
    // Score.cpp: implementation of the CScore class.
    //
    //
    #include
    #include "Score.h"

    using namespace std;

    //
    // Construction/Destruction
    //

    CScore::CScore()
    {

    }

    CScore::~CScore()
    {

    }

    CScore::CScore(int chinese,int math,int english)
    {
        this->chinese=chinese;
        this->math=math;
        this->english=english;        
    }

    void CScore::print_chengji()
    {
        cout<<"chinese is "<     return;
    }


    2 聚合------聚集
    关系 比较 松散


    CStudent 类中的 chengji变量为 指针(CScore *)

    CStudent 类 的 构造函数 不需要给出 chinese math english 这样的值
      只需要 chengji=NULL
      
    CStudent中 可以设计1个 成员函数,如addChengji(71,81,92)
            该函数实现,先给chengji分配内存空间,然后  调用CScore的构造函数来 赋值---------直接使用chengji.chinese=chinese不好
            
    通常还有 delChengji()函数


    //main.cpp

    #include "Student.h"

    int main()
    {
        CStudent stu1(1001,"zhangsan");
        stu1.addChengji(61,62,63);
        stu1.print_student();
        //stu1.del_student();

        return 0;
    }


    //Student.h
    // Student.h: interface for the CStudent class.
    //
    //

    #if !defined(AFX_STUDENT_H__6C8339B9_6432_4E34_A210_A960F4A095CC__INCLUDED_)
    #define AFX_STUDENT_H__6C8339B9_6432_4E34_A210_A960F4A095CC__INCLUDED_

    #if _MSC_VER > 1000
    #pragma once
    #endif // _MSC_VER > 1000

    #include "Score.h"

    class CStudent  
    {
    public:
        int number;
        char *name;
        //int chinese;
        //int math;
        //int english;
        CScore *chengji;
        CStudent();
        virtual ~CStudent();
        CStudent(int number,char *name);
        void print_student();
        void addChengji(int chinese,int math,int english);

    };

    #endif // !defined(AFX_STUDENT_H__6C8339B9_6432_4E34_A210_A960F4A095CC__INCLUDED_)

    //Student.cpp
    // Student.cpp: implementation of the CStudent class.
    //
    //
    #include
    #include "Student.h"

    using namespace std;
    //
    // Construction/Destruction
    //

    CStudent::CStudent()
    {

    }

    CStudent::~CStudent()
    {

    }

    CStudent::CStudent(int number,char *name)
    {
        this->number=number;
        this->name=new char[strlen(name)+1];
        strcpy(this->name,name);
        chengji=NULL;
        //this->chinese=chinese;
        //this->math=math;
        //this->english=english;
        //chengji.CScore(chinese,math,english);   因为CScore()是构造函数,不能这么用
    }

    void CStudent::print_student()
    {
        cout<<"number is "<     //cout<<"chines is "<     chengji->print_chengji();
        return;
    }

    void CStudent::addChengji(int chinese,int math,int english)
    {

        chengji=new CScore(chinese,math,english);
        return;
    }

            

      
    访问权限

    private            -----成员变量 一般都设置为        -------只能内部的成员函数来访问。如果外部(main函数)想访问----通过成员函数
    protected
    public            -----成员函数 一般                -------对外开放的  接口

  • 相关阅读:
    tiup status
    【青书学堂】 2023年第二学期 Bootstrap 前端界面框架技术(高起专) 作业
    stl文件转pcd格式
    一起看 I/O | 用 Health Connect 连通应用间的健康数据
    D. Secret Santa(构造)
    Zookeeper ---- ZooKeeper分布式锁案例
    知识干货:基础存储服务新手体验营
    TPH-yolov5 小目标检测
    TDSQL-C 真·秒级启停:连接断了,又没断
    机器学习笔记 - 构建推荐系统(6) 用于协同过滤的 6 种自动编码器
  • 原文地址:https://blog.csdn.net/zzjlhlcd/article/details/127712002