C++面向对象类的实例题目七

题目描述:

编写两个有意义的类,使一个类嵌套在另一个类中。

分析:

本题涉及两个类student和cdegree,前者为学生类,包含学生的学号(nubner),姓名(name)和成绩(degree),而成绩degree是类cdegree的对象。cdegree类有3个数据成员,分别为数学(math),英语(english)和物理(phy)分数。


程序代码:

#include<iostream>
#include<string>
using namespace std;
class Student 
{
	public:
		void getdata();
		void showdata();
	private:
		string number;
		string name;
		class Cdegree
		{
			public:
				double math;
				double english;
				double phy;
		}degree;				
};
void Student::getdata() 
{
	cout<<"Input number:";
	cin>>number;
	cout<<"Input name:";
	cin>>name;
	cout<<"Input degree of math:";
	cin>>degree.math;
	cout<<"Input degree of english:";
	cin>>degree.english;
	cout<<"Input degree of physics:";
	cin>>degree.phy; 
}
void Student::showdata()
{
	cout<<"=========分割线======="<<endl; 
	cout<<"Number:"<<number<<endl;
	cout<<"Name:"<<name<<endl;
	cout<<"Math"<<degree.math<<endl;
	cout<<"English:"<<degree.english<<endl;
	cout<<"Physics:"<<degree.phy<<endl; 
}
int main()
{
	Student s1;
	s1.getdata();
	s1.showdata();
	return 0;
}


结果输出:

Input number:007
Input name:qianshou
Input degree of math:89
Input degree of english:99
Input degree of physics:100
=========分割线=======
Number:007
Name:qianshou
Math89
English:99
Physics:100


转载于:https://www.cnblogs.com/zhezh/p/3773494.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值