[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(, ); // 打印对象的面积
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的更多相关文章
- [C++] 用Xcode来写C++程序[6] Name visibility
用Xcode来写C++程序[6] Name visibility 此小结包括了命名空间的一些使用细节 命名空间 #include <iostream> using namespace st ...
- [C++] 用Xcode来写C++程序[5] 函数的重载与模板
用Xcode来写C++程序[5] 函数的重载与模板 此节包括函数重载,隐式函数重载,函数模板,带参数函数模板 函数的重载 #include <iostream> using namespa ...
- [C++] 用Xcode来写C++程序[4] 函数
用Xcode来写C++程序[4] 函数 此节包括引用函数,内联函数,防止修改函数入参,函数自身带有默认值. 引用函数:防止复制对象,减少系统开销 内联函数:编译的时候根据具体情形将代码嵌入进去,成不成 ...
- [C++] 用Xcode来写C++程序[3] Constants
用Xcode来写C++程序[3] Constants 以下是一些基本数据的含义: 75 // int 75u // unsigned int 75l // long 75ul // unsigned ...
- [C++] 用Xcode来写C++程序[2] 操作变量
用Xcode来写C++程序[2] 操作变量 此节讲解包括变量的初始化的几种方式,以及泛型编程的两种变量赋值方式. 最基本的变量赋值以及操作: // operating with variables # ...
- [C++] 用Xcode来写C++程序[1] 新建C++项目工程
用Xcode来写C++程序[1] 新建C++项目工程 第一节从新建工程并编译C++源码开始 新建工程 源码: // // main.cpp // YeHelloWorld // // Created ...
- 使用Xcode IDE写node.js
最近在玩node.js 但是发现很多IDE就是用不顺手 后来发现Xcode可以剖析java script 于是试着使用Xcode来当做node.js的编辑器 首先,在Mac上必须先安装node.js的 ...
- 使用Code::blocks在windows下写网络程序
使用Code::blocks在windows下写网络程序 作者 He YiJun – storysnail<at>gmail.com 团队 ls 版权 转载请保留本声明! 本文档包含的原创 ...
- JAVA-集合作业-已知有十六支男子足球队参加2008 北京奥运会。写一个程序,把这16 支球队随机分为4 个组。采用List集合和随机数
第二题 已知有十六支男子足球队参加2008 北京奥运会.写一个程序,把这16 支球队随机分为4 个组.采用List集合和随机数 2008 北京奥运会男足参赛国家: 科特迪瓦,阿根廷,澳大利亚,塞尔维亚 ...
随机推荐
- mysql创建用户,并指定用户的权限(grant命令)
参考链接http://blog.csdn.net/leili0806/article/details/8573636,谢谢这位仁兄 1.创建新用户的SQL语句: CREATE USER 'pig'@' ...
- Cmake编写JNI
调用两个库 CMakeLists.txt //把那种大段的注释去掉了 cmake_minimum_required(VERSION ) add_library( # Sets the name of ...
- Python socket编程之构造IP首部和ICMP首部
这两天在做一个实验需要自己构造IP首部,遇到诸多问题,搞了一天终于搞定. 关于socket的介绍网上一大堆,我只记录构造IP头时我遇到的问题.由于没玩过socket构造IP首部,网上找了段代码研究下, ...
- [转] What is a Full Stack developer?
期望一个凡人掌握开发过程中各个方面的知识,合理吗?也许不合理,但是Facebook正是要寻找这样的人.在一个OSCON会议上,一名Facebook的工程师告诉我的,他们只聘请“全能(Full stac ...
- asp.net web api 跨域,带cookie
官网上有一个介绍 http://www.asp.net/web-api/overview/security/enabling-cross-origin-requests-in-web-api 但是只支 ...
- Oracle官网下载参考文档
最近有人问我有没有Oracle11g数据库官方参考文档,我就想,这不是在官网可以下载到的吗,疑惑,问了之后才知道,他官网找过,但时没有找到.不要笑,其实有很多人一样是找不到的,下面就一步一步操作下: ...
- Linq in条件查询
Linq 实现sql中的not in和in条件查询 T-SQL的IN: Select ProductID, ProductName, CategoryID From dbo.Products Wh ...
- JS获取地址栏参数&jquery
第一种:字符串拆分法 window.location.href 或者 location.href 或者 window.location 获得地址栏中的所有内容 decodeURI()可以解码地址栏中的 ...
- 湘潭校赛 Easy Wuxing
Easy Wuxing Accepted : 25 Submit : 124 Time Limit : 1000 MS Memory Limit : 65536 KB 题目描述 “五行”是中国 ...
- 悟空模式-java-普通工厂模式
[大圣看玩多时,问土地道:“此树有多少株数?”土地道:“有三千六百株.前面一千二百株,花微果小,三千年一熟,人吃了成仙了道,体健身轻.中间一千二百株,层花甘实,六千年一熟,人吃了霞举飞升,长生不老.后 ...