类族对象统计

问题描述:

类person派生出类student和类teacher,其中student类派生出类graduate类和undergraduate类。用各种方法建立类对象,并统计各类对象的个数。

代码如下:

#include<iostream>
using namespace std;

class person
{                 //定义person类 其中私有成员数据id
private:
	int id;   //人物编号
	static int numberp;   //静态数据成员 记录person类对象的个数
public:
	person(int id=0)
	{
		this->id = id;
		numberp++;
	}
	person(person &p)
	{
		id = p.id;
		numberp++;
	}
	void datap()       //用来输出person类的私有数据成员
	{
		cout << "person编号为:" << id << endl;
	}
	void showp()     //用来输出person类对象的个数
	{
		cout << "person类对象的个数为:" << numberp << endl;
	}
	~person()
	{

	}
};
int person::numberp = 0;

class student : public person
{                 //定义student类,由person类派生出  其中私有成员数据ids 
private:
	int ids;    //表示学生学号
	static int numbers;    //记录student类的个数
public:
	student(int ids=0,int id=0) : person(id)
	{
		this->ids = ids;
		numbers++;
	}
	student(student &s)
	{
		ids = s.ids;
		numbers++;
	}
	void datas()
	{
		cout << "student学号为:" << ids << endl;
	}
	void shows()
	{
		cout << "student类对象的个数为:" << numbers << endl;
	}
	~student()
	{

	}
};
int student::numbers = 0;

class teacher : public person
{                //定义teacher类,由类person派生出  其中私有数据成员idt
private:
	int idt;    //表示教师的工号
	static int numbert;   //记录teacher类的个数
public:
	teacher(int idt=0,int id=0) : person(id)
	{
		this->idt = idt;
		numbert++;
	}
	teacher(teacher &t)
	{
		idt = t.idt;
		numbert++;
	}
	void datat()   //展示teacher类的私有成员数据
	{
		cout << "teacher工号为:" << idt << endl;
	}
	void showt()   //展示teacher类对象的个数
	{
		cout << "teacher类对象的个数为:" << numbert << endl;
	}
	~teacher()
	{

	}
};
int teacher::numbert = 0;

class graduate : public student
{                 //定义graduate类,是由student类派生出,而student类是由person类派生出
private:
	int idg;     //graduate类的私有数据成员
	static int numberg;      //用于记录graduate类对象的个数
public:
	graduate(int idg = 0, int ids = 0, int id = 0) : student(ids,id)
	{
		this->idg = idg;
		numberg++;
	}
	graduate(graduate &g)
	{
		idg = g.idg;
		numberg++;
	}
	void datag()
	{
		cout << "graduate学号为:" << idg << endl;
	}
	void showg()       //用于显示graduate类对象的个数
	{
		cout << "graduate类对象的个数为:" << numberg << endl;
	}
	~graduate()
	{

	}
};
int graduate::numberg = 0;
 
class undergraduate : public student
{                    //定义undergraduate类,是由student类派生出,而student类是由person类派生出
private:
	int idu;         //undergraduate类的私有数据成员
	static int numberu;          //用于记录undergraduate类对象的个数
public:
	undergraduate(int idu, int ids, int id) : student(ids, id)
	{
		this->idu = idu;
		numberu++;
	}
	undergraduate(undergraduate &u)
	{
		idu = u.idu;
		numberu++;
	}
	void datau()
	{
		cout << "undergraduate学号为:" << idu << endl;
	}
	void showu()
	{
		cout << "undergraduate类对象的个数为:" << numberu << endl;
	}
	~undergraduate()
	{

	}
};
int undergraduate::numberu = 0;

