C++之组合、聚合类间关系


组合


给学生类  里  放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.
//
///////////////////////

你可能感兴趣的:(C++,C/C++以下克上)