欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

c++類和對(duì)象基本概念

 更新時(shí)間:2021年11月24日 10:47:48   作者:Huang?Jason  
這篇文章主要為大家介紹了c++類和對(duì)象,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助

什么是類?

一系列事物的抽象,對(duì)于c++而言,萬(wàn)事萬(wàn)物都可以是類。

類包括:屬性+行為

屬性:事物特征->數(shù)據(jù)類型描述;

行為事物的操作->函數(shù)描述;

什么是對(duì)象?

類的具體化,類的實(shí)例化,抽象->具象;

類的特點(diǎn):封裝、繼承、派生、多態(tài)。

類的定義

創(chuàng)建方法:class

class 類名{

//權(quán)限限定詞

public:

protected://保護(hù)屬性

private://當(dāng)不做繼承時(shí),數(shù)據(jù)成員寫(xiě)成私有屬性

};//一定有一個(gè)分號(hào)

權(quán)限限定詞作用:類外只能訪問(wèn)public屬性下面的東西(接口),類外訪問(wèn)類中數(shù)據(jù)只能通過(guò)對(duì)象訪問(wèn)(static成員除外)

若類中申明類外實(shí)現(xiàn),需要用類名限定(告訴別人函數(shù)屬于哪個(gè)類的)

沒(méi)有寫(xiě)在權(quán)限限定詞下的屬性,默認(rèn)私有屬性。

權(quán)限限定詞只是限定類外對(duì)類中的訪問(wèn),對(duì)類內(nèi)無(wú)權(quán)限之分。

結(jié)構(gòu)體中默認(rèn)屬性是共有屬性

創(chuàng)建對(duì)象

普通對(duì)象、對(duì)象數(shù)組(使用較少)

#include <iostream>
#include <string>
using namespace std;
class MM 
{
public:
	void print() 
	{
		cout << name << "\t" << age << endl;
	}
	void initData(string nname,int nage) 
	{
		name = nname;
		age = nage;
	}
protected:
	//新標(biāo)準(zhǔn),可以在類中給數(shù)據(jù)直接初始化
	string name="默認(rèn)值";
	int age=0;
};
int main() 
{
	//沒(méi)有寫(xiě)構(gòu)造函數(shù)的情況下,和C語(yǔ)言的創(chuàng)建方式是一樣的
	MM  mm;
	mm.print();			//沒(méi)有初始化數(shù)據(jù)
	MM mmArray[4];		//一般很少用對(duì)象數(shù)組
	//mmArray[0]----mmArray[3]
	//數(shù)組: 多個(gè)變量名有規(guī)律,內(nèi)存連續(xù)的變量的集合
	for (int i = 0; i < 4; i++) 
	{
		mmArray[i].initData(string("name") + to_string(i), i + 19);
		mmArray[i].print();
	}
	MM* p = new MM;
	p->initData("張三", 18);
	p->print();
	delete p;
	p = nullptr;
	return 0;
}
#include <iostream>
#include <string>
using namespace std;
class GirlFriend
{
	void print() 
	{
		cout << "不在限定詞下的屬性" << endl;
		cout << "默認(rèn)為私有屬性" << endl;
	}
public:
	//共有屬性
	//成員函數(shù)
	//類中實(shí)現(xiàn)函數(shù)
	void  printData() 
	{
		cout << m_name << "\t" << m_age << endl;
	}
	//為了訪問(wèn)不能訪問(wèn)的部分,通常提供一些接口
	void  initData(string name, int age);
protected:
	//保護(hù)屬性
	//數(shù)據(jù)成員
	string m_name;
private:
	//當(dāng)前類不做繼承處理,數(shù)據(jù)成員寫(xiě)成私有屬性
	int m_age;
};
//類外實(shí)現(xiàn)類中函數(shù),需要類名限定,告訴別人這個(gè)函數(shù)是哪里來(lái)的
void GirlFriend::initData(string name,int age)  
{
	//Lisa.initData("Lisa", 19);  name="Lisa" age=19
	m_name = name;    //Lisa.m_name=Lisa
	m_age = age;	  //Lisa.m_age=19;
	//mm.initData("MM", 29);   name="MM" age=29
	//mm.m_name=MM;
	//mm.age=29
}
struct MM 
{
	int num;   //默認(rèn)屬性是公有屬性
protected:
	string name;
private:
	int age;
};
void testMM() 
{
	//MM mm = { 1001,"name",28 };
	MM mm;
	mm.num = 103;
	//mm.name = "Ilove";
	//mm.age = 13;
}
int main()
{
	GirlFriend  Lisa;
	Lisa.initData("Lisa", 19);
	Lisa.printData();
	//類外只能訪問(wèn)public
	//Lisa.m_name = "Lisa";
	//Lisa.m_age = 18;
	GirlFriend mm;
	mm.initData("MM", 29);
	mm.printData();
	//mm.print();  --->不能訪問(wèn)私有屬性
 
	return 0;
}

