c++实验题(类)

CPU类

CPU类(类的设计)
**【问题描述】**声明一个CPU类,包含等级(rank)、频率(frequency)、电压(voltage)等属性,有两个公有成员函数run、stop。其中,rank为枚举类型CPU_Rank,声明为enum CPU_Rank{P1=1,P2,P3,P4,P5,P6,P7};frequency为单位是MHz的整型数,vlotage为浮点型的电压值。
(1)分别编写不同的set函数和get函数修改、获取私有数据成员。
(2)在构造函数、析构函数、run函数、stop函数中分别输出对应的提示语句。
(3)在主函数中调用run和stop函数,并观察构造函数和析构函数的调用顺序。
【输入形式】 无输入
【输出形式】 构造函数里的输出语句、run成员函数里的输出语句、stop成员函数里的输出语句、析构函数里的输出语句
【样例输出】
Construct CPU!
CPU Run!
CPU Stop!
Destruct CPU!

#include<iostream>
using namespace std;
class CPU
{
public:
    CPU()
    {
        cout<<"Construct CPU!"<<endl;
    }
    void run()
    {
        cout<<"CPU Run!"<<endl;
    }
    void stop()
    {
        cout<<"CPU Stop!"<<endl;
    }
   ~CPU()
   {
       cout<<"Destruct CPU!";
   }
private:
    enum CPU_Rank{P1=1,P2,P3,P4,P5,P6,P7};
    int frequency;
    float vlotage;
};
int main()
{
    CPU qy;
    qy.run();
    qy.stop();
    return 0;

}

椭圆类

【问题描述】
设计并测试一个名为Ellipse的椭圆类:
(1)其私有数据成员为外切矩形的左上角与右下角两个点的坐标(4个int型x1,y1,x2,y2)
(2)声明4个公有的成员函数分别访问椭圆的外切矩形的顶点坐标
(3)设计1个构造函数Ellipse(int,int,int,int)对椭圆的外切矩形的顶点坐标赋值
(4)设计1个公有成员函数Area()计算椭圆的面积。
【输入形式】 在主函数里输入顶点坐标,并声明一个Ellipse类的对象。
【输出形式】 在主函数里调用该对象的成员函数输出外切矩形的顶点坐标,计算并输出椭圆的面积。
【样例输入】
-3 1 3 -1
【样例输出】
-3 1 3 -1
9.4245

#include<bits/stdc++.h>
using namespace std;
class Ellipse{
private:
    int x1,y1,x2,y2;
public:
    Ellipse(int a,int b,int c,int d){
        x1=a;
        y1=b;
        x2=c;
        y2=d;
    }
    int Getx1(){
        return x1;
    }
    int Getx2(){
        return x2;
    }
    int Gety1(){
        return y1;
    }
    int Gety2(){
        return y2;
    }
    double Area(){
        return 3.1415*(y2-y1)*(x1-x2)/4;
    }
};
int main(){
    int x1,y1,x2,y2;
    cin>>x1>>y1>>x2>>y2;
    Ellipse a(x1,y1,x2,y2);
    cout<<a.Getx1()<<" "<<a.Gety1()<<" "<<a.Getx2()<<" "<<a.Gety2()<<endl;
    cout<<a.Area();
    return 0;
}

Date类

【问题描述】 定义一个满足如下要求的Date类
(1)用下面的格式输出日期
日/月/年
(2)可运行在日期上加一天的操作
(3)设置日期
【输入形式】 输入原始日期以及新的日期
【输出形式】 输出原始日期加一天后的日期,以及设置新日期后的日期
【样例输入】
2018 11 30
2020 2 9
【样例输出】
after add: 1/12/2018
new date: 9/2/2020
【提示】 冒号后面有一个空格

#include<bits/stdc++.h>
using namespace std;
class Date{
private:
     int year,month,day;
public:

