用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. Linux-(kill,wc,killall,ln,cal,date)

    kill命令 1.命令格式: kill [参数] [进程号] 2.命令功能: 发送指定的信号到相应进程.不指定型号将发送SIGTERM(15)终止指定进程.如果仍无法终止该程序可用“-KILL” 参数 ...

  2. SpringBoot入门 (三) 日志配置

    上一篇博文记录了再springboot项目中读取属性文件中配置的属性,本文学习在springboot项目中记录日志. 日志记录在项目中是很常见的一个功能了,对排查问题有很大帮助,也可以做分类分析及统计 ...

  3. BEA-141150 - An error occurred while preparing application component uri of application application with HTTP response responseCode: message

    网上查询的 Cause:This is either because the resource it self is not available on the administration serve ...

  4. RabbitMQ上手记录–part 6-Shovel

    上一part<RabbitMQ上手记录–part 5-节点集群高可用(多服务器)>讲到了通过多个服务器来搭建RabbitMQ的节点集群,示例当中提到的服务器都是在同一个局域网中的(实际上是 ...

  5. c#调用webservices

    有两种方式,静态调用(添加web服务的暂且这样定义)和动态调用: 静态调用: 使用添加web服务的方式支持各种参数,由于vs2010会自动转换,会生成一个特定的Reference.cs类文件   动态 ...

  6. [转]How to tell NLog to log exceptions?

    本文转自:https://stackoverflow.com/questions/9199073/how-to-tell-nlog-to-log-exceptions 问: Target: <t ...

  7. C#将Dll嵌入到EXE

    感谢博主的方法: https://blog.csdn.net/lin381825673/article/details/39122257 我之前也看了网上的其他的方法试了都不行 第一种方法就是说用IL ...

  8. C#基础笔记(第十四天)

    1.MD5加密 用户在数据库存密码需要进行再加密,这样一个过程叫MD5加密只要涉及到存用户的密码一定要用MD5加密MD5密码一般都是16进制的把一个密码转换成16进制的过程就叫MD5加密把字符串加密成 ...

  9. myBatis组件之缓存实现及使用

    一 .概述 先讲缓存实现,主要是mybatis一级缓存,二级缓存及缓存使用后续补充 Mybatis缓存的实现是基于Map的,从缓存里面读写数据是缓存模块的核心基础功能:除核心功能之外,有很多额外的附加 ...

  10. HDU1560(KB2-E IDA星)

    DNA sequence Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...