int main()
{
	person p1(12);
	cout << "建立person类对象1后" << endl;
	p1.showp();
	person p2(p1);
	cout << "建立person类对象2后(利用person类对象对其进行赋值)" << endl;
	p2.showp();
	student s1(12,34);
	cout << "建立student类对象1后" << endl;
	s1.showp();
	s1.shows();
	student s2(s1);
	cout << "建立student类对象2后(利用student类对象对其进行赋值)" << endl;
	s2.showp();
	s2.shows();
	teacher t1(23, 22);
	cout << "建立teacher类对象后" << endl;
	t1.showp();
	t1.showt();
	graduate g1(13, 24, 45);
	cout << "建立graduate类对象1后" << endl;
	g1.showp();
	g1.shows();
	g1.showg();
	graduate g2(g1);
	cout << "建立graduate类对象2后(利用graduate对象给其赋值)" << endl;
	g2.showp();
	g2.shows();
	g2.showg();
	undergraduate u1(23, 33, 44);
	cout << "建立undergraduate类对象后" << endl;
	u1.showp();
	u1.shows();
	u1.showu();
	person p3(s1);
	cout << "建立person类对象3后(利用student对象给其初始化)" << endl;
	p3.showp();
}

问题总结:

对于派生类和基类的关系,派生类继承了基类的成员数据(函数),又可能增添了新的数据成员(函数),所以派生类包含基类少的(基类)调用多的(派生类)没问题,但是反过来就可能会有问题。

对于静态成员数据,多个对象都可调用,但是不同对象中的静态成员数据占用同一个内存空间。改变一个,其余的都会变。必须类里面定义,类外边初始化

对于派生类的构造函数和拷贝构造函数,函数体中只用对派生类新加的成员数据进行处理即可。

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
统计建模与R软件 统计建模与R软件 上下册 第1章 概率统计的基本知识 1.1 随机事件与概率 1.1.1随机事件 1.1.2 概率 1.1.3 古典概型 1.1.4 几何概型 1.1.5 条件概率 1.1.6 概率的乘法公式、全概率公式、Bayes公式 1.1.7 独立事件 1.1.8 n重Bemoulli试验及其概率计算 1.2 随机变量及其分布 1.2.1随机变量的定义 1.2.2 随机变量的分布函数 1.2.3 离散型随机变量 1.2.4 连续型随机变量 1.2.5 随机向量 1.3 随机变量的数字特征 1.3.1数学期望 1.3.2 方差 1.3.3 几种常用随机变量分布的期望与方差 1.3.4 协方差与相关系数 1.3.5 矩与协方差矩阵 1.4 极限定理 1.4.1大数定律 1.4.2 中心极限定理 1.5 数理统计的基本概念 1.5.1 总体、个体、简单随机样本 1.5.2 参数空间与分布族 1.5.3 统计量和抽样分布 1.5.4 正态总体样本均值与样本方差的分布 习题 第2章 R软件的使用 2.1 R软件简介 2.1.1 R软件的下载与安装 2.1.2 初识R软件 2.1.3 R软件主窗口命令与快捷方式 2.2 数字、字符与向量 2.2.1向量 2.2.2 产生有规律的序列 2.2.3 逻辑向量 2.2.4 缺失数据 2.2.5 字符型向量 2.2.6 复数向量 2.2.7 向量下标运算 2.3 对象和它的模式与属性 2.3.1 固有属性:mode和length 2.3.2 修改对象的长度 2.3.3 attributes()和attr()函数 2.3.4 对象的class属性 2.4 因子 2.4.1 factor()函数 2.4.2 tapply()数 2.4.3 gl()函数 2.5 多维数组和矩阵 2.5.1 生成数组或矩阵 2.5.2 数组下标 2.5.3 数组的四则运算 2.5.4 矩阵的运算 2.5.5与矩阵(数组)运算有关的函数 …… 第3章 数据描述性分析 第4章 参数估计 第5章 假设检验 第6章 回归分析 第7章 方差分析 第8章 应用多元分析(Ⅰ) 第9章 应用多元分析(Ⅱ) 第10章 计算机模拟 附录 索引 参考文献

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值