    void setDate(int a,int b,int c){
        year=a;
        month=b;
        day=c;
    }
    void getDate(){
        cout<<day<<"/"<<month<<"/"<<year<<endl;
    }
    void add(){
        int daymax;
    if(month==1||month==3||month==5||month==8||month==10||month==12)
        daymax=31;
    if(month==4||month==6||month==9||month==11)
        daymax=30;
    if(month==2)
    {
        if((year%4==0&&year%100!=0)||(year%400==0))
            daymax=29;
        else
            daymax=28;
    }
    day++;
    if(day>daymax)
    {
        day=1;
        month++;
    }
    if(month>12)
    {
        month=1;
        year++;
    }

    }
};
int main(){
    Date a,b;
    int year1,month1,day1,year2,month2,day2;
    cin>>year1>>month1>>day1;
    cin>>year2>>month2>>day2;
    a.setDate(year1,month1,day1);
    b.setDate(year2,month2,day2);
    a.add();
    cout<<"after add: ";
    a.getDate();
    cout<<"new date: ";
    b.getDate();
}

椭圆类2(拷贝构造函数和析构函数)

【问题描述】
(1)拷贝(复制)构造函数的实现。在已经完成的“椭圆类——1”的基础上,增加一个拷贝构造函数。函数原型格式:Ellipse(const Ellipse & e);
(2)增加Show()函数,显示椭圆的外切矩形的顶点坐标。
(3)增加一个成员函数Fun(int y),将椭圆外切矩形的左上角和右下角的纵坐标分别加y和减y。
(4)增加析构函数,在析构函数中输出“xigou”以及待析构对象的外切矩形左上角的横纵坐标。
【输入形式】 在主函数中输入顶点坐标后创建一个对象,并用该对象作为初始值再创建一个新对象。输入一个值,用于修改新对象的外切矩形的纵坐标。
【输出形式】 在主函数里调用这2个对象的Show函数分别输出外切矩形的顶点坐标,调用Area函数分别计算并输出椭圆的面积。接收修改值y后,调用Fun函数修改新对象的纵坐标。重新计算新对象的面积并输出。
【样例输入】
-3 1 3 -1
1
【样例输出】
-3 1 3 -1
-3 1 3 -1
9.4245
9.4245
18.849
xigou -3 2
xigou -3 1

#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
const double PI = 3.1415;
class Ellipse
{
public:
    Ellipse(int xx1, int yy1, int xx2, int yy2)
    {
        x1 = xx1;
        y1 = yy1;
        x2 = xx2;
        y2 = yy2;
    }
    Ellipse()
    {
        x1 = 0;
        y1 = 0;
        x2 = 0;
        y2 = 0;
    }
    Ellipse(const Ellipse & e)
    {
        x1 = e.x1;
        x2 = e.x2;
        y1 = e.y1;
        y2 = e.y2;
    }
    ~Ellipse()
    {
        cout<<"xigou "<<x1<<" "<<y1<<endl;
    }
    void Show()
    {
        cout<<x1<<" "<<y1<<" "<<x2<<" "<<y2<<endl;
    }

    double Area()
    {
        return fabs(PI*x1*y1);
    }

    void Fun(int y)
    {
        y1 += y;
        y2 -= y;
    }
private:
    int x1;
    int y1;
    int x2;
    int y2;
};

int main()
{
    int x1, y1, x2, y2, y;
    cin>>x1>>y1>>x2>>y2;
    cin>>y;
    Ellipse tuoyuan(x1, y1, x2, y2);
    Ellipse tuoyuan2(tuoyuan);
    tuoyuan.Show();
    tuoyuan2.Show();
    cout<<tuoyuan.Area()<<endl;
    tuoyuan2.Fun(y);
    cout<<tuoyuan.Area()<<endl;
    cout<<tuoyuan2.Area()<<endl;
}

时间类

