
c++当中类是一个数据类型,封装了数据以及操作。个人理解:c++类就是对事物的的抽象,c++万物即可为类,和c语言的结构体一样,是一系列事物的共同属性和行为
对象就是类的具体化(实例化)。举个例子:女朋友类
抽象的过程:类 属性:年龄,身份证号,姓名,三围,身高,体重…
行为:购物,看电影…
- 具体的过程:对象 属性的具体化
int float double char string…自定类型 ———–>c++类中数据成员class 类名
{public:protected:private:
};
权限限定词:
权限限定是用来限定类外对象对于类中数据的访问权限,类外只能访问public属性,c++类中默认属性是private属性,注:c++类中毫无权限可言,可以互相访问,结构体默认属性是public,权限限定词在结构体中也适用
c++类中的一般数据和行为只能通过对象来访问。对象主要有的有以下几种形式:
- 普通对象
- 对象数组:c++一般很少用到对象数组
- 对象指针
- 普通指针指向对象
- 指针做动态内存申请
- 综上访问的数据有以下两种访问:
- 对象.成员
- 对象指针->成员
- c++允许在类中直接给数据做初始化
- c++提供公有接口传参的方式做数据的访问
- c++提供公有接口返回数据的引用做数据的访问
注意:c++中有类了,结构体没用了?当然不是,为了访问数据方便,不需要考虑权限问题,可以选择使用结构体
#include
using namespace std;
class Node
{
public:void initNode(int data){m_data = data;next = nullptr;}void printNode(){cout << m_data << " ";}Node*& getNext() { return next; }
protected:int m_data;Node* next;
};
class List
{
public:void createHead() //创建表头{headNode = new Node; //new一个表头节点headNode->getNext() = nullptr; //把表头节点next指针置为空}void insertNode(int data) //插入节点{Node* newNode = new Node; //new一个新节点newNode->initNode(data); //初始化新节点数据newNode->getNext() = headNode->getNext(); headNode->getNext() = newNode;}void printList(){Node* pmove = headNode->getNext(); //从第二个节点开始while( pmove != NULL ) {pmove->printNode(); //调用打印节点函数pmove = pmove->getNext(); //指针往下走}cout << endl;}
protected:Node* headNode;
};int main()
{List list;list.createHead();for (int i = 0; i < 10; i++){list.insertNode(i + 2);}list.printList();return 0;
}
#include
#include
using namespace std;
struct Node
{int data;Node* next;void print(){cout << data << " ";}void initNode(int Data){data = Data;next = nullptr;}
};
class List
{
public:void push_front(int data); //头插法void push_back(int data); //尾插法void pop_front(); //头部删除void pop_back(); //尾部删除//万金油函数int size() { return m_size; }bool empty() { return m_size == 0; }//访问头结点和尾结点数据int front() { return frontNode->data; }int tail() { return tailNode->data; }void print();
protected://万精油属性int m_size = 0; //记录当前链表中的节点个数Node* frontNode = nullptr; //起标识作用,第一个节点Node* tailNode = nullptr; //最后一个节点
};void List::push_front(int data)
{Node* newNode = new Node;newNode->initNode(data);if ( m_size==0 ) //只有一个节点的时候,头就是尾,尾就是头{tailNode = newNode;}else{newNode->next = frontNode;}frontNode = newNode;m_size++;
}void List::push_back(int data)
{Node* newNode = new Node;newNode->initNode(data);if(m_size==0){frontNode = newNode;}else{tailNode->next = newNode;}tailNode = newNode;m_size++;
}void List::pop_front()
{if ( empty())return;Node* nextNode = frontNode->next;delete frontNode;if (nextNode == nullptr){tailNode = nullptr;}frontNode = nextNode;m_size--;
}void List::pop_back(){if (empty()){return;}else if ( frontNode == tailNode ){pop_front();}else{Node* curNode = frontNode;while (curNode->next != tailNode){curNode = curNode->next;}delete tailNode;curNode->next = nullptr;tailNode = curNode;m_size--;}
}void List::print()
{Node* curNode = frontNode;while (curNode != NULL) {curNode->print();curNode = curNode->next;}cout << endl;
}int main()
{List list;for (int i = 0; i < 10; i++){list.push_front(i);}list.print();list.push_back(99);list.print();list.pop_front();list.print();list.pop_back();list.print();//边打印边删while (!list.empty()){cout << list.front() << " ";list.pop_front();}//了解,这些函数都有现成的std::list mylist;mylist.push_back(45);mylist.pop_back();return 0;
}
上一篇:组件的概念