YTU 2444: C++习题 对象转换
2444: C++习题 对象转换
时间限制: 1 Sec 内存限制: 128 MB
提交: 914 解决: 581
题目描述
定义一个Teacher(教师)类(教师号,姓名,性别,薪金)和一个Student(学生)类(学号,姓名,性别,成绩),二者有一部分数据成员是相同的,num(号码),name(姓名),sex(性别)。编写程序,将一个Student对象(学生)转换为Teacher(教师)类,只将以上3个相同的数据成员移植过去。可以设想为: 一位学生大学毕业了,留校担任教师,他原有的部分数据对现在的教师身份来说仍然是有用的,应当保留并成为其教师数据的一部分。
输入
一个教师的信息和一个学生的信息
输出
学生的信息和学生转换为教师后的信息
样例输入
10001 Li f 1234.5
20010 Wang m 89.5
样例输出
student1:
num:20010
name:Wang
sex:m
score:89.50
teacher2:
num:20010
name:Wang
sex:m
pay:1500.00
提示
主函数已给定如下,提交时不需要包含,会自动添加到程序尾部
int main()
{
cout<<setiosflags(ios::fixed);
cout<<setprecision(2);
int num;
char name[20];
char sex;
float score;
float pay;
cin>>num>>name>>sex>>pay;
Teacher teacher1(num,name,sex,pay);
cin>>num>>name>>sex>>score;
Student student1(num,name,sex,score);
cout<<"student1:"<<endl;
student1.display();
Teacher teacher2=Teacher(student1);
teacher2.setpay(1500);
cout<<"teacher2:"<<endl;
teacher2.display();
return 0;
}
迷失在幽谷中的鸟儿,独自飞翔在这偌大的天地间,却不知自己该飞往何方……
#include <iostream>
#include <iomanip>
using namespace std;
class Student
{
public:
Student(int n,char *na,char s,float sc):num(n),sex(s),score(sc)
{
int i;
for(i=0; na[i]!='\0'; i++)
{
name[i]=na[i];
}
name[i]='\0';
}
void display()
{
cout<<"num:"<<num<<endl<<"name:";
for(int i=0; name[i]!='\0'; i++)cout<<name[i];
cout<<endl<<"sex:"<<sex<<endl;
cout<<"score:"<<score<<endl;
}
int num;
char name[20],sex;
float score,pay;
};
class Teacher:public Student
{
public:
Teacher(int n,char *na,char s,float p):Student(n,na,s,p),num(n),sex(s) {}
Teacher(Student&a):Student(num,name,sex,score)
{
sex=a.sex;
num=a.num;
int i;
for(i=0; a.name[i]!='\0'; i++)name[i]=a.name[i];
name[i]='\0';
}
void setpay(int n)
{
pay=n;
}
void display()
{
cout<<"num:"<<num<<endl<<"name:";
for(int i=0; name[i]!='\0'; i++)cout<<name[i];
cout<<endl<<"sex:"<<sex<<endl;
cout<<"pay:"<<pay<<endl;
}
int num;
char name[20],sex;
float score,pay;
};
int main()
{
cout<<setiosflags(ios::fixed);
cout<<setprecision(2);
int num;
char name[20];
char sex;
float score;
float pay;
cin>>num>>name>>sex>>pay;
Teacher teacher1(num,name,sex,pay);
cin>>num>>name>>sex>>score;
Student student1(num,name,sex,score);
cout<<"student1:"<<endl;
student1.display();
Teacher teacher2=Teacher(student1);
teacher2.setpay(1500);
cout<<"teacher2:"<<endl;
teacher2.display();
return 0;
}
#include <iostream>
#include <iomanip>
using namespace std;
class Student
{
public:
Student(int n,char *na,char s,float sc):num(n),sex(s),score(sc)
{
int i;
for(i=0; na[i]!='\0'; i++)
{
name[i]=na[i];
}
name[i]='\0';
}
void display()
{
cout<<"num:"<<num<<endl<<"name:";
for(int i=0; name[i]!='\0'; i++)cout<<name[i];
cout<<endl<<"sex:"<<sex<<endl;
cout<<"score:"<<score<<endl; }
int num;
char name[20],sex;
float score,pay;
};
class Teacher:public Student
{
public:
Teacher(int n,char *na,char s,float p):Student(n,na,s,p),num(n),sex(s) {}
Teacher(Student&a):Student(num,name,sex,score)
{
sex=a.sex;
num=a.num;
int i;
for(i=0; a.name[i]!='\0'; i++)name[i]=a.name[i];
name[i]='\0';
}
void setpay(int n)
{
pay=n;
}
void display()
{
cout<<"num:"<<num<<endl<<"name:";
for(int i=0; name[i]!='\0'; i++)cout<<name[i];
cout<<endl<<"sex:"<<sex<<endl;
cout<<"pay:"<<pay<<endl;
}
int num;
char name[20],sex;
float score,pay;
};
int main()
{
cout<<setiosflags(ios::fixed);
cout<<setprecision(2);
int num;
char name[20];
char sex;
float score;
float pay;
cin>>num>>name>>sex>>pay;
Teacher teacher1(num,name,sex,pay);
cin>>num>>name>>sex>>score;
Student student1(num,name,sex,score);
cout<<"student1:"<<endl;
student1.display();
Teacher teacher2=Teacher(student1);
teacher2.setpay(1500);
cout<<"teacher2:"<<endl;
teacher2.display();
return 0;
}
YTU 2444: C++习题 对象转换的更多相关文章
- YTU 2432: C++习题 对象数组输入与输出
2432: C++习题 对象数组输入与输出 时间限制: 1 Sec 内存限制: 128 MB 提交: 1603 解决: 1152 题目描述 建立一个对象数组,内放n(n<10)个学生的数据( ...
- C++习题 对象转换
[Submit][Status][Web Board] Description 定义一个Teacher(教师)类(教师号,姓名,性别,薪金)和一个Student(学生)类(学号,姓名,性别,成绩),二 ...
- 采用Lambda表达式快速实现实体模型对象转换到DTO
在项目中,采用code first时建立的模型对象不能直接用于数据传输,需要从新根据需求建立Dto对象 为什么需要建立Dto对象呢? DTO即数据传输对象.之前不明白有些框架中为什么要专门定义DTO来 ...
- Newtonsoft.Json 把对象转换成json字符串
var resultJson = new { records = rowCount, page = pageindex, //总页数=(总页数+页大小-1)/页大小 total = (rowCount ...
- python class对象转换成json/字典
# -*- encoding: UTF-8 -*- class Student: name = '' age = 0 def __init__(self, name, age): self.name ...
- 前台 JSON对象转换成字符串 相互转换 的几种方式
在最近的工作中,使用到JSON进行数据的传递,特别是从前端传递到后台,前台可以直接采用ajax的data函数,按json格式传递,后台Request即可,但有的时候,需要传递多个参数,后台使用requ ...
- javascript 中关于对象转换数字值的一些特点
下面是摘至<Javascript 高级程序设计第三版>里的一段话 是关于对象转换数字值的一些规则 "在应用于对象时,先调用对象的valueOf()方法以取得一个可供操作的值.然后 ...
- android XMl 解析神奇xstream 五: 把复杂对象转换成 xml ,并写入SD卡中的xml文件
前言:对xstream不理解的请看: android XMl 解析神奇xstream 一: 解析android项目中 asset 文件夹 下的 aa.xml 文件 android XMl 解析神奇xs ...
- android XMl 解析神奇xstream 三: 把复杂对象转换成 xml
前言:对xstream不理解的请看: android XMl 解析神奇xstream 一: 解析android项目中 asset 文件夹 下的 aa.xml 文件 android XMl 解析神奇xs ...
随机推荐
- 回顾基础知识,类,fbv,cbv
一 类中绑定方法的传参,不需要self class Foo(object): def __init__(self,name): self.name = name def foo(self,x): se ...
- uva 10692 高次幂取模
Huge Mod Input: standard input Output: standard output Time Limit: 1 second The operator for exponen ...
- 稻草人(bzoj 4237)
Description JOI村有一片荒地,上面竖着N个稻草人,村民们每年多次在稻草人们的周围举行祭典. 有一次,JOI村的村长听到了稻草人们的启示,计划在荒地中开垦一片田地.和启示中的一样,田地需要 ...
- Codeforces Round #291 (Div. 2) D. R2D2 and Droid Army [线段树+线性扫一遍]
传送门 D. R2D2 and Droid Army time limit per test 2 seconds memory limit per test 256 megabytes input s ...
- linux基础命令之一
1.cpio cpio(copy in/out) 功能说明:备份文件. 语 法:cpio [-0aABckLovV][-C <输入/输出大小>][-F <备份档>][-H &l ...
- laravel的视图
//输出视图 //建立控制器方法public function hello_test(){ return view('member/hello_test',['name'=>'张三','age' ...
- CKeditor如何实现图片上传功能
http://makaiyuan.blog.51cto.com/5819595/1049521 如何在数据库中导入excel文件内的数据:http://jingyan.baidu.com/album/ ...
- ubuntu下不同版本python安装pip及pip的使用
由于ubuntu系统自带python2.7(默认)和python3.4,所以不需要自己安装python. 可以使用python -V和python3 -V查看已安装python版本. 在不同版本的py ...
- String源码分析(1)--哈希篇
本文基于JDK1.8,首发于公众号:Plus技术栈 让我们从一段代码开始 System.out.println("a" + "b" == "ab&qu ...
- Golang 入门 : 等待 goroutine 完成任务
Goroutine 是 Golang 中非常有用的功能,但是在使用中我们经常碰到下面的场景:如果希望等待当前的 goroutine 执行完成,然后再接着往下执行,该怎么办?本文尝试介绍这类问题的解决方 ...