C++一道关于继承的题目,求大神解答,感激不尽 40
1001.ThePerson,Student,Employee,FacultyandStaffClassTotal:56Accepted:25TimeLimit:1sec...
1001. The Person, Student, Employee, Faculty and Staff Class
Total: 56 Accepted: 25
Time Limit: 1sec Memory Limit:256MB
Description
Design a class named Person and its two derived classes named Student and Employee. Make Faculty and Staff derived classes of Employee.
Person class has the following member:
string name;
string address;
string phoneNumber;
string email;
Student class has the following member:
enum class_status{frssman,sophomore,junior,senior};
class_status status;
Employee class has the following member:
string office;
int salary;
MyDate dateHired;
Faculty class has the following member:
string officeHours;
int rank;
Staff class has the following member:
string title;
MyDate class contains the following member:
int year;
int month;
int day;
Override the toString function in each class to display the class name.
For example, in Employee class:
string toString()
{
return "Employee";
}
Make sure that the following code:
void f(Person &p)
{
cout << p.toString() << endl;
}
Person person;
Student student;
Employee employee;
Faculty faculty;
Staff staff;
f(person);
f(student);
f(employee);
f(faculty);
f(staff);
output:
Person
Student
Employee
Faculty
Staff 展开
Total: 56 Accepted: 25
Time Limit: 1sec Memory Limit:256MB
Description
Design a class named Person and its two derived classes named Student and Employee. Make Faculty and Staff derived classes of Employee.
Person class has the following member:
string name;
string address;
string phoneNumber;
string email;
Student class has the following member:
enum class_status{frssman,sophomore,junior,senior};
class_status status;
Employee class has the following member:
string office;
int salary;
MyDate dateHired;
Faculty class has the following member:
string officeHours;
int rank;
Staff class has the following member:
string title;
MyDate class contains the following member:
int year;
int month;
int day;
Override the toString function in each class to display the class name.
For example, in Employee class:
string toString()
{
return "Employee";
}
Make sure that the following code:
void f(Person &p)
{
cout << p.toString() << endl;
}
Person person;
Student student;
Employee employee;
Faculty faculty;
Staff staff;
f(person);
f(student);
f(employee);
f(faculty);
f(staff);
output:
Person
Student
Employee
Faculty
Staff 展开
展开全部
#include<iostream>
#include<string>
using namespace std;
class MyDate {
int year;
int month;
int day;
};
class Person {
string name;
string address;
string phoneNumber;
string email;
public:
virtual string toString() { //这个函数要被重载的,必须定义成virtual
return "Person";
}
};
class Student : public Person {
enum class_status {
frssman, sophomore, junior, senior
};
class_status status;
public:
string toString() {
return "Student";
}
};
class Employee : public Person {
string office;
int salary;
MyDate dateHired;
public:
virtual string toString() { //这个函数要被重载的,必须定义成virtual
return "Employee";
}
};
class Faculty : public Employee {
string officeHours;
int rank;
public:
string toString() {
return "Faculty";
}
};
class Staff : public Employee {
string title;
public:
string toString() {
return "Staff";
}
};
void f(Person &p) { //这个函数不能放在class中。
cout << p.toString() << endl;
}
int main() {
Person person;
Student student;
Employee employee;
Faculty faculty;
Staff staff;
f(person);
f(student);
f(employee);
f(faculty);
f(staff);
system("PAUSE");
return 0;
}
#include<string>
using namespace std;
class MyDate {
int year;
int month;
int day;
};
class Person {
string name;
string address;
string phoneNumber;
string email;
public:
virtual string toString() { //这个函数要被重载的,必须定义成virtual
return "Person";
}
};
class Student : public Person {
enum class_status {
frssman, sophomore, junior, senior
};
class_status status;
public:
string toString() {
return "Student";
}
};
class Employee : public Person {
string office;
int salary;
MyDate dateHired;
public:
virtual string toString() { //这个函数要被重载的,必须定义成virtual
return "Employee";
}
};
class Faculty : public Employee {
string officeHours;
int rank;
public:
string toString() {
return "Faculty";
}
};
class Staff : public Employee {
string title;
public:
string toString() {
return "Staff";
}
};
void f(Person &p) { //这个函数不能放在class中。
cout << p.toString() << endl;
}
int main() {
Person person;
Student student;
Employee employee;
Faculty faculty;
Staff staff;
f(person);
f(student);
f(employee);
f(faculty);
f(staff);
system("PAUSE");
return 0;
}
展开全部
//你的算法不太对,可以借助数组来实现
#include<iostream.h>
#include<math.h>
void main()
{
int newx=0,k,p,j,i=0;
unsigned long x,a;
int b[80];
cout<<"x=";
cin>>x;
cout<<"k=";
cin>>k;
a=x;p=k;
while(x>9)
{
j=i++;
b[j]=x%10;
x=(x-b[j])/10;
if(x<9)
b[i]=x;
//cout<<b[j];
}
while(k-1!=0)
{
b[k-1]=b[k-2];
k--;
}
if(p>i+1)
{
cout<<"newx is "<<a<<endl;
}
else
{
for(j=1;j<=i;j++)
{
newx=newx+b[j]*pow(10,j-1);
}
cout<<"newx is "<<newx<<endl;
}
}
追问
不知道请不要乱回答好吗(´・_・`)
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询