CPP2022-24-类与对象

 

 

 

程序填空题

#include <iostream>
using namespace std;
class CAT
{     public:
           CAT();
           CAT(const CAT&);
          ~CAT();
          int GetAge() const { return *itsAge; }
          void SetAge(int age){ *itsAge=age; }
      protected:
          int* itsAge;
};
CAT::CAT()
{    itsAge=new int;
     *itsAge =5;
}
CAT::CAT(const CAT& c)
{
itsAge = new int
; 
*itsAge= *(c.itsAge)
; 
}
CAT::~CAT()
{     delete itsAge;   }
#include <
iostream
>
using namespace std;

class Point
{
private:
//访问权限设置,私有权限
    int x;//横坐标
    int y;//纵坐标
public:
//访问权限设置,公有权限

    //以下为构造函数,用参数a,b分别为横纵坐标进行初始化
    
Point
(int a,int b)
    {
        
x=a
;
        
y=b
;
    }
    
    //以下为拷贝构造函数,借用对象a_point完成初始化
    Point(
Point &
a_point)
    {
        x=a_point.x;
        y=a_point.y;
    }
    
    //以下为析构函数
    
~Point()

    {
        cout<<"Deconstructed Point";
        print();
    }
    
    //以下为输出点的信息的函数,要求在一行中输出点的坐标信息,形如:(横坐标,纵坐标)
    void print()
    {
        cout<<
"("<
<<endl;
    }
};


int main()
{
    Point b_point(0,0);
    b_point.print();
    int a,b;
    
cin>>a>>b;
//从标准输入流中提取数值给a,b
    Point c_point(a,b);
    c_point.print();
  
return 0;
//主函数的返回语句
}
/*设输入为10 10,则本程序的运行结果为:
(0,0)

(10,10)

Deconstructed Point(10,10)

Deconstructed Point(0,0)

*/
#include <iostream>
using namespace std;
class Box
{
public:
    Box(int,int,int);
private:
    int length;
    int width;
    int height;
};
Box::Box(int len,int w,int h)

{
    length=len;
    width=w;
    height=h;
    cout<<"The Box is created. Parm:length="<<length<<", width="<<width<<", height="<<height<<"."<<endl;
}
int main( )
{
    
Box Box(12,10,5);

    return 0;
}

函数题 

 

6-1 创建CPU

分数 10

全屏浏览题目

切换布局

作者 杨军

单位 四川师范大学

定义一个CPU类,包含等级(Rank)、频率(frequency)、电压(voltage)等属性。其中,rank为枚举类型CPU__Rank,定义为enum CPU_Rank{P1=1,P2,P3,P4,P5,P6,P7},frequency为单位是MHz的整型数,voltage为浮点型的电压值。

函数接口定义:

 

根据题目要求写出类。

裁判测试程序样例:

 