【问题描述】
设计一个Time类,并设计多个重载的构造函数,可以设置时间、进行时间的加减运算、按12小时格式和24小时格式输出时间。
例如:
class Time
{
int hour,minute,second;
public:
int SecCalc(){return(hour*60+minute)*60+second; }
Time(int h,int m, int s=0);
Time(int s=0);
void SetTime(int h=0,int m=0, int s=0);
void print_12();
void print_24();
Time Add(Time &);
Time Sub(Time &);
};
其中构造函数Time::Time(int s)根据总秒数计算hour、minute、second并构造对象;
成员函数Time::print_12()按12小时格式显示时间,如“09:20:45 AM”、“03:15:20 PM”;
成员函数Time::print_24()按24小时格式显示时间,如“09:20:45”、“4:30:20”;
成员函数Time Time::Add(Time&)用来实现两个时间对象的值进行相加;
成员函数Time Time::Sub(Time&)用来实现两个时间对象的值进行相减。
为了检验Time类的功能,主函数按如下方式设计:
int main()
{
Time t1(2,34),t2,t3(3723);
t2.SetTime(13,23,34);
cout<<“t2:”;
t2.print_12();
cout<<endl<<“t2:”;
t2.print_24();
cout<<"\nt1+t2:";
t1.Add(t2).print_24();
cout<<"\nt1-t2:";
t2.Sub(t1).print_24();
cout<<endl<<“t3:”;
t3.print_24();
return 0;
}
【输入形式】 无输入
【输出形式】 程序运行结果
【样例输入】
【样例输出】
t2:01:23:34 PM
t2:13:23:34
t1+t2:15:57:34
t1-t2:10:49:34
t3:01:02:03

#include <iostream>
#include <iomanip>
#include <cstdio>
using namespace std;
class Time
{
private:
    int hour,minute,second;
public:
    int SecCalc(){return(hour*60+minute)*60+second;  }
    Time(int h,int m, int s=0);
    Time(int s=0);
    void SetTime(int h=0,int m=0, int s=0);
    void print_12();
    void print_24();
    Time Add(Time &T);
    Time Sub(Time &T);
};
Time::Time(int h,int m, int s)
{
    hour = h;
    minute = m;
    second = s;
}
Time::Time(int s)
{
    int m, h;
    m = s/60;
    s = s%60;
    h = m/60;
    m = m%60;
    hour = h;
    minute = m;
    second = s;
}
void Time::SetTime(int h,int m, int s)
{
    hour = h;
    minute = m;
    second = s;
}
void Time::print_12()
{
    if(hour>12)
    {
        cout.fill('0');
        cout.width(2);
        cout<<hour-12<<":";
        cout.fill('0');
        cout.width(2);
        cout<<minute<<":";
        cout.fill('0');
        cout.width(2);
        cout<<second<<" PM";
    }
    else
    {
        cout.fill('0');
        cout.width(2);
        cout<<hour<<":";
        cout.fill('0');
        cout.width(2);
        cout<<minute<<":";
        cout.fill('0');
        cout.width(2);
        cout<<second<<" AM";
    }
}
void Time::print_24()
{
        cout.fill('0');
        cout.width(2);
        cout<<hour<<":";
        cout.fill('0');
        cout.width(2);
        cout<<minute<<":";
        cout.fill('0');
        cout.width(2);
        cout<<second;
}
Time Time::Add(Time &T)
{
    Time a;
    a.second = second;
    a.minute = minute;
    a.hour = hour;
    a.second += T.second;
    a.minute += T.minute;
    a.hour += T.hour;
    if(a.second >= 60)
    {
        a.minute++;
        a.second -= 60;
    }
    if(a.minute >= 60)
    {
        a.hour++;
        a.second -= 60;
    }
    if(a.hour >= 24)
        a.hour -= 24;
    return a;
}
Time Time::Sub(Time &T)
{
    Time a;
    a.second = second;
    a.minute = minute;
    a.hour = hour;
    a.second -= T.second;
    a.minute -= T.minute;
    a.hour -= T.hour;
    if(a.second<0)
    {
        a.minute--;
        a.second += 60;
    }
    if(a.minute<0)
    {
        a.hour--;
        a.minute += 60;
    }
    if(a.hour<0)
        a.hour += 24;
    return a;
}
int main()
{
    Time t1(2,34),t2,t3(3723);
    t2.SetTime(13,23,34);
    cout<<"t2:";
    t2.print_12();
    cout<<endl<<"t2:";
    t2.print_24();
    cout<<"\nt1+t2:";
    t1.Add(t2).print_24();
    cout<<"\nt1-t2:";
    t2.Sub(t1).print_24();
    cout<<endl<<"t3:";
    t3.print_24();
    return 0;
}

Person类、学生类、教师类和研究生类(多重继承)

【问题描述】 研究生Graduate既有学生Student的属性,又有教师Teacher的属性,通过多重继承说明一个研究生类Graduate。

