面向对象上机题-成绩查询

//面向对象上机 作业一
//Date:2005/10/27
//Make By:张建波
#include "iostream.h"
#include "string.h"

void main()
{
char *name[]={"Smith","John","Mary","Havard","kai"};
int score[]={80,72,89,66,96};
char fname[10]; //待查询的姓名
do
{
cout<<"/n请输入要查询的学生姓名:";
cin>>fname;
int ID=-1;
for(int i=0;i<5;i++)
if(fname[0]==name[i][0]) //首字母匹配,继续匹配
{int count=0;
int L=strlen(name[i]);
for(int j=0;j<L;j++)
if(fname[j]==name[i][j])count++;
if(count==L){ID=i;break;} //找到学生数据,跳出循环
}
if(ID==-1 && fname[0]!='#')cout<<"/n警告:没有该学生的数据!"<<endl;
else
if(ID>=0)cout<<name[ID]<<"的成绩是:"<<score[ID]<<endl;
}
while(fname[0]!='#');
}

### 面向对象编程上机实验题目示例 以下是一些面向对象编程相关的上机实验题目,涵盖了类和对象、继承与派生、模板类设计等核心内容。 #### 1. 类和对象的使用 编写一个程序实现以下功能:定义一个名为`Student`的类,包含学生的姓名、学号和成绩。要求实现输入学生信息、计算平均分以及输出学生信息的功能[^3]。 ```cpp #include <iostream> using namespace std; class Student { private: string name; int id; double score; public: void setInfo(string n, int i, double s) { name = n; id = i; score = s; } double getScore() { return score; } void display() { cout << "Name: " << name << ", ID: " << id << ", Score: " << score << endl; } }; int main() { Student stu1, stu2; stu1.setInfo("Alice", 101, 85.5); stu2.setInfo("Bob", 102, 90.0); stu1.display(); stu2.display(); double avg = (stu1.getScore() + stu2.getScore()) / 2; cout << "Average Score: " << avg << endl; return 0; } ``` #### 2. 继承与派生 设计一个基类`Shape`,派生出两个子类`Circle`和`Rectangle`。要求实现计算面积和周长的功能,并测试不同形状的实例[^4]。 ```cpp #include <iostream> using namespace std; class Shape { public: virtual double getArea() = 0; virtual double getPerimeter() = 0; }; class Circle : public Shape { private: double radius; public: Circle(double r) : radius(r) {} double getArea() override { return 3.1415 * radius * radius; } double getPerimeter() override { return 2 * 3.1415 * radius; } }; class Rectangle : public Shape { private: double length, width; public: Rectangle(double l, double w) : length(l), width(w) {} double getArea() override { return length * width; } double getPerimeter() override { return 2 * (length + width); } }; int main() { Circle c(5.0); Rectangle r(4.0, 6.0); cout << "Circle - Area: " << c.getArea() << ", Perimeter: " << c.getPerimeter() << endl; cout << "Rectangle - Area: " << r.getArea() << ", Perimeter: " << r.getPerimeter() << endl; return 0; } ``` #### 3. 模板类设计 根据给定的`main()`函数,定义一个模板类`Stack`,支持不同类型的数据存储和操作。要求实现压栈和弹栈功能,并测试整型和浮点型数据的操作[^2]。 ```cpp #include <iostream> using namespace std; template <typename T, int size> class Stack { private: T data[size]; int top; public: Stack() : top(-1) {} bool Push(T value) { if (top >= size - 1) return false; data[++top] = value; return true; } bool Pop(T &value) { if (top < 0) return false; value = data[top--]; return true; } }; int main() { Stack<int, 100> stk1; Stack<double, 100> stk2; int n, m; cin >> n >> m; for (int i = 0; i < n; ++i) { int temp; cin >> temp; stk1.Push(temp); } for (int i = 0; i < m; ++i) { double temp; cin >> temp; stk2.Push(temp); } int a; if (stk1.Pop(a)) cout << a << endl; if (stk1.Pop(a)) cout << a << endl; double x; if (stk2.Pop(x)) cout << x << endl; if (stk2.Pop(x)) cout << x << endl; return 0; } ``` #### 4. 函数重载 编写一个程序,实现对不同类型数组的最大值查找功能。要求通过函数重载实现对整型、浮点型和长整型数组的支持[^5]。 ```cpp #include <iostream> using namespace std; int max(int a[], int size) { int m = a[0]; for (int i = 1; i < size; ++i) { if (a[i] > m) m = a[i]; } return m; } float max(float a[], int size) { float m = a[0]; for (int i = 1; i < size; ++i) { if (a[i] > m) m = a[i]; } return m; } long max(long a[], int size) { long m = a[0]; for (int i = 1; i < size; ++i) { if (a[i] > m) m = a[i]; } return m; } int main() { int x[5] = {11, 22, 666, 44, 55}; float y[5] = {11.11, 22.22, 33.33, 888.88, 55.55}; long z[5] = {1234567, 222222, 333333, 444444, 555555}; cout << max(x, 5) << endl; cout << max(y, 5) << endl; cout << max(z, 5) << endl; return 0; } ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值