/* 请在这里填写答案 */ int main() { CPU a(P6,3,300); cout<<"cpu a's parameter"<<endl; a.showinfo(); //显示性能参数 CPU b; cout<<"cpu b's parameter"<<endl; b.showinfo(); //显示性能参数 CPU c(a); cout<<"cpu c's parameter"<<endl; c.showinfo(); //显示性能参数 }

输入样例:

输出样例:

create a CPU!
cpu a's parameter
rank:6
frequency:3
voltage:300
create a CPU!
cpu b's parameter
rank:1
frequency:2
voltage:100
copy create a CPU!
cpu c's parameter
rank:6
frequency:3
voltage:300
destruct a CPU!
destruct a CPU!
destruct a CPU!
#include<iostream>
using namespace std;
class CPU
{
  int frequency;
  float voltage;
  int rank;
public:
  CPU()
  {
    rank=1;
    frequency=2;
    voltage=100;
    cout<<"create a CPU!"<<endl;
  }
  CPU(int r,int f,float v)
  {
    rank=r;
    frequency=f;
    voltage=v;
    cout<<"create a CPU!"<<endl;
  }
  CPU(CPU &a)
  {
    rank=a.rank;
    frequency=a.frequency;
    voltage=a.voltage;
    cout<<"copy create a CPU!"<<endl;
  }
  ~CPU()
  {
    cout<<"destruct a CPU!"<<endl;
  }
  void showinfo()
  {
    cout<<"rank:"<<rank<<endl;
    cout<<"frequency:"<<frequency<<endl;
    cout<<"voltage:"<<voltage<<endl;
  }
};
#define PI  1
#define P2  2
#define P3  3
#define P4  4
#define P5  5
#define P6  6

 

6-2 创建计算机

分数 10

全屏浏览题目

切换布局

作者 杨军

单位 四川师范大学

定义一个简单的Computer类,有数据成员芯片(cpu)、内存(ram)、光驱(cdrom)等等,有两个公有成员函数run、stop。cpu为CPU类的一个对象,ram为RAM类的一个对象,cdrom为CDROM类的一个对象,定义并实现这个类,为以上的类编写构造和析构函数,注意使用类组合的思想解决该问题,使得给出的主函数代码可以正确运行并得到给出的输出结果。

函数接口定义:

 

根据要求写出类的实现代码

裁判测试程序样例:

 

/* 请在这里填写答案 */ 在这里给出函数被调用进行测试的例子。例如: int main() { COMPUTER cpt1(6,4.0,200,60,32); //测试带参数构造 cout<<"cpt1's parameter:"<<endl; cpt1.showinfo(); cout<<"------------------------------"<<endl; COMPUTER cpt2; //测试不带参数构造 cout<<"cpt2's parameter:"<<endl; cpt2.showinfo(); cout<<"------------------------------"<<endl; COMPUTER cpt3(cpt1); //测试复制构造 cout<<"cpt3's parameter:"<<endl; cpt3.showinfo(); cout<<"------------------------------"<<endl; }

输入样例:

输出样例:

在这里给出相应的输出。例如:

create a CPU!
create a RAM!
create a CDROM!
create a COMPUTER with para!
cpt1's parameter:
cpu parameter:
rank:6
frequency:4
voltage:200
ram parameter:
volumn:60 GB
cdrom parameter:
speed:32
------------------------------
create a CPU!
create a RAM!
create a CDROM!
no para to create a COMPUTER!
cpt2's parameter:
cpu parameter:
rank:1
frequency:2
voltage:100
ram parameter:
volumn:1 GB
cdrom parameter:
speed:16
------------------------------
create a CPU by copy!
create a RAM by copy!
create a CDROM by copy!
create a COMPUTER by copy!
cpt3's parameter:
cpu parameter:
rank:6
frequency:4
voltage:200
ram parameter:
volumn:60 GB
cdrom parameter:
speed:32
------------------------------
destruct a COMPUTER!
destruct a CDROM!
desturct a RAM!
desturct a CPU!
destruct a COMPUTER!
destruct a CDROM!
desturct a RAM!
desturct a CPU!
destruct a COMPUTER!
destruct a CDROM!
desturct a RAM!
desturct a CPU!
#include<bits/stdc++.h>
using namespace std;
class CPU
{
		int rank;
		double frequency;
		int voltage;
	public:
		CPU(int R,double F,int V)
        {
            rank=R;
            frequency=F;
            voltage=V;
			cout<<"create a CPU!"<<endl;
		}
		CPU(CPU &cpu)
        {
            rank=cpu.rank;
            frequency=cpu.frequency;
            voltage=cpu.voltage;
			cout<<"create a CPU by copy!"<<endl;
		}
		void showinfo()
        {
			cout<<"cpu parameter:"<<endl;
			cout<<"rank:"<<rank<<endl;
			cout<<"frequency:"<<frequency<<endl;
			cout<<"voltage:"<<voltage<<endl;
		}
		~CPU()
        {
			cout<<"desturct a CPU!"<<endl;
		}
};
class RAM
{
		int volumn;
	public:
		RAM(int V)
        {
            volumn=V;
			cout<<"create a RAM!"<<endl;
		}
		RAM(RAM &ram)
        {
            volumn=ram.volumn;
			cout<<"create a RAM by copy!"<<endl;
		}
		void showinfo()
        {
			cout<<"ram parameter:"<<endl;
			cout<<"volumn:"<<volumn<<" GB"<<endl;
		}
		~RAM()
        {
			cout<<"desturct a RAM!"<<endl;
		}
};
class CDROM
{
		int speed;
	public:
		CDROM(int S)
        {
            speed=S;
			cout<<"create a CDROM!"<<endl;
		}
		CDROM(CDROM &cdrom)
        {
            speed=cdrom.speed;
			cout<<"create a CDROM by copy!"<<endl;
		}
		void showinfo()
        {
			cout<<"cdrom parameter:"<<endl;
			cout<<"speed:"<<speed<<endl;
		}
		~CDROM()
        {
			cout<<"destruct a CDROM!"<<endl;
		}
};
class COMPUTER
{
		CPU cpu;
		RAM ram;
		CDROM cdrom;
	public:
		COMPUTER():cpu(1,2,100),ram(1),cdrom(16)
        {
			cout<<"no para to create a COMPUTER!"<<endl;
		}
		COMPUTER(int rank,double frequency,int voltage,int volumn,int speed):cpu(rank,frequency,voltage),ram(volumn),cdrom(speed)
        {
			cout<<"create a COMPUTER with para!"<<endl;
		}
		COMPUTER(COMPUTER &computer):cpu(computer.cpu),ram(computer.ram),cdrom(computer.cdrom)
        {
			cout<<"create a COMPUTER by copy!"<<endl;
		}
		void showinfo()
        {
			cpu.showinfo();
			ram.showinfo();
			cdrom.showinfo();
		}
		~COMPUTER()
        {
			cout<<"destruct a COMPUTER!"<<endl;
		}
};

 

6-3 复数类

分数 20

全屏浏览题目

切换布局

作者 杨军

单位 四川师范大学

  1. 已知一个名为Complex的复数类,这个类包含:
    (1)私有成员:实部、虚部,且均为int 型
    (2)公有的带默认形参值的构造函数、复制构造函数
    (3)公有成员函数Display,其作用为显示复数
    要求:
    (1)实现满足上述属性和行为的Complex类定义;
    (2)设计函数AddComplex,函数AddComplex功能为实现两个复数相加,要求该函数的形参为复数类的常引用;
    (3)保证如下主函数能正确运行。

裁判测试程序样例:

 

/* 请在这里填写答案 */ int main(){ int x,y; cin>>x>>y; Complex c0(x,y); Complex c1(c0); cout<<"c1 is: "; c1.Display(); cin>>x>>y; Complex c2(x,y); cout<<"c2 is: "; c2.Display(); Complex c3; c3 = AddComplex(c1,c2); cout<<"c3 is: "; c3.Display(); return 0; }

输入样例:

在这里给出一组输入。例如:

2 -3 
3 4

输出样例:

在这里给出相应的输出。例如:

c1 is: 2-3i
c2 is: 3+4i
c3 is: 5+1i
#include<bits/stdc++.h>
using namespace std;
class Complex
{
    public:
    int real;
    int imag;
    Complex(int r,int i)
    {
        real=r;
        imag=i;
    }
    Complex(const Complex &c)
    {
        real=c.real;
        imag=c.imag;
    }
    void Display()
    {
        if(imag>0)
        {
            cout<<real<<"+"<<imag<<"i"<<endl;
        }
        else
        {
            cout<<real<<imag<<"i"<<endl;
        }
    }
    Complex(const Complex& c1,const Complex& c2)
    {
        real=c1.real+c2.real;
        imag=c1.imag+c2.imag;
    }
    Complex()
    {
        real;
        imag;
    }
};
Complex AddComplex(Complex c1,Complex c2)
{
    Complex c;
    c.real=c1.real+c2.real;
    c.imag=c1.imag+c2.imag;
    return c;
}

 

6-4 定义Date类

分数 10

全屏浏览题目

切换布局

作者 范鹏程

单位 内蒙古师范大学

本题要求实现一个日期类定义,根据所定义的类可以完成相关的类测试。

Date类定义:

根据Date被使用的情况,进行Date类定义,要求通过构造函数进行日期初始化,并通过display()函数进行日期格式显示,显示格式为"月/日/年"

测试程序样例:

main( ) 函数定义如下

 

int main() { Date d1(3,25,2019); Date d2(3,30); Date d3(10); Date d4; d1.display(); d2.display(); d3.display(); d4.display(); return 0; } /* 请在这里填写答案 */

输出样例:

在这里给出相应的输出。例如:

3/25/2019
3/30/2019
10/1/2019
1/1/2019
#include<bits/stdc++.h>
using namespace std;
class Date
{
    private:
    int year;
    int month;
    int day;
    public:
    Date(int d=1,int m=1,int y=2019)
    {
        day=d;
        month=m;
        year=y;
    };
    void display()
    {
        cout<<day<<"/"<<month<<"/"<<year<<endl;
    }
};

 

6-5 学生类的构造与析构

分数 10

全屏浏览题目

切换布局

作者 范鹏程

单位 内蒙古师范大学

类定义:

 

定义一个学生类Student,使得main()函数能够得到指定的输出结果

main()函数如下:

 

/* 请在这里填写答案 */ int main() {Student stud1(10010,"Wang_li",'f'); stud1.display(); Student stud2(10011,"Zhang_fun",'m'); stud2.display(); return 0; }

输入样例:

输出样例:

在这里给出相应的输出。例如:

Constructor called.
num:10010
name:Wang_li
sex:f

Constructor called.
num:10011
name:Zhang_fun
sex:m

Destructor called.
Destructor called.
#include<bits/stdc++.h>
using namespace std;
class Student
{
    private:
    int number;
    string name;
    char sex;
    public:
    Student(int num,string n,char s)
    {
        cout<<"Constructor called."<<endl;
        number=num;
        name=n;
        sex=s;
    }
    void display()
    {
        cout<<"num:"<<number<<endl;
        cout<<"name:"<<name<<endl;
        cout<<"sex:"<<sex<<endl;
        cout<<endl;
    }
    ~Student()
    {
        cout<<"Destructor called."<<endl;
    }
};

 

 

 

 

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值