栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

初学OOP --- 类的进阶内容(2)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

初学OOP --- 类的进阶内容(2)

对象常量,作用域内枚举:
#pragma once


//class.h

#include
#ifndef _CLASS_H_
#define _CLASS_H_

class exam
{
private:
	enum {months = 12};
	static const int days = 365;
	double month[months];
	double day[days];
public:
	void fuc(int a = months, int b = days)
	{
		std::cout << a << " , " << b << std::endl;
	}
};


#endif

//对象常量: 所有对象都可以使用的量
// 方法一:枚举 enum { months = 12 },因为此处枚举不会创建类数据成员,所以不需要提供枚举的类型名
// 在类中声明的枚举其作用域为整个类
// 方法二:static静态变量,可以将该常量和其他静态变量放在一起,而不是存储在对象中,即可以被所有对象访问
//const int months = 12 不行的原因: 声明类只是描述了对象的形式,并没有创建对象,所以在创建对象前,该常量都是不存在的,没有存储该量的存储空间

//作用域内枚举: enum class name{ , , ,}  作用:防止不同枚举定义中的枚举量发生名称冲突
// eg: enum class egg{large}   enum class t-shirt(large)
// 此时 egg a = egg::large 和 t-shirt b = t-shirt::large不会发生冲突


//main.cpp

#include"class.h"


int main()
{
	enum class t1 { GodFishhh=1,AFish=2};
	enum class t2 {GodFishhh=10,AFish=20};
	// t1和t2中同名的枚举量不会发生冲突
	t1 temp1 = t1::GodFishhh;
	t2 temp2 = t2::GodFishhh;
	exam c1;
	c1.fuc();
	


	system("pause");
	return 0;

	
}

//1.对象常量: 所有对象都可以使用的量
// 方法一:枚举 enum { months = 12 },因为此处枚举不会创建类数据成员,所以不需要提供枚举的类型名
// 在类中声明的枚举其作用域为整个类
// 方法二:static静态变量,可以将该常量和其他静态变量放在一起,而不是存储在对象中,即可以被所有对象访问
//const int months = 12 不行的原因: 声明类只是描述了对象的形式,并没有创建对象,所以在创建对象前,该常量都是不存在的,没有存储该量的存储空间

//2.作用域内枚举: enum class name{ , , ,}  作用:防止不同枚举定义中的枚举量发生名称冲突
// eg: enum class egg{large}   enum class t-shirt(large)
// 此时 egg a = egg::large 和 t-shirt b = t-shirt::large不会发生冲突

抽象数据类型(模拟栈的内存管理)
//class.h

#pragma once

#ifndef _CLASS_H_
#define _CLASS_H_

typedef unsigned long Item; //使得Item相当于unsigned long

class Stack
{
private:
	enum {MAX =10};//类常量,类的所有对象都可以使用
	Item items[MAX]; //items数组相当于栈
	int top;//标记当前数据在栈中的位置
public:
	Stack(); //构造函数
	bool isempty() const;
	bool isfull() const;
	bool push(const Item& item);
	bool pop(Item& item);
};

#endif

//fuc.cpp


#include 
#include"class.h"
using namespace std;

Stack::Stack()
{
	top = 0; //将标定参数初始在数组(即栈)的最底端
}

bool Stack::isempty() const
{
	return top == 0;  //返回的结果是top==0的真假
}

bool Stack::isfull() const
{
	return top == MAX; //返回的结构是top==MAX的真假
}


// pop 和 push 过程应当满足 先进后出 原则
bool Stack::push(const Item& item)   //参数相当于 const unsigned long & item
{
	//实现逻辑:如果top0,即还未将所有的变量清除,且当前的top处应当是一个空位,所以用前自减移到更上一位,并将该位置的值赋给item,则可以在主函数中输出被pop出栈的值
	if (top >0)
	{
		item = items[--top];
		return true;
	}
	else
	{
		return false;
	}
}

//利用返回值为bool类型的函数来实现


// main.cpp

#include
#include    // 用到了toupper():将小写字母改为大写字符(大写字母仍为大写字母) 以及 isalpha():判断是否位字母  两个函数
#include"class.h"
using namespace std;


int main()
{
	Stack st;
	char ch;
	Item po;
	cout << "Please enter A to add a purchase order" << endl;
	cout << "P to process a PO,or Q to quit" << endl;
	while (cin >> ch && toupper(ch) != 'Q')
	{
		while (cin.get() != 'n')
		{
			continue;
		}
		if (!isalpha(ch))
		{
			cout << 'a';
			continue;
		}
		switch (ch)
		{
		case 'A':
		case 'a':
			cout << "Enter a PO number to add: ";
			cin >> po;
			if (st.isfull())
			{
				cout << "Stack already full" << endl;
			}	
			else
			{
				st.push(po);
			}		
			break;
		case 'P':
		case 'p':
			if (st.isempty())
			{
				cout << "Stack already empty" << endl;
			}
			else
			{
				st.pop(po);
				cout << po << " has been already popped" << endl;
			}
			break;

		default:
			cout << "wrong input,please input while following the instruction again" << endl;
		}
		
		cout << "Please enter A to add a purchase order" << endl;
		cout << "P to process a PO,or Q to quit" << endl;
	}
	cout << "Bye~" << endl;


	system("pause");
	return 0;
}

//很久没用swtich语句了,有点生疏,整理了一下switch语句的一些用法和注意点

//参考文章:(1条消息) C++中的case标签_舒夜无痕的博客-CSDN博客_c++ case

//1.case关键字和它的对应值一起称为case标签,且case标签必须是整型常量                                         整型常量:

        (1)C++中的const int,注意仅限于C++中的const,C中的const是只读变量,不是常量;

        (2)单个字符,如case 'a': 是合法的,因为文字字符是常量,被转成ASCII码,为整型;

        (3)使用#define定义的整型,#define定义的一般为常量,比如#define pi 3.14,但是也必须是整型才可以;

        (4)使用enum定义的枚举成员。因为枚举成员是const的,且为整型。如果不手动指定枚举值,则默认枚举值为从0开始,依次加1。

//2.case标签只能在开关内使用:即case语句要写在switch的{ }中

//3.若不想case中的所有情况都被调用,则一定要记住每种情况的代码段末尾要加上break;

//4.default是用于上述所有case情况的值均不满足,跳转运行的代码

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1031747.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号