c++初阶-------类和对象-1

简介: c++初阶-------类和对象

面向对象和面向对象的认识

C语言是面向过程的,关注的是过程,分析出求解问题的步骤,通过函数调用逐步解决问题。

就拿洗衣服来说

C++是基于面向对象的,关注的是对象,将一件事情拆分成不同的对象,靠对象之间的交互完

成。

类的引入

我们知道C语言的struct的结构体只能定义变量,不能定义函数,所以我们只能在结构体外定义,但在c++中不仅继承了C语言的struct的所有用法,还补充了C语言结构体不能定义函数的用法

#include<iostream>
#include<stdlib.h>
using std::cout;
using std::endl;
using std::cin;
struct SList
{
  int* a;
  int top;
  int capacity;
  //插入
  void Push(int x)
  {
    if (top == capacity)
    {
      //扩容
      int t = capacity > 0 ? 2 * capacity : 4;
      int* tmp = (int*)realloc(a, sizeof(int) * t);
      if (tmp == NULL)
      {
        perror("ralloc");
        return;
      }
      capacity = t;
      a = tmp;
    }
    a[top] = x;
    top++;
  }
  //初始化
  void Init()
  {
    a = NULL;
    top = 0;//栈底元素下一个
    capacity = 0;
  }
  //删除
  void Pop()
  {
    top--;
  }
};
int main()
{
  SList stack;
  stack.Init();
  stack.Push(4);
  stack.Push(3);
  stack.Push(2);
  stack.Push(1);
  stack.Pop();
  return 0;
}

类的定义

相比于struct定义类,c++更喜欢使用class来定义

class className
{
// 类体:由成员函数和成员变量组成
};  // 一定要注意后面的分号

class为定义类的关键字,ClassName为类的名字,{}中为类的主体,注意类定义结束时后面分

号不能省略。

类体中内容称为类的成员:类中的变量称为类的属性或成员变量; 类中的函数称为类的方法或者成员函数。


访问限定符

类将对象的属性与方法结合在一块,让对象更加完善,通过访问权限选择性的将其接口提供给外部的用户使用

1fd209346b015d46721bd5ce5fbff014_50d24bc667f143ee95142a5056a24f44.png

访问限定符有三种 public、protected private

#include<iostream>
#include<stdlib.h>
using std::cout;
using std::endl;
using std::cin;
class SList
{
protected:
  int* a;
  int top;
  int capacity;
public:
  //插入
  void Push(int x)
  {
    if (top == capacity)
    {
      //扩容
      int t = capacity > 0 ? 2 * capacity : 4;
      int* tmp = (int*)realloc(a, sizeof(int) * t);
      if (tmp == NULL)
      {
        perror("ralloc");
        return;
      }
      capacity = t;
      a = tmp;
    }
    a[top] = x;
    top++;
  }
  //初始化
  void Init()
  {
    a = NULL;
    top = 0;//栈底元素下一个
    capacity = 0;
  }
  //删除
  void Pop()
  {
    top--;
  }
};
int main()
{
  SList stack;
  stack.Init();
  stack.Push(4);
  stack.Push(3);
  stack.Push(2);
  stack.Push(1);
  stack.Pop();



  return 0;
}

【访问限定符说明】


  1. public修饰的成员在类外可以直接被访问
  2. protected和private修饰的成员在类外不能直接被访问(此处protected和private是类似的)
  3. 访问权限作用域从该访问限定符出现的位置开始直到下一个访问限定符出现时为止
  4. 如果后面没有访问限定符,作用域就到 } 即类结束。
  5. class的默认访问权限为private,struct为public(因为struct要兼容C)


类的方法声明和定义分离

在类中,函数的定义有两种,

一种是在类的内部进行定义,编译器可能会将其当成内联函数处理。相当于函数被inline修饰了,至于会不会被展开,由编译器决定的>。

上面的代码就是在类的内部定义的

另一种就是在头文件里面声明,然后在其他cpp文件定义

头文件

#include<iostream>
#include<stdlib.h>
using std::cout;
using std::endl;
using std::cin;
class SList
{
protected:
  int* a;
  int top;
  int capacity;
public:
  void Push(int x);
  void Init();
  //删除
  void Pop()
  {
    top--;
  }
};


cpp文件

