关于c++这个怎么做

img


做这个矩形类的具体思路是什么啊,谢谢嘞
就是具体的成员函数的大致作用

  • 你可以参考下这个问题的回答, 看看是否对你有帮助, 链接: https://ask.csdn.net/questions/7485378
  • 你也可以参考下这篇文章:C++中回调函数使用详解(普通函数作为回调函数以及类的成员函数作为回调函数)
  • 除此之外, 这篇博客: C++数据结构队列的链式存储应用 医院就诊系统中的 使用队列的链式存储结构实现医院就诊系统,队列的特点先进先出 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
  • 医院就诊系统一共实现四个功能:1排队,2就诊,3查询排队患者信息,4退出系统
    1、 排队,排队的病历号从0开始,每执行依次排队,病历号+1,进链队
    2、 就诊,依次出链队,先进先出,同时随机显示诊断结果
    3、 查询排队人数,不仅显示出人数,也显示出排队患者的病历号
    4、 退出系统,同时清空链队。

    具体代码的解释都写在注释里了

    #include<iostream>
    #include<fstream>
    #include<stdlib.h>
    #include<stdio.h>
    using namespace std; 
    typedef int ElemType;
    struct LNode{
    	ElemType data;//值域 
    	LNode* next;//链接指针域 
    };
    struct LinkQueue{
    	LNode* front;//队首指针 
    	LNode* rear;//队尾指针 
    }; 
    
    //1.初始化队列
    void InitQueue(LinkQueue& HQ){
    	HQ.front=HQ.rear=NULL;
    
    } 
    //7.计算链队长度
    int LenthQueue(LinkQueue& HQ){
    	LNode* p;
    	int Lenth=0;
    	p=HQ.front->next;
    	while(p!=NULL){
    		Lenth++;
    		p=p->next;
    	}
    	return Lenth;
    } 
    
    //2.向链队中插入一个元素
    ElemType EnQueue(LinkQueue& HQ,ElemType item){
    	LNode* newptr=new LNode;//得到一个新结点
    	newptr->data=item;
    	newptr->next=NULL;//新结点指针域置空
    	if(HQ.rear==NULL)
    	   HQ.front=HQ.rear=newptr;//若链队为空的话,则新结点既是队首又是队尾 
    	else//若链队非空,则新结点被链接到队尾,并且修改队尾指针 
    	   HQ.rear=HQ.rear->next=newptr;
    
    } 
    //3.从链队中删除一个元素
    ElemType OutQueue(LinkQueue& HQ,ElemType &e){
    	//判空
    	if(HQ.front==NULL){
    		cerr<<"没有患者在排队!"<<endl;
    	} 
       /* LNode* p=HQ.front;//暂存队首指针以便回收队首结点
    	HQ.front=p->next;//修改队首指针,使其指向下一个结点
    	e = p->data;
    	if(HQ.front==NULL)
    	  HQ.rear=NULL;//若删除之后链队为空,则使队尾指针为空 
    	delete p;*/
    	LNode *p;
    	p = HQ.front->next;
    	e = p->data;
    	HQ.front->next = p->next;
    	if (HQ.rear == p)
    		HQ.rear = HQ.front;
    	free(p);		
    } 
    /*//4.读取队首元素
    ElemType PeekQueue(LinkQueue& HQ){
    	if(HQ.front==NULL){
    		cerr<<" 链队队首无元素!"<<endl;
    		exit(1);
    	} 
    	return HQ.front->data;
    } 
    */ 
    //5.检查链队是否为空
    bool EmptyQueue(LinkQueue& HQ){
    	return HQ.rear==NULL; 
    } 
    
    //6.清除链队中的所有元素,使其成为一个空队
     void ClearQueue(LinkQueue& HQ){
        LNode* p=HQ.front;//队首指针赋给p
    	while(p!=NULL){/*依次删除链队里的每一个结点,循环结束后,队首指针已经为空!*/
    		HQ.front=HQ.front->next;
    		delete p;
    		p=HQ.front;
    	} 
    	HQ.rear=NULL;
     }
     //疾病类型的输出,运用随机 
    void Type_Sickness(int type){
    	switch(type){
    		case 1:
    		    cout<<" 您得的是感冒,吃点药吧!"<<endl;
    			break;
    		case 2:
    			cout<<" 您得的是严重感冒,打针吧!"<<endl;
    			break;
    		case 3:
    			cout<<" 您发烧37.8度,挂瓶吧!"<<endl;
    		    break;
    		case 4:
    			cout<<" 您是劳累过度了,注意休息即可!"<<endl;
    			break;
    		case 5:
    			cout<<" 您的病需要进一步诊断,请转去大医院!"<<endl;
    			break;
    		case 6:
    			cout<<" 您什么病也没有,恭喜您!"<<endl;
    			break;
    	}
    }
    
     //查询
     void Search(LinkQueue& HQ) {
     	LNode *p;
     	//判空
    	if(HQ.front==NULL){
    		cerr<<"当前无排队患者!"<<endl;
    	}
    	else{
    		cout<<"当前排队患者:"<<endl;
    		p=HQ.front->next;
    		while(p!=NULL){
    			cout<<"病历号为"<<p->data<<"的患者"<<endl;
    			p=p->next; 
    		}
    	} 
    }
    main(){
    	LinkQueue HQ;
        InitQueue(HQ);
       
        int num;//排队病历号 
        //int numm;//当前就诊患者病历号 
        int n;
        cout<<"**医院就诊系统**"<<endl<<endl;
        cout<<" 1.排队"<<endl;
        cout<<" 2.就诊"<<endl;
        cout<<" 3.查询"<<endl;
        cout<<" 4.下班"<<endl;
    
    while(1){
    	cout<<"请选择服务:"<<endl;
    	cin>>n;
    	if(n==1){//排队 
    		EnQueue(HQ,num);
    		cout<<"患者病历号为:"<<num<<endl;
    		cout<<"在您前面有"<<LenthQueue(HQ)<<"位患者"<<endl; 
    		num++; 
    	}
    	if(n==2){//就诊 
    		OutQueue(HQ,num);
    		cout<<"当前就诊患者病历号为:"<<num<<endl;
    		cout<<"诊断结果为:";
    		Type_Sickness(rand()%6);// rand()%6产生的值为0-5,rand()函数产生的数在0到32767之间 
    	}
    	if(n==3){//查询
    	    Search(HQ) ;
    	    cout<<endl;
    	}
    	if(n==4){//退出 
    	ClearQueue(HQ);
    	cout<<"您已经成功退出系统!"<<endl; 
    		break;
    	}	
      }
      system("pause");
      return 0; 
    }
    
  • 您还可以看一下 夏曹俊老师的C++ 设计模式原理与实战大全-架构师需备课程课程中的 简单工厂实用工程技术:C++对象动态创建技术与配置化开发小节, 巩固相关知识点

