c++类和对象

简介: c++类和对象

de6131228f8e42c3b4f1c783bc7c87d7.png

目录

1.什么是类

c++当中类是一个数据类型,封装了数据以及操作。个人理解:c++类就是对事物的的抽象,c++万物即可为类,和c语言的结构体一样,是一系列事物的共同属性和行为

2.什么是对象

  • 对象就是类的具体化(实例化)。举个例子:女朋友
  • 抽象的过程:类 属性:年龄,身份证号,姓名,三围,身高,体重…

行为:购物,看电影…

  • 具体的过程:对象 属性的具体化

3.两个重要概念

  1. 属性:用的数据描述属性:int float double char string…自定类型 ———–>c++类中数据成员
  2. 行为:用函数描述行为 ———>c++类中叫做成员函数

4.类和对象初识

1.类的创建

class 类名

{

 public:

   protected:

   private:

};

2.类的权限问题

权限限定词:

  • public:公有属性
  • 函数是public属性,通常叫做公有接口
  • protected:保护属性
  • private:私有属性

权限限定是用来限定类外对象对于类中数据的访问权限,类外只能访问public属性,c++类中默认属性是private属性,注:c++类中毫无权限可言,可以互相访问结构体默认属性是public,权限限定词在结构体中也适用

3.对象访问数据

c++类中的一般数据和行为只能通过对象来访问。对象主要有的有以下几种形式:

  • 普通对象
  • 对象数组:c++一般很少用到对象数组
  • 对象指针
  • 普通指针指向对象
  • 指针做动态内存申请
  • 综上访问的数据有以下两种访问:
  • 对象.成员
  • 对象指针->成员

5.c++类中数据访问

  • c++允许在类中直接给数据做初始化
  • c++提供公有接口传参的方式做数据的访问
  • c++提供公有接口返回数据的引用做数据的访问

注意:c++中有类了,结构体没用了?当然不是,为了访问数据方便,不需要考虑权限问题,可以选择使用结构体

6.有头链表

#include<iostream>

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;

}

7.无头链表

#include<iostream>

#include<new>

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<int> mylist;

mylist.push_back(45);

mylist.pop_back();

return 0;

}

相关文章
|
6天前
|
C++ 容器
C++中自定义结构体或类作为关联容器的键
C++中自定义结构体或类作为关联容器的键
13 0
|
5天前
|
存储 安全 编译器
【C++】类和对象(下)
【C++】类和对象(下)
【C++】类和对象(下)
|
4天前
|
编译器 C++
virtual类的使用方法问题之C++类中的非静态数据成员是进行内存对齐的如何解决
virtual类的使用方法问题之C++类中的非静态数据成员是进行内存对齐的如何解决
|
4天前
|
编译器 C++
virtual类的使用方法问题之静态和非静态函数成员在C++对象模型中存放如何解决
virtual类的使用方法问题之静态和非静态函数成员在C++对象模型中存放如何解决
|
4天前
|
编译器 C++
virtual类的使用方法问题之在C++中获取对象的vptr(虚拟表指针)如何解决
virtual类的使用方法问题之在C++中获取对象的vptr(虚拟表指针)如何解决
|
5天前
|
编译器 C++
【C++】类和对象(中)
【C++】类和对象(中)
|
5天前
|
存储 编译器 程序员
【C++】类和对象(上)
【C++】类和对象(上)
|
6天前
|
存储 编译器 C++
【C++】类和对象(下)
【C++】类和对象(下)
|
6天前
|
存储 算法 搜索推荐
【C++】类的默认成员函数
【C++】类的默认成员函数
|
11天前
|
C++
C++ --> 类和对象(三)
C++ --> 类和对象(三)
26 9