这道编程题怎么写

 我来答
方思菱0Hx
2016-10-20 · TA获得超过2257个赞
知道大有可为答主
回答量:2182
采纳率:0%
帮助的人:586万
展开全部
编程题
5.1
#include <iostream>

#define PI 3.14159

using namespace std;

//重载函数
double getGirth(double length, double width)
{
return 2 * (length + width);
}

//重载函数
double getGirth(double radius)
{
return PI * radius * radius;
}

void main( )
{
cout<<getGirth(12, 10)<<endl;
cout<<getGirth(2)<<endl;
}

5.2
#include <iostream>
#include <string>

using namespace std;

class Person
{
protected:
string name;
int age;
public:
Person(){}
Person(string n, int a):name(n), age(a){};
~Person(){}
void display()
{
cout<<"姓名:"<<name<<endl;
cout<<"年龄:"<<age<<endl;
}
};

class Student : public Person
{
protected:
int sid;
public:
Student(int id, string n, int a)
{
name = n;
age =a;
sid = id;
}
~Student(){}
void display()
{
cout<<"姓名:"<<name<<endl;
cout<<"年龄:"<<age<<endl;
cout<<"学号:"<<sid<<endl;
}
};

class Teacher : public Person
{
protected:
string title;
public:
Teacher(string t, string n, int a)
{
name = n;
age =a;
title = t;
}
~Teacher(){}
void display()
{
cout<<"姓名:"<<name<<endl;
cout<<"年龄:"<<age<<endl;
cout<<"职称:"<<title<<endl;
}
};

void main( )
{
Person p = Person("张三其", 25);
p.display();

Student s = Student(1001, "李师煊", 19);
s.display();

Teacher t = Teacher("副教授", "王五", 39);
t.display();
}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式