用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. yum命令速查

    1 yum的基本原理 CentOS可在yum服务器上下载官方网站给出的rpm给出的表头列表数据,该数据除了记载每个rpm软件的相依性之外,也说明了rpm所放置的容器(repository)所在.通过分 ...

  2. [codeup] 1128 出租车费

    题目描述 某市出租车计价规则如下:起步4公里10元,即使你的行程没超过4公里:接下来的4公里,每公里2元:之后每公里2.4元.行程的最后一段即使不到1公里,也当作1公里计费. 一个乘客可以根据行程公里 ...

  3. JavaWeb项目WebContent下的资源文件无法引用

    JavaWeb项目引用资源的时候尽量使用绝对路径. 作者在帮助同学完善其JavaWeb项目端页面的时候,css样式文件怎么也引用不了. 第一个想到的是:是不是文件路径写错了? 于是,作者换了绝对路径, ...

  4. [IDE]快捷键整理

      VS Resharper eclipse 备注 运行 Ctrl+F5 - Ctrl+F11   调试 F5 - F11   逐语句执行 F11 - F5 IE.FF: F11 逐过程执行 F10 ...

  5. 十八、curator recipes之DistributedDelayQueue

    简介 curator实现了类似DelayQueue的分布式延迟队列 官方文档:http://curator.apache.org/curator-recipes/distributed-delay-q ...

  6. Hadoop shell 一查就会

    Hadoop shell 命令有三种格式 hdfs + dfs (必须是dfs) Hadoop + dfs Hadoop + df 命令 说明 hadoop 版本查看 hadoop version h ...

  7. K:伸展树(splay tree)

      伸展树(Splay Tree),也叫分裂树,是一种二叉排序树,它能在O(lgN)内完成插入.查找和删除操作.在伸展树上的一般操作都基于伸展操作:假设想要对一个二叉查找树执行一系列的查找操作,为了使 ...

  8. set集合去重机制

  9. MPVUE - 使用vue.js开发微信小程序

    MPVUE - 使用vue.js开发微信小程序 什么是mpvue? mpvue 是美团点评前端团队开源的一款使用 Vue.js 开发微信小程序的前端框架.框架提供了完整的 Vue.js 开发体验,开发 ...

  10. react组件直接在document上添加事件

    demo:比如组件里有个div写的框框,点击document body的背景色变红,点击div写的框框没效果 componentDidMount(){ document.onclick = this. ...