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

目录
相关文章
|
22天前
|
存储 缓存 算法
【C/C++ 性能优化】提高C++程序的缓存命中率以优化性能
【C/C++ 性能优化】提高C++程序的缓存命中率以优化性能
111 0
C4.
|
1月前
|
C++
C++class的构造与析构
C++class的构造与析构
C4.
23 0
|
1月前
|
编译器 Linux C++
3C++程序的编写和实现
3C++程序的编写和实现
17 2
|
1月前
|
C++ 开发者
2C++的程序的构成和书写形式
2C++的程序的构成和书写形式
16 2
|
17天前
|
存储 缓存 C++
C++链表常用的函数编写(增查删改)内附完整程序
C++链表常用的函数编写(增查删改)内附完整程序
|
29天前
|
编译器 测试技术 调度
C++ 中 template<class T>和template<typename T>的区别
C++ 中 template<class T>和template<typename T>的区别
10 0
|
30天前
|
缓存 编译器 程序员
C/C++编译器并行优化技术:并行优化针对多核处理器和多线程环境进行优化,以提高程序的并行度
C/C++编译器并行优化技术:并行优化针对多核处理器和多线程环境进行优化,以提高程序的并行度
60 0
|
30天前
|
缓存 编译器 程序员
C/C++编译器全局优化技术:全局优化是针对整个程序进行的优化,包括函数之间的优化
C/C++编译器全局优化技术:全局优化是针对整个程序进行的优化,包括函数之间的优化
25 0
|
30天前
|
缓存 算法 编译器
C/C++编译器内存优化技术:内存优化关注程序对内存的访问和使用,以提高内存访问速度和减少内存占用。
C/C++编译器内存优化技术:内存优化关注程序对内存的访问和使用,以提高内存访问速度和减少内存占用。
35 0
|
30天前
|
自然语言处理 编译器 调度
深入gcc编译器:C/C++代码如何变为可执行程序
深入gcc编译器:C/C++代码如何变为可执行程序
75 0

热门文章

最新文章