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

时间: 2024-08-08 01:02:47

[C++] 用Xcode来写C++程序[7] Class的相关文章

[C++] 用Xcode来写C++程序[4] 函数

用Xcode来写C++程序[4] 函数 此节包括引用函数,内联函数,防止修改函数入参,函数自身带有默认值. 引用函数:防止复制对象,减少系统开销 内联函数:编译的时候根据具体情形将代码嵌入进去,成不成功编译器说了算,减少系统开销提升性能 引用函数(防止篡改初始值的入参声明方式):防止修改数据源 函数参数带有默认值:函数的某个参数可以给定默认值,精简函数的使用 最简单的函数 #include <iostream> using namespace std; int addition (int a,

[C++] 用Xcode来写C++程序[5] 函数的重载与模板

用Xcode来写C++程序[5] 函数的重载与模板 此节包括函数重载,隐式函数重载,函数模板,带参数函数模板 函数的重载 #include <iostream> using namespace std; int operate (int a, int b) { return (a * b); } double operate (double a, double b) { return (a / b); } int main () { int x = 5; int y = 2; double n

[C++] 用Xcode来写C++程序[6] Name visibility

用Xcode来写C++程序[6] Name visibility 此小结包括了命名空间的一些使用细节 命名空间 #include <iostream> using namespace std; namespace foo { // 函数 int value() { return 5; } } namespace bar { // 常量 const double pi = 3.1416; // 函数 double value() { return 2*pi; } } int main () {

[C++] 用Xcode来写C++程序[3] Constants

用Xcode来写C++程序[3] Constants 以下是一些基本数据的含义: 75 // int 75u // unsigned int 75l // long 75ul // unsigned long 75lu // unsigned long 3.14159 // 3.14159 6.02e23 // 6.02 x 10^23 1.6e-19 // 1.6 x 10^-19 3.0 // 3.0 3.14159L // long double 6.02e23f // float 'z'

[C++] 用Xcode来写C++程序[2] 操作变量

用Xcode来写C++程序[2] 操作变量 此节讲解包括变量的初始化的几种方式,以及泛型编程的两种变量赋值方式. 最基本的变量赋值以及操作: // operating with variables #include <iostream> using namespace std; int main () { // 声明变量 int a, b; int result; // 赋值 a = 5; b = 2; a = a + 1; result = a - b; // 打印结果 cout <&

[C++] 用Xcode来写C++程序[1] 新建C++项目工程

用Xcode来写C++程序[1] 新建C++项目工程 第一节从新建工程并编译C++源码开始 新建工程 源码: // // main.cpp // YeHelloWorld // // Created by XianMingYou on 15/3/5. // Copyright (c) 2015年 XianMingYou. All rights reserved. // #include <iostream> using namespace std; int main() { cout <

只用vs2008写qt程序

qt是一个c++的界面库,其特点就是其源码可以跨平台编译,这样在写自己的小工具时可以方便地在windows,mac或linux环境下移植了.在windows下写c++程序当然选vs,在mac下写程序当然用xcode,在linux下就完全控制台吧. 本人仅仅将其看成是一个c++的库,所以不想学习新的的IDE--qt creator,也不想去学qt designer,所以qt的预处理都需要自己用命令来折腾. Moc 从qt继承而来的类只要用了关键字 Q_OBJECT,都必须生成其对应的moc文件,命

嵌入式Linux之旅——环境搭建篇之烧写裸机程序

本小节将介绍如何使用oflash和openjtag烧写裸机程序.oflash也支持并口烧写,方法与openjtag类似.如果你想使用jlink烧写,需要安装SEGGER的J-Flash的工具,这里我们就不多介绍. 首先需要先安装oflash,oflash由开发板厂商提供或者从网上下载.将oflash加上可执行权限,拷贝到“/usr/bin”目录下即可.命令如下: sudo cp oflash /usr/bin/ && sudo chmod +x /usr/bin/oflash 下面就是具体

JAVA-集合作业-已知有十六支男子足球队参加2008 北京奥运会。写一个程序,把这16 支球队随机分为4 个组。采用List集合和随机数

第二题 已知有十六支男子足球队参加2008 北京奥运会.写一个程序,把这16 支球队随机分为4 个组.采用List集合和随机数 2008 北京奥运会男足参赛国家: 科特迪瓦,阿根廷,澳大利亚,塞尔维亚,荷兰,尼日利亚.日本,美国,中国,新西 兰,巴西,比利时,韩国,喀麦隆,洪都拉斯,意大利 package Test03; import java.util.ArrayList; import java.util.List; import java.util.Random; public class