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 <iostream>
#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 "<<number<<endl<<"name is "<<name<<endl;
    //cout<<"chines is "<<chengji.chinese
    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 <iostream>
#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 "<<chinese<<"  math is "<<math<<"  english is "<<english<<endl;
    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 <iostream>
#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 "<<number<<endl<<"name is "<<name<<endl;
    //cout<<"chines is "<<chengji.chinese
    chengji->print_chengji();
    return;
}

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

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

        

  
访问权限:

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Bryan Ding

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值