zoukankan      html  css  js  c++  java
  • [C++] 用Xcode来写C++程序[7] Class

    用Xcode来写C++程序[7] Class

    不带构造函数的Rectangle类

    //
    //  Rectangle.h
    //  Plus
    //
    //  Created by YouXianMing on 15/3/12.
    //  Copyright (c) 2015年 YouXianMing. All rights reserved.
    //
    
    #ifndef __Plus__Rectangle__
    #define __Plus__Rectangle__
    
    #include <stdio.h>
    
    class Rectangle {
        
        int width;  //
        int height; //
        
    public:
        
        /**
         *  面积
         *
         *  @return 求取面积
         */
        int  area();
        
        /**
         *  设置长与宽
         *
         *  @param x 长
         *  @param y 宽
         */
        void set_values (int x, int y);
    };
    
    #endif
    //
    //  Rectangle.cpp
    //  Plus
    //
    //  Created by YouXianMing on 15/3/12.
    //  Copyright (c) 2015年 YouXianMing. All rights reserved.
    //
    
    #include "Rectangle.h"
    
    int Rectangle::area() {
        return width * height;
    }
    
    void Rectangle::set_values (int x, int y) {
        width  = x;
        height = y;
    }
    #include <iostream>
    #include "Rectangle.h"
    
    using namespace std;
    
    int main () {
        
        // 创建出对象
        Rectangle rect;
        
        // 给对象设置值
        rect.set_values(3, 4);
        
        // 打印对象的面积
        cout << "area: " << rect.area();
        
        return 0;
    }

    带构造函数的Rectangle类

    //
    //  Rectangle.h
    //  Plus
    //
    //  Created by YouXianMing on 15/3/12.
    //  Copyright (c) 2015年 YouXianMing. All rights reserved.
    //
    
    #ifndef __Plus__Rectangle__
    #define __Plus__Rectangle__
    
    #include <stdio.h>
    
    class Rectangle {
        
        int width;  //
        int height; //
        
    public:
        
        /**
         *  构造函数
         */
        Rectangle(int, int);
        
        /**
         *  面积
         *
         *  @return 求取面积
         */
        int  area();
    };
    
    #endif
    //
    //  Rectangle.cpp
    //  Plus
    //
    //  Created by YouXianMing on 15/3/12.
    //  Copyright (c) 2015年 YouXianMing. All rights reserved.
    //
    
    #include "Rectangle.h"
    
    int Rectangle::area() {
        return width * height;
    }
    #include <iostream>
    #include "Rectangle.h"
    
    using namespace std;
    
    int main () {
        
        // 创建出对象
        Rectangle rect(3, 4);
        
        // 打印对象的面积
        cout << "area: " << rect.area();
        
        return 0;
    }

    重载了构造函数的Rectangle类

    //
    //  Rectangle.h
    //  Plus
    //
    //  Created by YouXianMing on 15/3/12.
    //  Copyright (c) 2015年 YouXianMing. All rights reserved.
    //
    
    #ifndef __Plus__Rectangle__
    #define __Plus__Rectangle__
    
    #include <stdio.h>
    
    class Rectangle {
        
        int width;  //
        int height; //
        
    public:
        
        /**
         *  构造函数
         */
        Rectangle(int x, int y);
        Rectangle();
        
        /**
         *  面积
         *
         *  @return 求取面积
         */
        int  area();
    };
    
    #endif
    //
    //  Rectangle.cpp
    //  Plus
    //
    //  Created by YouXianMing on 15/3/12.
    //  Copyright (c) 2015年 YouXianMing. All rights reserved.
    //
    
    #include "Rectangle.h"
    
    int Rectangle::area() {
        return width * height;
    }
    
    
    Rectangle::Rectangle() {
        width  = 5;
        height = 5;
    }
    
    
    Rectangle::Rectangle(int x, int y) {
        width  = x;
        height = y;
    }
    #include <iostream>
    #include "Rectangle.h"
    
    using namespace std;
    
    int main () {
        
        // 创建出对象
        Rectangle rectA(3, 4);
        Rectangle rectB;
        
        // 打印对象的面积
        cout << "areaA: " << rectA.area() << endl;
        cout << "areaB: " << rectB.area() << endl;
        
        return 0;
    }

  • 相关阅读:
    webrtc系列之-像老鼠一样打洞
    Ubuntu记录用户IP访问操作信息工具
    OPENVIDU实现同一用户同时发布多个流媒体
    如何使用Nginx-rtmp搭建简易的HLS直播系统
    python实现数据库主从状态监控
    简单分析实现运维利器---批量操作bashshell
    《我与Windows Server 2008R2那点事儿》之域控账户故障事件
    常用动态路由协议之IS-IS
    在云服务器上搭建Python开发环境
    基于Python3接口自动化测试开发相关常用方法
  • 原文地址:https://www.cnblogs.com/YouXianMing/p/4333437.html
Copyright © 2011-2022 走看看