从前面实验题目完成的Person类派生出Teacher类,新增专业(dept)和月薪(salary)属性,并定义Teacher类的构造函数初始化新增的数据成员,showMe成员函数显示新增的数据成员的值。

再从Person类派生出Student类,新增班级(class)和学号(ID)属性,定义Student类的构造函数初始化新增的数据成员,showMe成员函数显示新增的数据成员的值。

通过“多重继承”说明一个研究生类Graduate,定义Graduate类的构造函数,定义Graduate类的showMe成员函数显示各个数据成员的值。

主函数设计如下,请勿修改:
int main(){
Graduate stu1(“Lisi”,22,‘m’,“College of Informatics”,2000,“2015013”,“S101”);
stu1.showMe();
return 0;
}
提示:(1)Graduate的父类是Student和Teacher,而Student和Teacher有共同的父类Person,如果是普通继承则姓名、性别和年龄就发生了重复。因此,这一题需要用到虚基类的概念。
(2)注意showMe()成员函数在每个类里都有。
【样例输出】
class: S101
id: 2015013
name: Lisi
sex: m
age: 22
co: College of Informatics
salary: 2000

//有警告
#include<iostream>
#include<string.h>
using namespace std;
class Person
{
protected:
    char name[10];
    int age;
    char sex;
 public:
    Person(){}
    Person(char *s,int n,char c)
    {
    strcpy(name,s);
    age=n; sex=(c=='m'?0:1);
    }
    char *getName() { return name; }
    char getSex() { return sex==0?'m':'f'; }
    int getAge() { return age; }
    void showMe() {
    cout<<"name: "<<getName()<<endl<<"sex: "<<getSex()<<endl<<"age: "<<getAge()<<endl;
    }
};
class Teacher:virtual public Person
{
protected:
    char dept[30];
    double salary;
public:
    Teacher(char *d,double s)
    {
       // Register(n,a,c);
        strcpy(dept,d);
        salary=s;
    }
    void showMe()
    {
        cout<<"co: "<<dept<<endl<<"salary: "<<salary<<endl;
    }
};
class Student:virtual public Person
{
protected:
    char id[10],Class[10];
public:
    Student(char *i,char *c)
    {
        strcpy(id,i);
        strcpy(Class,c);
    }
    void showMe()
    {
        cout<<"class: "<<Class<<endl;
        cout<<"id: "<<id<<endl;
    }
};
class Graduate:public Teacher,public Student
{
public:
    Graduate(char *na,int ag,char se,char *de,int salary,char *i,char *c):Person(na,ag,se),Teacher(de,salary),Student(i,c)
    {

    }
    void showMe()
    {
        Student::showMe();
        Person::showMe();
        Teacher::showMe();
    }
};
int main(){

  Graduate stu1("Lisi",22,'m',"College of Informatics",2000,"2015013","S101");

  stu1.showMe();

  return 0;

}

车类的继承与派生(虚基类)

【问题描述】 声明一个车(vehicle)基类,具有MaxSpeed,Weight等成员变量,Run,Stop等成员函数,其中Run函数输出“Run”,Stop函数输出“Stop”。
并由此派生出自行车(bicycle)类,汽车(motorcar)类。自行车(bicycle)类有高度(Height)等属性,汽车(motorcar)类有座位数(SeatNum)等属性。
再从bicycle类和motorcar类派生出摩托车(motocycle)类。
在继承过程中,注意把vehicle设置为虚基类。如果不把vehicle设置为虚基类,会有什么问题?编程试试看。主函数如下,请勿改动:
int main(){
motocycle a;
a.Run();
a.Stop();
return 0;
}
调试过程中,你是否看到了error: request for member ‘Run’ is ambiguous。请问这句话怎么翻译?
【样例输出】
Run
Stop

#include<iostream>
using namespace std;
class vehicle
{
    double MaxSpeed,Weight;
public:
    void Run(){cout<<"Run"<<endl;}
    void Stop(){cout<<"Stop"<<endl;}
};
class bicycle:virtual public vehicle
{
    double Height;
};
class motorcar:virtual public vehicle
{
    double SeatNum;
};
class motocycle:public motorcar,public bicycle
{

};
int main()
{
  motocycle a;
  a.Run();
  a.Stop();
  return 0;
}

  • 0
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值