用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(, ); // 打印对象的面积
cout << "area: " << rect.area(); return ;
}

带构造函数的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(, ); // 打印对象的面积
cout << "area: " << rect.area(); return ;
}

重载了构造函数的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 = ;
height = ;
} Rectangle::Rectangle(int x, int y) {
width = x;
height = y;
}
#include <iostream>
#include "Rectangle.h" using namespace std; int main () { // 创建出对象
Rectangle rectA(, );
Rectangle rectB; // 打印对象的面积
cout << "areaA: " << rectA.area() << endl;
cout << "areaB: " << rectB.area() << endl; return ;
}

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

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

    用Xcode来写C++程序[6] Name visibility 此小结包括了命名空间的一些使用细节 命名空间 #include <iostream> using namespace st ...

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

    用Xcode来写C++程序[5] 函数的重载与模板 此节包括函数重载,隐式函数重载,函数模板,带参数函数模板 函数的重载 #include <iostream> using namespa ...

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

    用Xcode来写C++程序[4] 函数 此节包括引用函数,内联函数,防止修改函数入参,函数自身带有默认值. 引用函数:防止复制对象,减少系统开销 内联函数:编译的时候根据具体情形将代码嵌入进去,成不成 ...

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

    用Xcode来写C++程序[3] Constants 以下是一些基本数据的含义: 75 // int 75u // unsigned int 75l // long 75ul // unsigned ...

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

    用Xcode来写C++程序[2] 操作变量 此节讲解包括变量的初始化的几种方式,以及泛型编程的两种变量赋值方式. 最基本的变量赋值以及操作: // operating with variables # ...

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

    用Xcode来写C++程序[1] 新建C++项目工程 第一节从新建工程并编译C++源码开始 新建工程 源码: // // main.cpp // YeHelloWorld // // Created ...

  7. 使用Xcode IDE写node.js

    最近在玩node.js 但是发现很多IDE就是用不顺手 后来发现Xcode可以剖析java script 于是试着使用Xcode来当做node.js的编辑器 首先,在Mac上必须先安装node.js的 ...

  8. 使用Code::blocks在windows下写网络程序

    使用Code::blocks在windows下写网络程序 作者 He YiJun – storysnail<at>gmail.com 团队 ls 版权 转载请保留本声明! 本文档包含的原创 ...

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

    第二题 已知有十六支男子足球队参加2008 北京奥运会.写一个程序,把这16 支球队随机分为4 个组.采用List集合和随机数 2008 北京奥运会男足参赛国家: 科特迪瓦,阿根廷,澳大利亚,塞尔维亚 ...

随机推荐

  1. ADB故障时的一些命令

    开发中经常用到adb重启等操作,简单记录一下. 1.重启 adb kill-server adb start-server 2.显示版本号 adb version 3.显示已连接的设备 adb dev ...

  2. 用ruby调用执行shell命令

    碰到需要调用操作系统shell命令的时候,Ruby为我们提供了六种完成任务的方法: 1.Exec方法:     Kernel#exec方法通过调用指定的命令取代当前进程:   例子:       $ ...

  3. [转]How can I list all foreign keys referencing a given table in SQL Server?

    本文转自:https://stackoverflow.com/questions/483193/how-can-i-list-all-foreign-keys-referencing-a-given- ...

  4. 微信小程序开发总结(详细)

    转载: 小程序开发总结(详细) 这段时间一直在做小程序,总结下.后续可能会不断更新,努力写仔细点,争取让人看到就能会写. 一,页面结构. 这基本是小程序的标准目录结构.我们从上到下解释下:pages文 ...

  5. 使用rem编写自适应屏幕网页造成div被span撑高的解决办法

    原始代码: <html> <head> <meta charset="utf-8"> <meta content="ie=edg ...

  6. Windows标准控件

    学习目的 学习创建, 使用Windows标准控件(按钮, 滚动条, 静态控件, 列表框, 编辑框, 组合框); 学习使用子窗口控件操作函数(EnableWindow, MoveWindow, SetW ...

  7. MVC,MVP 和 MVVM 的图示(转)

    作者: 阮一峰 日期: 2015年2月 1日 转自:http://www.ruanyifeng.com/blog/2015/02/mvcmvp_mvvm.html 复杂的软件必须有清晰合理的架构,否则 ...

  8. 使用CSS3改变文本选中的默认颜色——张鑫旭

    关于浏览器文字选中颜色 以我的系统举例(xp 默认主题),浏览器上页面文字选中后默认的背景色是一种蓝色, 不同浏览器的颜色有些许差异,但大致相同,文字颜色也近乎白色,如下图所示,截自Firefox3. ...

  9. python中类的约束和限制对象添加属性

    通过__slots__限制对象可添加的属性 class A: __slots__ = ['a', 'b'] pass a1 = A() a1.a = 10 print(a1.a) a1.c = 0 # ...

  10. vue-quill-editor 富文本集成quill-image-extend-module插件实例,以及UglifyJsPlugin打包抱错问题处理

    官网 vue-quill-editor Toolbar Module - Quill vue-quill-image-upload 图片支持上传服务器并调整大小 1.在 package.json 中加 ...