[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 北京奥运会男足参赛国家: 科特迪瓦,阿根廷,澳大利亚,塞尔维亚 ...
随机推荐
- CART树
算法概述 CART(Classification And Regression Tree)算法是一种决策树分类方法. 它采用一种二分递归分割的技术,分割方法采用基于最小距离的基尼指数估计函数,将当前的 ...
- jQuery操纵cookie(原生javascript处理cookie)
jQuery也是可以操作cookie的 1.首先下载jQuery.js 以及 jquery.cookie.js 这两个文件 2.安装(其实就是引用) <html> <he ...
- java.util.Collection源码分析和深度讲解
写在开头 java.util.Collection 作为Java开发最常用的接口之一,我们经常使用,今天我带大家一起研究一下Collection接口,希望对大家以后的编程以及系统设计能有所帮助,本文所 ...
- 线程7--GCD的基本使用
子线程执行延时操作,执行完成后返回主线程更新界面 dispatch_queue_t queue=dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DE ...
- SQLSERVER查询整个数据库中某个特定值所在的表和字段的方法
这几天有业务部门需要使用一个SAP B1老系统 中的报表,但是由于此报表没有加时间条件,导致一旦开始查询 就会导致B1系统异常退出.由于报表对应的SQL 是存在数据库中,所以想通过查找到这个报表的S ...
- spring boot获取request
1. Controller中 1.1 通过静态方法获取 HttpServletRequest request = ((ServletRequestAttributes)RequestContextHo ...
- php5.6+apache2.4+linux搭建php环境
前言 最近突然想搭建个人博客,尽管笔者擅长java-web,但综合各种原因,于是选择了大众化的php+mysql搭建个人博客.对于php,只闻其大名,但从未学过,于是,笔者将从php环境搭建开始,到服 ...
- 莫名其妙的标记之@noescape
Swift 中经常遇到一些不熟悉的关键字, 例如@autoclosure, @noescape...等等, 为什么要加这样的关键字, 我自己写方法的时候什么时候要加, 什么时候不加, 都是应该考虑的问 ...
- listview更改选中时item背景色(转)
默认情况下使用ListView背景色是黑色,选中item的高亮颜色是菊黄色,很多时候不得不自己定义背景色或者背景图 android:cacheColorHint="@android:colo ...
- js-权威指南学习笔记16
1.元素的style属性可以用来设置样式,但是不适合用来查询样式(只能查询到内联样式). 2.CSS里的层叠指示了应用于文档中任何给定元素的样式规则是各个来源的层叠效果:Web浏览器的默认样式表.文档 ...