1. 程式人生 > >C++類的定義與物件建立

C++類的定義與物件建立

C++類內成員變數預設private,外部物件不能直接訪問

#include<iostream>
using namespace std;
#include<stdlib.h>  /*在stdlib.h標頭檔案中定義了system()函式*/

class CGoods
{
//公有public、私有private、保護protected是訪問限定符
public:   //類內不設定訪問說明時,預設private
	char  Name[21];
	int   Amount;
	float Price;
	float Total_value;
};

void main()
{
	CGoods cd;   //物件(類定義的變數),類例項化出物件
	//類的物件能在外部直接訪問類內的公有成員
	strcpy(cd.Name,"C++");
	cd.Amount=10;   //cd是物件,.是成員訪問符
	cd.Price=12;
	cd.Total_value=cd.Amount*cd.Price;
	system("PAUSE");  //system函式可以使命令列視窗執行後不立即退出  
}

C++類的公有函式的實現有兩種:1)在類的內部實現 2)在類外實現

並且類的物件可以通過公有函式訪問類的私有成員變數

#include<iostream>
using namespace std;
#include<stdlib.h>  /*在stdlib.h標頭檔案中定義了system()函式*/
class CGoods
{
private:
	char  Name[21];
	int   Amount;
	float Price;
	float Total_value;
public:   //公有方法針對私有資料進行操作
	//C++類的公有函式的實現:1)在類的內部實現(行內函數)
	void RegisterGoods(char name[],int amount,float price)   //輸入資料
	{
		strcpy(Name,name);   //拷貝名字
		Amount=amount;
		Price=price;
	}
	void CountTotal(void)   //計算商品總價值
	{
		Total_value=Price*Amount;
	}
	void GetName(char name[])   //讀取商品名
	{
		strcpy(name,Name);
	}
	int GetAmount(void)   //讀取商品數量
	{
		return Amount;
	}
	float GetPrice(void)   //讀取商品單價
	{
		return Price;
	}
	float GetTotal_value(void)
	{
		return Total_value;
	}
};

void main()
{
	CGoods c1,c2;   
	c1.RegisterGoods("C++",10,45);
	c2.RegisterGoods("Java",5,43);
	cout<<"c1 amount:"<<c1.GetAmount()<<endl;   //物件c1,c2必須通過類的公有方法操作類的私有資料
	cout<<"c2 amount:"<<c2.GetAmount()<<endl;   
	system("PAUSE");  //system函式可以使命令列視窗執行後不立即退出  
}

#include<iostream>
using namespace std;
#include<stdlib.h>  /*在stdlib.h標頭檔案中定義了system()函式*/

class CGoods
{
private:
	char  Name[21];
	int   Amount;
	float Price;
	float Total_value;
public: //公有方法針對私有資料進行操作
	//C++類的公有函式的實現:2)在類外實現
	void RegisterGoods(char[],int,float);   //輸入資料
	void CountTotal(void);   //計算商品總價值
	void GetName(char name[]);   //讀取商品名
	int GetAmount(void);   //讀取商品數量
	float GetPrice(void);   //讀取商品單價
	float GetTotal_value(void);
};

void CGoods::RegisterGoods(char name[],int amount,float price)   
//::是類的作用域解析符,說明定義的函式是類的成員函式,而不是全域性函式
{
	strcpy(Name,name);
	Amount=amount;
	Price=price;
}

void main()
{
	CGoods c1;
	c1.RegisterGoods("C++",10,45);
	system("PAUSE");
}

此外,為了隱藏類內方法的實現,想把類的方法做成庫檔案
//classfunction.h內容
#include<iostream>
using namespace std;

//標頭檔案內放類的宣告
class CGoods
{
private:
	char  Name[21];
	int   Amount;
	float Price;
	float Total_value;
public:   //公有方法針對私有資料進行操作
	//C++類的公有函式的實現:2)在類外實現
	void RegisterGoods(char[],int,float);   //輸入資料
	void CountTotal(void);   //計算商品總價值
	void GetName(char name[]);   //讀取商品名
	int GetAmount(void);   //讀取商品數量
	float GetPrice(void);   //讀取商品單價
	float GetTotal_value(void);
};
//classfunction.cpp內容
#include<stdlib.h>  /*在stdlib.h標頭檔案中定義了system()函式*/
#include"classfunction.h"   //引入標頭檔案
//原始檔對類的實現
//返回值 類名::函式名(引數)
void CGoods::RegisterGoods(char name[],int amount,float price)   
//::是類的作用域解析符,說明定義的函式是類的成員函式,而不是全域性函式
{
	strcpy(Name,name);
	Amount=amount;
	Price=price;
}
void CGoods::CountTotal(void)
{
	Total_value;
}
void CGoods::GetName(char name[])
{
	strcpy(name,Name);
}
int CGoods::GetAmount(void)
{
	return Amount;
}
float CGoods::GetPrice(void)
{
	return Price;
}
float CGoods::GetTotal_value(void)
{
	return Total_value;
}

//main內容
#include<stdlib.h>  /*在stdlib.h標頭檔案中定義了system()函式*/
#include"classfunction.h"   //引入標頭檔案
void main()
{
	CGoods c1;
	c1.RegisterGoods("C++",10,45);
	system("PAUSE");
}

不能通過main函式,直接用類名字呼叫類的成員變數,只定義了類並沒有為其“分配空間”,

必須先例項化物件之後,為資料成員分配空間,才可以通過物件對類的資料成員變數進行訪問、賦值等操作