#include"day2_1.h"
//插入
void SList::Push(int x)
{
  if (SList::top == capacity)
  {
    //扩容
    int t = capacity > 0 ? 2 * capacity : 4;
    int* tmp = (int*)realloc(a, sizeof(int) * t);
    if (tmp == NULL)
    {
      perror("ralloc");
      return;
    }
    capacity = t;
    a = tmp;
  }
  a[top] = x;
  top++;
}
//初始化
void SList::Init()
{
  a = nullptr;
  top = 0;//栈底元素下一个
  capacity = 0;
}
int main()
{
  SList stack;
  stack.Init();
  stack.Push(4);
  stack.Push(3);
  stack.Push(2);
  stack.Push(1);
  stack.Pop();
  return 0;
}


函数里面的变量会先在当前的{}里面找,找不到就会去对应的类里面找

我们在看到很多人写的c++代码里面的成员很多都会加 -

class Date
{
public:
  void Init(int year, int month, int day)
  {
    _year = year;
    _month = month;
    _day = day;
  }
private:
  int _year;
  int _month;
  int _day;
};


封装

C++中的封装是面向对象编程的特性之一,它允许将数据和操作封装在类的内部,对外部隐藏实现细节。通过使用访问修饰符(public、private、protected),可以控制类成员的访问权限,实现数据的安全性和可控性。


类的作用域

类定义了一个新的作用域,类的所有成员都在类的作用域中。在类体外定义成员时,需要使用 ::

作用域操作符指明成员属于哪个类域。


类的实例化

class Date
{
public:
  void Init(int year, int month, int day)
  {
    _year = year;
    _month = month;
    _day = day;
  }
private:
  int _year;
  int _month;
  int _day;
};


这个只叫定义类,没有实例化对象

int main()
{
  Date data;
  data.Init(2024, 1, 1);
}


当我们使用这个类来创建一个变量,就是实例化对象

一个类可以实例化很多个对象

类的大小

#include<iostream>
#include<stdlib.h>
using std::cout;
using std::endl;
using std::cin;
class Date
{
public:
  void Init(int year, int month, int day)
  {
    _year = year;
    _month = month;
    _day = day;
  }
private:
  int _year;
  int _month;
  int _day;
};
int main()
{
  Date data;
  data.Init(2024, 1, 1);
  Date data1;
  data1.Init(2024, 1, 1);
  Date data2;
  data2.Init(2024, 1, 1);
  cout << sizeof(Date);


}

可以发现类的大小是12,所以可以说明,类的方法的大小在类中不计算(内存对齐)

在反汇编中我们可以看到,不同对象的类的方法的地址是一样的,而不同对象的成员是不一样的,


c++初阶-------类和对象-2

https://developer.aliyun.com/article/1499043

相关文章
|
2天前
|
C语言 C++ 容器
C++ string类
C++ string类
8 0
|
2天前
|
C++ Linux
|
2天前
|
编译器 C++
【C++】继续学习 string类 吧
首先不得不说的是由于历史原因,string的接口多达130多个,简直冗杂… 所以学习过程中,我们只需要选取常用的,好用的来进行使用即可(有种垃圾堆里翻美食的感觉)
8 1
|
2天前
|
算法 安全 程序员
【C++】STL学习之旅——初识STL,认识string类
现在我正式开始学习STL,这让我期待好久了,一想到不用手撕链表,手搓堆栈,心里非常爽
16 0
|
2天前
|
存储 安全 测试技术
【C++】string学习 — 手搓string类项目
C++ 的 string 类是 C++ 标准库中提供的一个用于处理字符串的类。它在 C++ 的历史中扮演了重要的角色,为字符串处理提供了更加方便、高效的方法。
18 0
【C++】string学习 — 手搓string类项目
|
2天前
|
Java C++ Python
【C++从练气到飞升】06---重识类和对象(二)
【C++从练气到飞升】06---重识类和对象(二)
|
2天前
|
编译器 C++
【C++从练气到飞升】06---重识类和对象(一)
【C++从练气到飞升】06---重识类和对象(一)
|
2天前
|
设计模式 安全 算法
【C++入门到精通】特殊类的设计 | 单例模式 [ C++入门 ]
【C++入门到精通】特殊类的设计 | 单例模式 [ C++入门 ]
18 0
|
2天前
|
C语言 C++
【C++】string类(常用接口)
【C++】string类(常用接口)
21 1