算法和数据结构的编程题,用C语言实现。跪求 100
展开全部
编程题
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();
}
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();
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询