C++ 类的设计

一、c++类的设计

       是一种将抽象转换为用户定义类型的C++工具,它将数据表示操纵数据的方法组合成一个整洁的包。

语法:

       其中"class类名"称为类头。花括号中的部分称为类体,类体中定义了类成员表。

       在C++中,类是一种数据类型。客观事物是复杂的,要描述它必须从多方面进行,也就是用不同的数据类型来描述不同的方面。如商场中的商品可以这样描述: 

          商品名称(char), 该商品数量(int),该商品单价(float),该商品总价(float)。

class CGoods
{
private:
	char Name[LEN];
	int Amount;
	float Price;
	float Total_value;
};

        关键字class是数据类型说明符,指出下面说明的是类型。标识符CGoods是商品这个类的类型名。花括号中是构成类体的一系列的成员,关键字public是一种访问限定符。访问限定符(access specifier)有3三种: public (公共的) ,private (私有的) 和protected (保护的)。

        public说明的成员能从外部进行访问,private和protect说明的成员不能从外部进行访问。每种说明符可在类体中使用多次。它们的作用域是从该说明符出现开始到下一个说明符之前或类体结束之前结束。
        如果在类体起始点无访问说明符,系统默认定义为私有( private )。访问说明符private (私有
的)和protected (保护的) 体现了类具有封装性( Encapsulation)。

二、成员函数

class CGoods
{
private:
    char Name[21];
    int Amount;
    float Price;
    float Total;
public:
    void RegisterGoods(char[], int, float);            //输入数据
    void GetName(char[]);                                  //读取商品名
    void GetAmount(void);                                 //读取商品数量
    float GetPrice(void);                                     //读取商品单价
    void CountTotal(void);                                  //计算商品总价值
};


 

        类是一种数据类型,定义时系统不为类分配存储空间,所以不能对类的数据成员初始化。类中的任何数据成员也不能使用关键字extern、auto或register限定其存储类型。 

成员函数可以直接使用类定义中的任一成员, 可以处理数据成员,也可调用函数成员。成员函数的定义:
1、前面只对成员函数作了一个声明(函数的原型), 并没有对函数进行定义。
2、函数定义通常在类的说明之后进行,其格式如下:
      回值类型类名::函数名(参数表)

如下为一个商品类型的完整设计:

class CGoods
{
private:
	char Name[LEN];
	int Amount;
	float Price;
	float Total_value;
public:
	void RegisterGoods(const char*, int, float);
	//void RegisterGoods(CGoods* this,const char*, int, float);
	void CountTotal(void);
	//void CountTotal(CGoods* const this);
	void GetName(char*,int);
	int GetAmount(void);
	float GetPrice(void);
	//float GetTotal_value(CGoods* const this)
	float GetTotal_value(void)
	{
		return this->Total_value;
	}

};
//void CGoods::RegisterGoods(CGoods *this,const char*name, int amount, float price)
void CGoods::RegisterGoods(const char*name, int amount, float price)
{
	strcpy_s(this->Name, LEN, name);
	this->Amount = amount;
	this->Price = price;
}
//void CGoods::CountTotal(CGoods*this)
void CGoods::CountTotal(void)
{
	this->Total_value = this->Amount * this->Price;
}
void CGoods::GetName(char*name,int n)
{
	strcpy_s(name,n, Name);
}
int CGoods::GetAmount()
{
	return Amount;
}
float CGoods::GetPrice()
{
	return Price;
}
//float CGoods::GetTotal(CGoods *this)

三、对象的创建与使用

        对象是类的实例 (instance) 。声明一种数据类型只是告诉编译系统该数据类型的构造,并没有预定内存。类只是一个样板(图纸),以此样板可以在内存中开辟出同样结构的实例--对象。

int main()
{
	CGoods tea;
	CGoods book;
	tea.RegisterGoods("black_tea", 12, 560);                    //调用的时候传地址
	//RegisterGoods(&tea,"black_tea", 12, 560);
	tea.CountTotal();
	//CountTotal(&tea);
	book.RegisterGoods("Thinking In C++", 20, 128);
	//RegisterGoods("&book,Thinking In C++", 20, 128);
	book.CountTotal();
	//CountTotal(&book);


	void fun(int a);

	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值