怎麽用C语言编程第4道题?

2024-11-28 04:44:11
推荐回答(1个)
回答(1):

编程题
5.1
#include

#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<cout<}

5.2
#include
#include

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<<"姓名:"< cout<<"年龄:"< }
};

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<<"姓名:"< cout<<"年龄:"< cout<<"学号:"< }
};

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<<"姓名:"< cout<<"年龄:"< cout<<"职称:"< }
};

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

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

Teacher t = Teacher("副教授", "王五", 39);
t.display();
}