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;
    }

  • 相关阅读:
    一个最简单的例子学习SAP Cloud for Customer HTML mashup
    SAP C4C Embedded Component最常见的故障原因分析
    使用SAP CRM Mock framework进行单元测试
    ABAP Debugging Script(调试器脚本)使用的一些实际例子
    SAP ABAP字符和字符串变量隐式转换的一些规则
    两种使用代码获得SAP CRM product sales status的办法
    扎根CNCF社区贡献五年是怎样的体验?听听华为云原生开源团队的负责人怎么说
    LiteOS调测利器:backtrace函数原理知多少
    微服务容错时,这些技术你要立刻想到
    Volcano 监控设计解读,一看就懂
  • 原文地址:https://www.cnblogs.com/YouXianMing/p/4333437.html
Copyright © 2011-2022 走看看