public: student(string num1,string name1,string no1,double s1):person(num1,name1) { no=no1;score=s1; } void show() { person::show(); cout<<\ } };
class teacher:public person {
private: string pro; string department; public: teacher(string num1,string name1,string p1,string d1):person(num1,name1) { pro=p1; department=d1; } void show() { person::show(); cout<<\ } };
int main() { string n1,nu1,no1; string n2,nu2,p,d; double s; cin>>n1>>nu1>>no1>>s; cin>>n2>>nu2>>p>>d; student one(n1,nu1,no1,s); one.show(); teacher two(n2,nu2,p,d); two.show(); return 0; }
24:长方形的面积和长方体的体积
Time/Memory Limit:1000 MS/32768 K
Submitted: 229 Accepted: 159
Problem Description
先创建一个长方形类,有带参的构造函数,能够计算长方形的面积;在派生出长方体类,有带参的构造函数,能够计算长方体的表面积和体积。
Input
输入数据有多组,每组包含2行数据,第1行2个实数,表示长方形的长和宽;第2行3个实数,表示长方体的长、宽和高。
Output
输出长方形的面积和长方体的表面积和体积;具体格式见样例。
Sample Input
2 3 4 5 6
Sample Output
6
148 120
#include
protected: double l,w; public: A(double l1=0.0,double w1=0.0) { l=l1; w=w1; } double SS() { return l*w; } void show() { cout< class B:public A { private: double h; public: B(double l1=0.0,double w1=0.0,double h1=0.0):A(l1,w1) { h=h1; } double S() { double s; s=2*SS()+2*l*h+2*w*h; return s; } double VV() { return h*SS(); } void show() { cout< int main() { double l1,w1,l2,w2,h2; while(cin>>l1>>w1) { cin>>l2>>w2>>h2; A one(l1,w1); one.show(); B two(l2,w2,h2); two.show(); } return 0; } 25:多继承——Time类和Date类派生出 Birthtime类 Time/Memory Limit:1000 MS/32768 K Submitted: 66 Accepted: 56 Problem Description 定义一个Time类,其数据成员包括时、分、秒,成员函数包括构造函数和输出函数;定义一个Date类,其数据成员包括年、月、日,成员函数包括构造函数和输出函数;定义一个派生类,它继承类Time和Date,并且增加一个数据成员childname用于表示小孩的名字;设计主程序显示孩子的姓名和出生时间。 Input 输入数据有多行,每行包括一个字符串和六个整数,分别表示姓名、出生年月日和出生时分秒。 Output 输出有多行,对应每个输入输出一行。输出格式见Sample Output. Sample Input Jack 2012 1 1 10 10 10 Marry 2011 6 1 8 0 30 Sample Output Name:Jack Birthday:2012/1/1 Time:10:10:10 Name:Marry Birthday:2011/6/1 Time:8:0:30 #include private: int hour,minute,second; public: TIME(int h,int m,int s) { hour=h;minute=m;second=s; } void show() { cout<<\ } }; class DATE { private: int year,month,day; public: DATE(int y,int m1,int d) { year=y;month=m1;day=d; } void show() { cout<<\ } }; class CHILD:public TIME,public DATE { private: string childname; public: CHILD(string n,int y,int m1,int d,int h,int m,int s):DATE(y,m1,d),TIME(h,m,s) { childname=n; } void show() { cout<<\ DATE::show(); TIME::show(); } }; int main() { string n; int y,m1,d,h,m,s; while(cin>>n>>y>>m1>>d>>h>>m>>s) { CHILD one(n,y,m1,d,h,m,s); one.show(); } return 0; } 26:RoundTable类 Time/Memory Limit:1000 MS/32768 K Submitted: 83 Accepted: 54