成員訪問(wèn)(初始化)

1.提供共有接口來(lái)初始化數(shù)據(jù)(傳參)

2.通過(guò)提供共有接口返回值方式初始化數(shù)據(jù)

#include <iostream>
#include <string>
using namespace std;
class MM
{
public:
	//傳參
	void initData(string name, int age)
	{
		m_name = name;
		m_age = age;
	}
	//返回引用
	string& getName() 
	{
		return m_name;
	}
	int& getAge() 
	{
		return m_age;
	}
	void print() 
	{
		cout << m_name << "\t" << m_age << endl;
	}
protected:
	//默認(rèn)初始化
	string m_name="默認(rèn)值";
	int m_age=0;
	//不做初始化是一個(gè)垃圾值
};
 
int main() 
{
	MM girl;
	girl.initData("girl", 19);
	girl.print();
 
	MM mm;
	mm.getName() = "mm";
	mm.getAge() = 18;
	mm.print();
 
	MM boy;
	boy.print();
 
	return 0;
}

c++有頭鏈表與c對(duì)比:

#include <iostream>
#include <string>
using namespace std;
#if 0
struct Node
{
	int data;
	struct N ode* next;
};
struct Node* createList() 
{
	Node* headNode = new Node;
	headNode->next = nullptr;
	return headNode;
}
struct Node* createNode(int data) 
{
	Node* newNode = new Node;
	newNode->data = data;
	newNode->next = nullptr;
	return newNode;
}
void insertData(Node* headNode, int data) 
{
	Node* newNode = createNode(data);
	newNode->next = headNode->next;
	headNode->next = newNode;
}
void printList(Node* headNode) 
{
	Node* pMove = headNode->next;
	while (pMove != nullptr) 
	{
		cout << pMove->data<<" ";
		pMove = pMove->next;
	}
	cout << endl;
}
void testListC() 
{
	Node* list = createList();
	insertData(list, 10);
	insertData(list, 20);
	printList(list);
}
#endif
 
#if 0
struct Node 
{
	int data;
	Node* next;
};
class List 
{
public:
	void createList() 
	{
		headNode = new Node;
		headNode->next = nullptr;
	}
	void insertData(int data) 
	{
		Node* newNode = new Node;
		newNode->data = data;
		newNode->next = nullptr;
 
		newNode->next = headNode->next;
		headNode->next = newNode;
	}
	void printList() 
	{
		Node* pMove = headNode->next;
		while (pMove != nullptr) 
		{
			cout << pMove->data << " ";
			pMove = pMove->next;
		}
		cout << endl;
	}
protected:
	Node* headNode;		//用一個(gè)指針表示整個(gè)表頭
};
void testList1()
{
	List* pList = new List;		//C++第一步:創(chuàng)建對(duì)象
	pList->insertData(10);
	pList->insertData(20);
	pList->printList();
}
#endif 
class Node 
{
public:
	Node*& getNext() 
	{
		return next;
	}
	int& getData() 
	{
		return data;
	}
protected:
	int data;
	Node* next;
};
class List 
{
public:
	void createList() 
	{
		headNode = new Node;
		headNode->getNext() = nullptr;
	}
	void insertData(int data) 
	{
		Node* newNode = new Node;
		newNode->getNext() = nullptr;
		newNode->getData() = data;
 
		newNode->getNext() = headNode->getNext();
		headNode->getNext() = newNode;
	}
	void printList() 
	{
		Node* pMove = headNode->getNext();
		while (pMove != nullptr) 
		{
			cout << pMove->getData() << "\t";
			pMove = pMove->getNext();
		}
		cout << endl;
	}
protected:
	Node* headNode;
};
void testList2()
{
	//List list;
    //list.insertList(10);
    List* pList = new List;		//C++第一步:創(chuàng)建對(duì)象
	pList->createList();
	pList->insertData(10);
	pList->insertData(20);
	pList->printList();
}
int main() 
{
	//testListC();
	testList2();
	return 0;
}

