类成员函数

概念:
类的成员函数是指那些把定义和原型写在类定义内部的函数,就像类定义中的其他变量一样。

注意点:

  1. class类声明大括号后面有,与java不同;
  2. 使用范围解析运算符::定义的类成员函数,必须在class中声明
#include <iostream>
using namespace std; 

class Box
{
	public:
		double height;
		double length;
		double breath;
		double getVolume();
//		double getVolume()
//		{
//			return height * length * breath;
//		}
};

double Box::getVolume()
{
	return height * length * breath;
}
 
int main()
{
	Box box1;
	Box box2;
	
	box1.breath = 2;
	box1.height = 3;
	box1.length = 3;
	cout << "box1的体积为%f" << box1.getVolume()<<endl;
	
	box2.breath = 1;
	box2.height = 1;
	box2.length = 1;
	cout << "box2的体积为%f" << box2.getVolume();
}

相关推荐

  1. 成员函数

    2024-03-21 00:56:03       24 阅读
  2. C++入门【34-C++成员函数

    2024-03-21 00:56:03       25 阅读
  3. 的声明与成员函数的实现--Car

    2024-03-21 00:56:03       15 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-21 00:56:03       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-21 00:56:03       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-21 00:56:03       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-21 00:56:03       20 阅读

热门阅读

  1. Frida-hook基础使用之hook调试获取游戏结果

    2024-03-21 00:56:03       23 阅读
  2. 主流开发语言和开发环境介绍

    2024-03-21 00:56:03       18 阅读
  3. P3639 [APIO2013] 道路费用 题解

    2024-03-21 00:56:03       23 阅读
  4. 设计模式(行为型设计模式——职责链模式)

    2024-03-21 00:56:03       18 阅读
  5. 我的创作纪念日

    2024-03-21 00:56:03       22 阅读
  6. 三维GIS仿真技术发展的几点思考

    2024-03-21 00:56:03       18 阅读
  7. Git笔记

    2024-03-21 00:56:03       20 阅读
  8. FPGA与以太网相关接口知识

    2024-03-21 00:56:03       19 阅读
  9. .net core 接入nacos

    2024-03-21 00:56:03       24 阅读
  10. 力扣-3. 无重复字符的最长子串

    2024-03-21 00:56:03       18 阅读