[C++] 用Xcode来写C++程序[7] Class
2015-03-12 22:11
127 查看
用Xcode来写C++程序[7] Class
不带构造函数的Rectangle类
带构造函数的Rectangle类
重载了构造函数的Rectangle类
不带构造函数的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; }
相关文章推荐
- Xcode编写C/C++程序
- [C++] 用Xcode来写C++程序[1] 新建C++项目工程
- mac下使用xcode编写c++程序
- [C++] 用Xcode来写C++程序[3] Constants
- xcode开发c/c++程序
- C++程序编译的时候报错prototype for '类名::函数名'does not match any in class'类名'解决经验。
- 两个小问题解决记录:XCode中多Target时CFBundleDisplayName的多语言本地化;用C++写socket程序时编译错误说read和write找不到
- C++程序代写实现HashSet class
- [C++] 用Xcode来写C++程序[6] Name visibility
- mac环境下分别用vim和Xcode运行C++(hello word)程序
- [C++] 用Xcode来写C++程序[5] 函数的重载与模板
- Xcode 编译C++程序,freopen 读取文件时文件路径问题
- 在Xcode下执行C/C++程序
- [C++] 用Xcode来写C++程序[4] 函数
- caffe之mac环境下通过XCode调试C++程序
- 在你的C++程序里实现委托
- More Effective C++ Item M34:如何在同一程序中混合使用C++和C
- C++实战之程序书法
- 用PHP实现通过Web执行C/C++程序
- >>VC++6.0 和 Dev-C++4989 编译的程序性能差