總結(jié)

本篇文章就到這里了,希望能夠給你帶來(lái)幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!

相關(guān)文章

  • C語(yǔ)言基于EasyX庫(kù)實(shí)現(xiàn)有圖形界面時(shí)鐘

    C語(yǔ)言基于EasyX庫(kù)實(shí)現(xiàn)有圖形界面時(shí)鐘

    這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言基于EasyX庫(kù)實(shí)現(xiàn)有圖形界面時(shí)鐘,獲得本地時(shí)間,輸出文字,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • C++中的explicit關(guān)鍵字實(shí)例淺析

    C++中的explicit關(guān)鍵字實(shí)例淺析

    在C++程序中很少有人去使用explicit關(guān)鍵字,不可否認(rèn),在平時(shí)的實(shí)踐中確實(shí)很少能用的上,再說(shuō)C++的功能強(qiáng)大,往往一個(gè)問(wèn)題可以利用好幾種C++特性去解決。接下來(lái)給大家介紹 C++中的explicit關(guān)鍵字,需要的朋友可以參考下
    2017-03-03
  • C++中自定義sleep、條件變量sleep實(shí)例

    C++中自定義sleep、條件變量sleep實(shí)例

    這篇文章主要介紹了C++中自定義sleep、條件變量sleep實(shí)例,本文直接給出實(shí)例代碼并講解了功能作用和使用方法,需要的朋友可以參考下
    2015-03-03
  • C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單彈跳球游戲

    C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單彈跳球游戲

    這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單彈跳球游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-03-03
  • C++實(shí)現(xiàn)LeetCode(118.楊輝三角)

    C++實(shí)現(xiàn)LeetCode(118.楊輝三角)

    這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(118.楊輝三角),本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-07-07
  • C語(yǔ)言異或校驗(yàn)算法的項(xiàng)目實(shí)現(xiàn)

    C語(yǔ)言異或校驗(yàn)算法的項(xiàng)目實(shí)現(xiàn)

    異或校驗(yàn)算法(XOR校驗(yàn))是一種簡(jiǎn)單的校驗(yàn)算法,用于檢測(cè)數(shù)據(jù)在傳輸或存儲(chǔ)過(guò)程中是否發(fā)生了錯(cuò)誤,本文主要介紹了C語(yǔ)言異或校驗(yàn)算法的項(xiàng)目實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-08-08
  • Qt中樹(shù)形控件Tree Widget的使用方法匯總

    Qt中樹(shù)形控件Tree Widget的使用方法匯總

    最近小編在研究Tree Widget樹(shù)形控件的相關(guān)知識(shí),這種控件其實(shí)有時(shí)還是很有用處的,我主要利用的是帶有復(fù)選框的樹(shù)形控件,下面通過(guò)實(shí)例代碼給大家介紹下Qt中樹(shù)形控件Tree Widget的一些使用方法,感興趣的朋友一起學(xué)習(xí)吧
    2021-11-11
  • Matlab實(shí)現(xiàn)多子圖同步調(diào)整視角

    Matlab實(shí)現(xiàn)多子圖同步調(diào)整視角

    這篇文章主要為大家介紹了如何利用Matlab實(shí)現(xiàn)多子圖同步調(diào)整視角,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Matlab有一定幫助,需要的可以參考一下
    2022-03-03
  • C++類的返回值是*this的成員函數(shù)問(wèn)題

    C++類的返回值是*this的成員函數(shù)問(wèn)題

    這篇文章主要介紹了C++類的返回值是*this的成員函數(shù)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • VC++實(shí)現(xiàn)文件與應(yīng)用程序關(guān)聯(lián)的方法(注冊(cè)表修改)

    VC++實(shí)現(xiàn)文件與應(yīng)用程序關(guān)聯(lián)的方法(注冊(cè)表修改)

    這篇文章主要介紹了VC++實(shí)現(xiàn)文件與應(yīng)用程序關(guān)聯(lián)的方法,涉及VC++針對(duì)注冊(cè)表的相關(guān)操作技巧,需要的朋友可以參考下
    2016-08-08

最新評(píng)論