这个题目的意思是
编写一个Rectangle类,包含2个double私有成员width和height,一个无参构造函数默认创建一个width=1和height=1的矩形,一个有参构造函数包含width和height参数,公有的GetWidth SetWidth GetHeight SetHeight方法,公有的double getArea方法返回面积,getPerimeter方法返回周长,然后主程序定义R1(2, 7) R2(2.7, 5.9)两个对象,分别输出两个对象的长宽以及面积和周长
代码如下

#include<iostream>
using namespace std;

class Rectangle {
public:
    Rectangle() {
        width = 1;
        height = 1;
    }
    Rectangle(double w, double h) {
        width = w;
        height = h;
    }
    double GetWidth() const { return width; }
    double GetHeight() const { return height; }
    void SetWidth(double w) { width = w; }
    void SetHeight(double h) { height = h; }
    double getArea() const { return width * height; }
    double getPerimeter() const { return (width + height) * 2; }
private:
    double width, height;
};

int main() {
    Rectangle R1(2, 7);
    Rectangle R2(2.7, 5.9);
    cout << "长为" << R1.GetWidth() << "  宽为" << R1.GetHeight() << "  面积为" << R1.getArea() << "  周长为" << R1.getPerimeter() << endl;
    cout << "长为" << R2.GetWidth() << "  宽为" << R2.GetHeight() << "  面积为" << R2.getArea() << "  周长为" << R2.getPerimeter() << endl;
    return 0;
}