[C++] 用Xcode來寫C++程式[7] Class

來源:互聯網
上載者:User

標籤:

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

 

[C++] 用Xcode來寫C++程式[7] Class

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.