Vearlit 2019-11-27 09:05 采纳率: 66.7%
浏览 479
已采纳

怎么用C++编写组合对象的使用?

有以下要求:
1、设计类似PPT页的类CPPage,定义其数据成员、成员函数,要求至少包含一个矩形对象数组和一个椭圆形对象数组(最好是动态对象数组)(椭圆形类仿照矩形类设计)
2、设计成员函数Show显示本PPT页的相关信息:如矩形个数,椭圆形个数
3、设置CPPage类为矩形类的友元类;并在Show函数中直接访问矩形类的文本串。
4、增加一个静态成员对CPPage类进行计数;
5、对象信息的输入方式采用读取文本文件的方式。
注:拜托给出所有代码,救救孩子吧,万分感谢!

  • 写回答

1条回答 默认 最新

  • threenewbee 2019-11-27 09:07
    关注
    // Q977352.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    
    class Rectangle
    {
    friend class CPPage;
    private:
        int t, l, w,h;
        string msg;
    public:
        Rectangle() { t=l=w=h=0;  }
        Rectangle(int top, int left, int width, int height) { t =top;l=left;w=width;h=height; }
        Rectangle(const Rectangle & src) {  t=src.t;l=src.l;w=src.w;h=src.h;msg=src.msg; }
        void ShowMsg() { cout << t << l << w << h << msg; }
        ~Rectangle() { }
        void setMsg(string s) { msg = s; }
        string getMsg() { return msg; }
    };
    
    class Ellipse
    {
    private:
        int a, b;
    public:
        Ellipse() { a=b=0; }
        Ellipse(int a, int b) { this->a = a; this-> b = b; }
        Ellipse(const Ellipse & src) { a=src.a;b=src.b; }
        ~Ellipse() {}
        void Show() { cout << a << b; }
    };
    
    class CPPage
    {
    private:
        Rectangle rect[100];
        Ellipse elp[100];
        int rectn;
        int elpn;
        static int pagecount;
    public:
        CPPage() { rectn=elpn=0; }
        void Show() { 
            cout<<rectn<<elpn;
            for (int i = 0; i < rectn; i++)
                cout << rect[i].msg;
        }
        void addRect(Rectangle r) { rect[rectn++] = r; }
        void addEllipse(Ellipse e) { elp[elpn++] = e; }
    };
    
    int main()
    {
        ifstream infile;
        infile.open("c:\\1.txt");
        int m;
        int n;
        CPPage page;
        infile >> m;
        infile >> n;
        for (int i = 0; i < m; i++)
        {
            int t, l, w,h;
            string msg;
            infile >> t >> l >> w >> h;
            infile >> msg;
            Rectangle rect(t, l, w, h);
            rect.setMsg(msg);
            page.addRect(rect);
        }
        for (int i = 0; i < n; i++)
        {
            int a,b;
            infile >> a >> b;
            Ellipse elp(a,b);
            page.addEllipse(elp);
        }
        page.Show();
        return 0;
    }
    
    

    1.txt文本内容

    2
    1
    1
    1
    1
    1
    aaa
    2
    2
    2
    2
    bbb
    1
    1
    

    输出
    21aaabbb

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)