文章目录
- 第一次上机
- 第二次上机
- 第三次上机
- 第四次上机
- 1.编写一个学生和教师数据输入和显示程序。学生数据有编号、姓名、班号和成绩,教师数据有编号、姓名、职称和部门。要求将编号、姓名输入和显示设计成一个类Person,并作为学生类Student和教师类Teacher的基类。
- 2.分别定义Teacher(教师)类和Cadre(干部)类,采用多继承方式由这两个类派生出新类Teacher_Cadre(教师兼干部)。要求:
- 3.写一个程序,定义抽象基类Shape,由它派生出5个派生类:Circle,Square,Rectangle,Trapezoid,Triangle。用虚函数分别计算几种图形面积,并求它们的和。要求使用基类指针数组,使它的每一个元素指向一个派生类对象
第一次上机
一、求两个或三个正整数中的最大数,用带有默认参数的函数实现
find_max函数中带有默认参数c,实现了可以比较两个或者三个正整数的大小。
#include <iostream>
using namespace std;
int find_max(int a, int b, int c = 0) {
int max;
max = a > b ? a : b;
max = max > c ? max : c;
return max;
}
int main() {
int a, b, c;
cin >> a >> b;
cout << find_max(a, b) << endl; //比较两数大小
cin >> a >> b >> c;
cout << find_max(a, b, c) << endl; //比较三数大小
return 0;
}
二、输入三个整数将它们从小到大输出要求使用变量的引用
int &aa = a;等语句使用了变量的引用。
#include <iostream>
using namespace std;
void sort(int &a, int &b, int &c) {
int step;
if (a > b) {
step = a;
a = b;
b = step;
}
if (a > c) {
step = a;
a = c;
c = step;
}
if (b > c) {
step = b;
b = c;
c = step;
}
}
int main() {
int a, b, c;
cin >> a >> b >> c;
int &aa = a;
int &bb = b;
int &cc = c;
sort(aa, bb, cc);
cout << a << " " << b << " " << c;
}
三、编写一个程序,用同一函数名对几个数据进行从小到大排序数据类型可以是整型、浮点型,用重载函数实现
两个同名的函数sort,一个排序整型,一个排序浮点型,通过函数重载实现。
#include <iostream>
using namespace std;
void sort(int *a, int *b, int *c) {
int step;
if (*a > *b) {
step = *a;
*a = * b;
*b = step;
}
if (*a > *c) {
step = *a;
*a = *c;
*c = step;
}
if (*b > *c) {
step = *b;
*b = *c;
*c = step;
}
}
void sort(float *a, float *b, float *c) {
float step;
if (*a > *b) {
step = *a;
*a = * b;
*b = step;
}
if (*a > *c) {
step = *a;
*a = *c;
*c = step;
}
if (*b > *c) {
step = *b;
*b = *c;
*c = step;
}
}
int main() {
int x1, y1, z1;
float x2, y2, z2;
cin >> x1 >> y1 >> z1;
sort(&x1, &y1, &z1);
cout << x1 << " " << y1 << " " << z1 << endl;
cin >> x2 >> y2 >> z2;
sort(&x2, &y2, &z2);
cout << x2 << " " << y2 << " " << z2 << endl;
}
四、对第三题改用函数模板实现,并与第三题对比分析
template给函数模板提供了参数类型,使得函数可以处理多种类型的数据。
#include <iostream>
using namespace std;
template<class T>
void sort(T &a, T &b, T &c) {
T step;
if (a > b) {
step = a;
a = b;
b = step;
}
if (a > c) {
step = a;
a = c;
c = step;
}
if (b > c) {
step = b;
b = c;
c = step;
}
}
int main() {
int x1, y1, z1;
int &xx1 = x1, &yy1 = y1, &zz1 = z1;
float x2, y2, z2;
float &xx2 = x2, &yy2 = y2, &zz2 = z2;
cin >> x1 >> y1 >> z1;
sort(xx1, yy1, zz1);
cout << x1 << " " << y1 << " " << z1 << endl;
cin >> x2 >> y2 >> z2;
sort(xx2, yy2, zz2);
cout << x2 << " " << y2 << " " << z2 << endl;
}
运行结果同上
五、设计一个日期Date类,它能实现年月日的输入和输出,要求分别将成员函数放在类体内和类体外
set-date函数在类体内,show_date函数在类体外实现
#include <iostream>
using namespace std;
class Date {
private:
int year;
int month;
int day;
public:
void set_date(int year, int month, int day) {
this->year = year;
this->month = month;
this->day = day;
}
void show_date();
};
void Date::show_date() {
cout << year << "年" << month << "月" << day << "日" << endl;
}
int main() {
Date d;
int a, b, c;
cin >> a >> b >> c;
d.set_date(a, b, c);
d.show_date();
}
六、声明一个circle类,有数据成员半径Radius成员函数GetArea()计算面积,构造一个circle对象进行测试
#include <iostream>
#define pi 3.1415926
using namespace std;
class circle {
double Radius;
public:
void GetRadius(double Radius) {
this->Radius = Radius;
}
double GetArea() {
return pi * Radius * Radius;
}
};
int main() {
double r;
cin >> r;
circle c;
c.GetRadius(r);
cout << c.GetArea() << endl;
}
七、编写一个基于对象的编程,求出三个长方柱的体积。
数据成员包括长length,宽width,高height
1.由键盘输入三个长方柱的长宽高
2.计算体积
3.输出三个长方柱的体积
#include <iostream>
using namespace std;
class cuboid {
private:
int length, width, height;
public:
void get_data(int length, int width, int height) {
this->length = length;
this->width = width;
this->height = height;
}
int get_volume() {
return length * width * height;
}
};
int main() {
cuboid a, b, c;
int l1, w1, h1;
int l2, w2, h2;
int l3, w3, h3;
cin >> l1 >> w1 >> h1;
cin >> l2 >> w2 >> h2;
cin >> l3 >> w3 >> h3;
a.get_data(l1, w1, h1);
b.get_data(l2, w2, h2);
c.get_data(l3, w3, h3);
cout << "长方体a的体积为" << a.get_volume() << endl;
cout << "长方体b的体积为" << b.get_volume() << endl;
cout << "长方体c的体积为" << c.get_volume() << endl;
}
第二次上机
一、编写一个People类
#include <iostream>
using namespace std;
class People {
private:
int age, height, weight;
static int num;
public:
//构造函数
People(int a, int h, int w) {
age = a;
height = h;
weight = w;
num++;
}
void Eating() {
weight++;
}
void Sporting() {
height++;
}
void Sleeping() {
age++;
weight++;
height++;
}
void Show() {
cout << "年龄:" << age << "岁" << endl;
cout << "身高:" << height << "斤" << endl;
cout << "体重:" << weight << "厘米" << endl;
}
static void ShowNum() {
cout << "人数:" << num << endl;
}
};
int People::num = 0;//静态成员初始化要放在类外申请空间
主函数:构造people类xiaoming和xiaohong,调用成员函数
int main() {
People xiaoming = People(12, 150, 95);
xiaoming.Eating();
xiaoming.Sporting();
xiaoming.Sleeping();
cout << "xiaoming:" << endl;
xiaoming.Show();
People xiaohong = People(10, 140, 80);
cout << "xiaohong:" << endl;
xiaohong.Show();
xiaoming.ShowNum();
}
二、构造学生类
#include <iostream>
#include <cString>
using namespace std;
class Student {
private:
char name[18];
int num, mathScore, englishScore;
static int count, mathTotleScore, englishTotleScore;
public:
//构造函数
Student(char nm[], int nu, int math, int english ) {
strcpy(name, nm);
num = nu;
mathScore = math;
englishScore = english;
count++;
mathTotleScore += math;
englishTotleScore += english;
}
void showBace() {
cout << name << endl;
cout << "学号:" << num << endl;
cout << "数学成绩" << mathScore << endl;
cout << "英语成绩" << englishScore << endl;
}
static void showStatic() {
cout << "总人数" << count << endl;
cout << "数学总成绩" << mathTotleScore << endl;
cout << "英语总成绩" << englishTotleScore << endl;
}
};
//初始化静态成员
int Student::count = 0;
int Student::mathTotleScore = 0;
int Student::englishTotleScore = 0;
主函数构造成员xiaoming、xiaohong、xiaogang:并调用成员函数
int main() {
Student xiaoming = Student("小明", 190301, 85, 90);
Student xiaohong = Student("小红", 190302, 90, 80);
Student xiaogang = Student("小刚", 190303, 100, 60);
xiaoming.showBace();
xiaohong.showBace();
xiaogang.showBace();
xiaoming.showStatic();
}
三、构造Dog类,并使用函数指针和字符指针
#include <iostream>
using namespace std;
class Dog {
private:
char *name, *sex;
int age, weight;
public:
Dog(char *nm, char *s, int a, int w) {
name = nm;
sex = s;
age = a;
weight = w;
}
void show() {
cout << name << endl;
cout << "性别:" << sex << endl;
cout << "年龄:" << age << "岁" << endl;
cout << "体重:" << weight << "斤" << endl;
}
};
主函数:
int main() {
char *name = "养乐多";
char *sex = "公";
//类指针构造函数调用
Dog *yangleduo = new Dog(name, sex, 3, 5);
yangleduo->show();
}
四、管理个人活期账户
account类设计:
#include <iostream>
#include <string>
using namespace std;
class account {
private:
string name;
int id, password;
double balance, rate;
public:
account(string nm, int i, int pw) {
name = nm;
id = i;
password = pw;
balance = 0;
rate = 0.03;
}
void push() {
cout << "存入:"<<endl;
double a;
cin>>a;
cout << "存入账户" << a << "元" << endl;
balance += a;
cout << "余额" << balance << "元" << endl;
}
void pop() {
cout << "取出:"<<endl;
double a;
cin>>a;
if (a > balance) {
cout << "取出" << a << "元失败" << endl;
cout << "余额不足" << endl;
} else {
cout << "取出" << a << "元成功" << endl;
balance -= a;
}
cout << "余额" << balance << "元" << endl;
}
double interest() {
return balance * rate;
}
void show() {
cout << "账户名" << name << endl;
cout << "账号" << id << endl;
cout << "余额" << balance << "元" << endl;
cout << "年利息" << interest() << "元" << endl;
}
};
主函数设计:初始化账户小明
int main() {
account xiaoming = account("小明", 190301, 123456);
cout<<"0:打印账户信息"<<endl;
cout<<"1:存入账户"<<endl;
cout<<"2:取出"<<endl;
cout<<"其他:退出系统"<<endl;
while(1){
int a;
cin>>a;
if(a==0)xiaoming.show();
else if(a==1)xiaoming.push();
else if(a==2)xiaoming.pop();
else{
cout<<"退出"<<endl;
break;
}
}
}
第三次上机
1.建立一个对象数组,内放5个学生的数据(学号、成绩)
#include <iostream>
using namespace std;
class student {
public:
int id;
int grades;
void set(int mi, int mg) {
id = mi;
grades = mg;
}
void show() {
cout << "学号:" << id << "成绩:" << grades << endl;
}
};
//(2)设立函数max,使用对象指针做参数,在max函数中找到5个学生中的成绩最高者
int max(student *s, int n) {
int a = 0, b;
for (int i = 0; i < n; i++) {
if (s->grades > a) {
a = s->grades;
b = s->id;
}
s++;
}
return b;
}
int main() {
student s[5];
student *p = s;
cout << "依次输入学号成绩" << endl;
for (int i = 0; i < 5; i++) {
int a, b;
cin >> a >> b;
s[i].set(a, b);
}
//(1)用指针指向数组首元素,输出第一三五个学生的学号、成绩
for (int i = 0; i < 3; i++) {
p->show();
p++;
p++;
}
p = s;
cout << "成绩最高学生学号是:" << max(p, 5);
}
2.定义一个类Complex,重载运算符 + - * /,重载函数作为类成员函数
#include <iostream>
using namespace std;
class Complex {
private:
int rea;//实部
int vir;//虚部
public:
Complex() {
}
void set(int r, int v) {
rea = r;
vir = v;
}
//以复数格式输出
void show() {
if (rea != 0 && vir != 0)
cout << rea << "+" << vir << "i" << endl;
else if (rea != 0)
cout << rea << endl;
else if (vir != 0)
cout << vir << "i" << endl;
else
cout << 0 << endl;
}
//分别定义四个重载函数
Complex operator+(const Complex &c)const {
Complex temp;
temp.rea = c.rea + rea;
temp.vir = c.vir + vir;
return temp;
}
Complex operator-(const Complex &c)const {
Complex temp;
temp.rea = c.rea - rea;
temp.vir = c.vir - vir;
return temp;
}
Complex operator*(const Complex &c)const {
Complex temp;
temp.rea = c.rea * rea + c.vir * vir;
temp.vir = c.vir * rea + c.rea * vir;
return temp;
}
Complex operator/(const Complex &c)const {
Complex temp;
int a = -c.vir;
temp.rea = c.rea * rea + a * vir;
temp.vir = a * rea + c.rea * vir;
return temp;
}
};
int main() {
Complex a, b, c;
a.set(2, 2);
b.set(2, -2);
cout << "和为";
(a + b).show();
cout << "差为";
(a - b).show();
cout << "积为";
(a * b).show();
cout << "商为";
(a / b).show();
}
3.对于两行三列矩阵,重载流运算符使之能用于矩阵输入和输出
#include <iostream>
using namespace std;
//定义matrix类,类内包含两行三列数组
//流重载函数作为该类的友元函数
class matrix {
public:
int a[2][3];
friend istream &operator>>(istream &, matrix &);
friend ostream &operator<<(ostream &, const matrix &);
};
//重载流插入运算符
istream &operator>>(istream &in, matrix &m) {
for (int i = 0; i < 2; i++)
for (int j = 0; j < 3; j++)
in >> m.a[i][j];
return in;
}
//重载流输出运算符
ostream &operator<<(ostream &out, const matrix &m) {
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++)
out << m.a[i][j] << " ";
out << endl;
}
return out;
}
int main() {
matrix m;
cout<<"输入:"<<endl;
cin >> m;
cout<<"输出:"<<endl;
cout << m;
}
4.定义time类和date类,time类为date类的友元类,time类的display函数可以访问date类的私有数据
#include <iostream>
using namespace std;
class date {
private:
int year;
int mon;
int day;
public:
//time类定义为date类的友元类
friend class time;
date(int a, int b, int c) {
year = a;
mon = b;
day = c;
}
};
class time {
private:
int hour;
int min;
int sec;
public:
time(int a, int b, int c) {
hour = a;
min = b;
sec = c;
}
//time类中访问date类的私有函数
void display(const date &d) {
cout << d.year << "年";
cout << d.mon << "月";
cout << d.day << "日";
cout << hour << "时";
cout << min << "分";
cout << sec << "秒";
}
};
int main() {
//time类和date类的构造使用时间为代码编写时间
date d = date(2021, 11, 3);
time t = time(19, 19, 00);
t.display(d);
}
第四次上机
1.编写一个学生和教师数据输入和显示程序。学生数据有编号、姓名、班号和成绩,教师数据有编号、姓名、职称和部门。要求将编号、姓名输入和显示设计成一个类Person,并作为学生类Student和教师类Teacher的基类。
代码:
#include<iostream>
#include<string>
using namespace std;
//基类设计:
class Person{
protected:
string name;
string id;
public:
void getiofo(){
cin>>name>>id;
}
void print(){
cout<<"姓名:"<<name<<" 学号:"<<id;
}
};
//设计派生类并覆写函数:
class Teacher:public Person{
private:
string position,department;
public:
void getinfo(){
Person::getiofo();
cin>>position>>department;
}
void print(){
Person::print();
cout<<" 职务:"<<position<<" 部门:"<<department<<endl;
}
};
class Student:public Person{
private:
string clas;
int grades;
public:
void getinfo(){
Person::getiofo();
cin>>clas>>grades;
}
void print(){
Person::print();
cout<<" 班级:"<<clas<<" 成绩:"<<grades<<endl;
}
};
int main(){
Student xiaoming;
Teacher xiaowang;
cout<<"输入学生小明的基本信息:"<<endl;
xiaoming.getinfo();
cout<<"输入老师小王的基本信息:"<<endl;
xiaowang.getinfo();
cout<<"输出学生小明的基本信息:"<<endl;
xiaoming.print();
cout<<"输出老师小王的基本信息:"<<endl;
xiaowang.print();
}
2.分别定义Teacher(教师)类和Cadre(干部)类,采用多继承方式由这两个类派生出新类Teacher_Cadre(教师兼干部)。要求:
(1)在两个基类中都包含姓名、年龄、性别、地址、电话等数据成员。
(2)在Teacher类中还包含数据成员titile(职称),在Cadre类中还包含数据成员post(职务),在Teacher_Cadre类中还包含数据成员wages(工资)。
(3)对两个基类中的姓名、年龄、性别、地址、电话等数据成员用相同的名字,在引用这些数据成员时,指定作用域。
(4)在类体中声明成员函数,在类外定义成员函数。
(5)在派生类Teacher_Cadre的成员函数show中调用Teacher类中的display函数,输出姓名、年龄、性别、职称、地址、电话,然后再用cout语句输出职务与工资。
代码:
#include<iostream>
#include<string>
using namespace std;
//Teacher类定义
class Teacher{
protected:
string name,age,sex,address,phone,title;
public:
void display();
Teacher(string nm,string ag,string s,string add,string ph,string tit);
};
//类外实现构造函数和display函数
Teacher::Teacher(string nm,string ag,string s,string add,string ph,string tit){
name=nm;age=ag;sex=s;address=add;phone=ph;title=tit;
}
void Teacher::display(){
cout<<"姓名:"<<name<<endl;
cout<<"年龄:"<<age<<endl;
cout<<"性别:"<<sex<<endl;
cout<<"地址:"<<address<<endl;
cout<<"电话:"<<phone<<endl;
cout<<"职称:"<<title<<endl;
}
//Cadre类定义:
class Cadre{
protected:
string name,age,sex,address,phone,post;
public:
void display();
Cadre(string nm,string ag,string s,string add,string ph,string tit);
};
//类外实现函数:
Cadre::Cadre(string nm,string ag,string s,string add,string ph,string po){
name=nm;age=ag;sex=s;address=add;phone=ph;post=po;
}
void Cadre::display(){
cout<<"姓名:"<<name<<endl;
cout<<"年龄:"<<age<<endl;
cout<<"性别:"<<sex<<endl;
cout<<"地址:"<<address<<endl;
cout<<"电话:"<<phone<<endl;
cout<<"职位:"<<post<<endl;
}
class Teacher_Cadre:public Teacher,Cadre{
private:
string wages;
public:
void display();
Teacher_Cadre(string nm,string ag,string s,string add,string ph,
string tit,string po,string wag);
};
//实现派生类的构造函数:
Teacher_Cadre::Teacher_Cadre(string nm,string ag,string s,string add,
string ph,string tit,string po,string wag):
Teacher(nm,ag,s,add,ph,tit),Cadre(nm,ag,s,add,ph,po){
wages=wag;
}
//实现派生类的display函数:
void Teacher_Cadre::display(){
Teacher::display();
cout<<"职位:"<<post<<endl;
cout<<"工资:"<<wages<<endl;
}
//测试主函数
int main(){
Teacher_Cadre xiaowang=
Teacher_Cadre("小王","50岁","男","西沣路226号","12345","教授","副校长","15000/月");
xiaowang.display();
}
3.写一个程序,定义抽象基类Shape,由它派生出5个派生类:Circle,Square,Rectangle,Trapezoid,Triangle。用虚函数分别计算几种图形面积,并求它们的和。要求使用基类指针数组,使它的每一个元素指向一个派生类对象
代码:
#include<iostream>
#define pi 3.1415926//圆周率
using namespace std;
//定义基类
class Shape{
protected:
double area;
public:
double getarea(){
return area;
}
Shape(double a){
area=a;
}
};
//定义五个派生类:
class Circle:public Shape{
private:
double redius;
public:
Circle(double r):Shape(pi*r*r){
redius=r;
}
};
class Square:public Shape{
private:
double length;
public:
Square(double a):Shape(a*a){
length=a;
}
};
class Rectangle:public Shape{
private:
double length,weigth;
public:
Rectangle(double a,double b):Shape(a*b){
length=a;
weigth=b;
}
};
class Trapezoid:public Shape{
private:
double height,high,low;
public:
Trapezoid(double a,double b,double h):Shape((a+b)*h/2){
height=h;
high=a;
low=b;
}
};
class Triangle:public Shape{
private:
double low,height;
public:
Triangle(double a,double h):Shape(a*h/2){
low=a;
height=h;
}
};
int main(){
//构造五类对象
Circle c=Circle(1);
Square s=Square(1);
Rectangle r=Rectangle(2,1);
Trapezoid t=Trapezoid(2,1,1);
Triangle tt=Triangle(1,1);
//定义基类数组并初始化
Shape *p[5]={&c,&s,&r,&t,&tt};
double sum;
//输出每个面积并计算总面积
for(int i=0;i<5;i++){
cout<<"面积"<<i+1<<": "<<p[i]->getarea()<<endl;
sum+=p[i]->getarea();
}
cout<<"总面积"<<sum;
}