c++類和對象基本概念
什么是類?
一系列事物的抽象,對于c++而言,萬事萬物都可以是類。
類包括:屬性+行為
屬性:事物特征->數(shù)據(jù)類型描述;
行為事物的操作->函數(shù)描述;
什么是對象?
類的具體化,類的實例化,抽象->具象;
類的特點(diǎn):封裝、繼承、派生、多態(tài)。
類的定義
創(chuàng)建方法:class
class 類名{
//權(quán)限限定詞
public:
protected://保護(hù)屬性
private://當(dāng)不做繼承時,數(shù)據(jù)成員寫成私有屬性
};//一定有一個分號
權(quán)限限定詞作用:類外只能訪問public屬性下面的東西(接口),類外訪問類中數(shù)據(jù)只能通過對象訪問(static成員除外)
若類中申明類外實現(xiàn),需要用類名限定(告訴別人函數(shù)屬于哪個類的)
沒有寫在權(quán)限限定詞下的屬性,默認(rèn)私有屬性。
權(quán)限限定詞只是限定類外對類中的訪問,對類內(nèi)無權(quán)限之分。
結(jié)構(gòu)體中默認(rèn)屬性是共有屬性
創(chuàng)建對象
普通對象、對象數(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() { //沒有寫構(gòu)造函數(shù)的情況下,和C語言的創(chuàng)建方式是一樣的 MM mm; mm.print(); //沒有初始化數(shù)據(jù) MM mmArray[4]; //一般很少用對象數(shù)組 //mmArray[0]----mmArray[3] //數(shù)組: 多個變量名有規(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ù) //類中實現(xiàn)函數(shù) void printData() { cout << m_name << "\t" << m_age << endl; } //為了訪問不能訪問的部分,通常提供一些接口 void initData(string name, int age); protected: //保護(hù)屬性 //數(shù)據(jù)成員 string m_name; private: //當(dāng)前類不做繼承處理,數(shù)據(jù)成員寫成私有屬性 int m_age; }; //類外實現(xiàn)類中函數(shù),需要類名限定,告訴別人這個函數(shù)是哪里來的 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(); //類外只能訪問public //Lisa.m_name = "Lisa"; //Lisa.m_age = 18; GirlFriend mm; mm.initData("MM", 29); mm.printData(); //mm.print(); --->不能訪問私有屬性 return 0; }
成員訪問(初始化)
1.提供共有接口來初始化數(shù)據(jù)(傳參)
2.通過提供共有接口返回值方式初始化數(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; //不做初始化是一個垃圾值 }; 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對比:
#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; //用一個指針表示整個表頭 }; void testList1() { List* pList = new List; //C++第一步:創(chuàng)建對象 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)建對象 pList->createList(); pList->insertData(10); pList->insertData(20); pList->printList(); } int main() { //testListC(); testList2(); return 0; }
總結(jié)
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
VC++實現(xiàn)文件與應(yīng)用程序關(guān)聯(lián)的方法(注冊表修改)
這篇文章主要介紹了VC++實現(xiàn)文件與應(yīng)用程序關(guān)聯(lián)的方法,涉及VC++針對注冊表的相關(guān)操作技巧,需要的朋友可以參考下2016-08-08