友元机制与其他访问控制机制的区别与联系
导言在 C++ 面向对象编程中,访问控制机制是实现封装性的核心手段。友元机制和继承是两种主要的访问控制方式,它们既有联系又有显著区别。 一、本质区别 特性 友元机制 继承机制 关系性质 单向的 "授权" 关系 父子间的 "派生" 关系 访问目的 临时突破封装边界 实现代码复用与扩展 关系方向 非对称(A 是 B 的友元≠B 是 A 的友元) 可传递(间接继承) 生命周期 编译期静态确定 运行期动态体现(多态) 代码耦合度 低到中等(仅需声明) 高(子类依赖父类实现) 二、访问权限差异1. 友元机制的访问特点 可以直接访问所有私有成员(private)和保护成员(protected) 无需通过类的接口(public 成员)进行访问 访问权限是单向且不可传递的 示例: 12345678910111213141516171819class A {private: int x; friend class B; // B可以直接访问A的私有成员};class B...
Leetcode 0225. Implement Stack using Queues
225. Implement Stack using QueuesImplement a last-in-first-out (LIFO) stack using only two queues. The implemented stack should support all the functions of a normal stack (push, top, pop, and empty). Implement the MyStack class: void push(int x): Pushes element x to the top of the stack. int pop(): Removes the element on the top of the stack and returns it. int top(): Returns the element on the top of the stack. boolean empty(): Returns true if the stack is empty, false...
Leetcode 0232. Implement Queue using Stacks
232. Implement Queue using StacksImplement a first in first out (FIFO) queue using only two Stacks. The implemented queue should support all the functions of a normal queue (push, peek, pop, and empty). Implement the MyQueue class: void push(int x) Pushes element x to the back of the queue. int pop() Removes the element from the front of the queue and returns it. int peek() Returns the element at the front of the queue. boolean empty() Returns true if the queue is empty, false...
Leetcode 1048. Longest String Chain
1048. Longest String ChainYou are given an array of words where each word consists of lowercase English letters. wordA is a predecessor of wordB if and only if we can insert exactly one letter anywhere in wordA without changing the order of the other characters to make it equal to wordB. For example, "abc" is a predecessor of "abac", while "cba" is not a predecessor of "bcad".A word chain is a sequence of words [word1, word2, ..., wordk] with k...
C++ 友元机制深度解析:突破封装边界的艺术
一、友元机制概述C++ 面向对象编程中,封装通过public、private和protected确保数据安全。但特定场景需突破封装,友元(friend)机制由此诞生,它允许指定外部函数或类访问当前类私有、保护成员,同时维持其他实体的封装性,核心价值在于受控突破封装、支持高效数据访问及解决权限问题。 二、友元函数详解2.1 友元函数的定义与实现友元函数是类中声明为friend的非成员函数,可访问类所有成员。如Circle类中,calculateArea和isSameSize通过声明为友元,直接访问私有成员计算圆面积和比较大小。 1234567891011121314151617181920#include <iostream>#include <cmath>class Circle {private: double radius; const double PI = 3.1415926;public: Circle(double r) : radius(r) {} friend double...
C++ 文件读取技术详解:get ()、getline () 与流提取运算符
一、核心函数原型与参数解析1.1 get () 函数家族get()函数是 C++ 中最基础的字符读取工具,有多个重载版本: 1234567891011// 读取单个字符(包含空白字符)int get();// 读取字符到指定缓冲区,最多n-1个字符,遇到分隔符停止istream& get(char* s, streamsize n);// 带自定义分隔符的版本istream& get(char* s, streamsize n, char delim);// 读取字符到字符对象istream& get(char& c); 关键特性: 不会跳过空白字符(空格、制表符、换行符等) 读取失败时返回 EOF(-1) 保留分隔符在输入流中(不提取) 1.2 getline () 函数getline()专为读取完整行设计,主要有两种形式: 123456789// 从输入流读取一行到字符数组istream& getline(char* s, streamsize n);// 带自定义分隔符的版本istream&...
CONST AUTO 的应用
一、const auto迭代器的适用场景1. 只读访问容器元素时当你只需要读取容器元素而不需要修改它们时,应该使用const auto声明迭代器: 1234567891011121314151617#include <vector>#include <iostream>void printData(const std::vector<int>& data) { // 函数参数为const引用,只能使用const迭代器 for (const auto it = data.begin(); it != data.end(); ++it) { std::cout << *it << " "; // 只读访问 // *it = 100; // 编译错误,不允许修改 } std::cout << std::endl;}int main() { ...
auto 关键字在 C++ 迭代器遍历中的应用
一、auto 关键字简化迭代器遍历在 C++11 之前,迭代器遍历代码往往冗长且可读性差: 类型声明冗长,增加代码量和阅读负担 容易出现类型不匹配错误 当容器类型改变时,需要修改所有相关迭代器声明 代码不够直观,隐藏了真正的业务逻辑 C++11 引入的auto关键字彻底改变了迭代器的使用方式,让我们能够完全摆脱冗长的传统迭代器声明。auto能够自动推导迭代器类型,使代码更加简洁、可读且不易出错。 1.1 基本迭代模式1234567891011121314151617181920#include <vector>#include <iostream>int main() { std::vector<int> numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9}; // 使用auto声明迭代器 for (auto it = numbers.begin(); it != numbers.end(); ++it) { std::cout...
C++ 静态成员与单例模式:从基础到线程安全实现
一、静态成员的本质与特性1.1 静态数据成员:类级别的共享状态静态数据成员不属于任何对象实例,而是属于整个类,其内存空间在程序生命周期内唯一存在。 123456789101112131415161718192021222324252627#include <iostream>class Counter {private: // 静态数据成员声明:类内声明,类外定义 static int s_totalInstances; int m_id; // 非静态成员:每个对象拥有独立副本public: Counter() { m_id = ++s_totalInstances; // 每次创建对象自增计数 } int getId() const { return m_id; } static int getTotalInstances() { return s_totalInstances; }};//...
继承与多态
一、继承机制:代码复用的基石1.1 继承的概念与本质继承是面向对象编程中实现代码复用的核心机制,它允许一个类(派生类)获取另一个类(基类)的成员变量和成员函数。这种关系类似于现实世界中的 "is-a" 关系,例如 "狗是一种动物"。 在 C++ 中,继承通过类定义时的冒号语法实现: 12345678910111213class Animal {public: void speak() { std::cout << "The animal makes a sound." << std::endl; }};class Dog : public Animal {public: void bark() { std::cout << "The dog barks." << std::endl; }}; 在上述示例中,Dog...
C++中class与struct的异同点深度解析
一、基础语法差异1.1 访问修饰符差异 class 默认成员访问权限为 private 声明的成员变量与函数仅对类内部可见,需使用 public/protected 显式开放 struct 默认成员访问权限为 public 结构体成员对所有作用域开放,需使用 private/protected 显式限制 12345678910class MyClass { int privateVar; // 默认私有public: void publicFunc() {}};struct MyStruct { int publicVar; // 默认公有 void publicFunc() {}}; 1.2 成员函数特性 class 支持抽象方法(纯虚函数) 通过 = 0 定义的虚函数使类成为抽象类,如 virtual void func() = 0; struct 不支持抽象方法 ...
Leetcode 1023. Camelcase Matching
1023. Camelcase MatchingGiven an array of strings queries and a string pattern, return a boolean array answer where answer[i] is true if queries[i] matches pattern, and false otherwise. A query word queries[i] matches pattern if you can insert lowercase English letters into the pattern so that it equals the query. You may insert a character at any position in pattern or you may choose not to insert any characters at all. Example 1: 12345Input: queries =...
Leetcode 0825. Friends Of Appropriate Ages
825. Friends Of Appropriate AgesThere are n persons on a social media website. You are given an integer array ages where ages[i] is the age of the ith person. A Person x will not send a friend request to a person y (x != y) if any of the following conditions is true: age[y] <= 0.5 * age[x] + 7 age[y] > age[x] age[y] > 100 && age[x] < 100 Otherwise, x will send a friend request to y. Note that if x sends a request to y, y will not necessarily send a request to x. Also, a...
Leetcode 0826. Most Profit Assigning Work
826. Most Profit Assigning WorkYou have n jobs and m workers. You are given three arrays: difficulty, profit, and worker where: difficulty[i] and profit[i] are the difficulty and the profit of the ith job, and worker[j] is the ability of jth worker (i.e., the jth worker can only complete a job with difficulty at most worker[j]). Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if three workers attempt the same job that pays $1, then...
C++ 析构函数详解:原理、实现
一、析构函数基础概念1.1 析构函数的定义析构函数是 C++ 面向对象编程中用于对象销毁时进行资源清理的特殊成员函数。当对象生命周期结束时,析构函数会被自动调用,负责释放对象所占用的资源。 语法特征: 函数名与类名相同,前面加波浪号~ 没有返回值,也不指定 void 没有参数,因此无法重载 不能被显式调用(编译器自动调用) 123456789101112class MyClass {public: // 构造函数 MyClass() { std::cout << "构造函数被调用" << std::endl; } // 析构函数 ~MyClass() { std::cout << "析构函数被调用" << std::endl; }}; 1.2...
C++ 左值与右值:语义解析与实战应用
一、左值与右值的核心定义在 C++ 中,表达式根据其特性被分为左值(lvalue)和右值(rvalue),这一分类直接影响着变量的存储、引用绑定和资源管理。根据 C++17 标准( IO/IEC 14882:2017),左值是指 "可以取地址且具有身份 (identity) 的表达式",而右值则是 "非左值的表达式",通常是临时的、不具有持久身份的对象。 1.1 左值的核心特征左值具有以下关键特性: 可以通过&运算符获取地址S 具有持久性,在表达式结束后仍然存在 可以出现在赋值运算符的左侧 1234int x = 10; // x是左值int* ptr = &x; // 合法,左值可以取地址x = 20; // 合法,左值可以出现在赋值左侧 1.2 右值的核心特征右值具有以下关键特性: 不能通过&运算符获取地址 临时性,通常在表达式结束后销毁 不能出现在赋值运算符的左侧 1234int y = x + 5; // x + 5是右值// int* ptr = &(x +...
C++ 变量作用域与存储持续度完全解析
导言:变量类型的四维属性体系在 C++ 中,每个变量都具有四个核心属性,这些属性共同决定了变量的行为特征: 作用域 (Scope):变量在程序中可见的区域 存储持续度 (Storage Duration):变量在内存中存在的时间 链接属性 (Linkage):变量在不同编译单元间的可见性 生命周期 (Lifetime):变量从创建到销毁的时间段 一、作用域类型详解1.1 块作用域 (Block Scope)块作用域是由花括号{}界定的区域,包括函数体、循环体、条件语句体等。 12345678910void function() { int a = 0; // 块作用域开始 if (a == 0) { int b = 1; // 内部块作用域 // a和b在此可见 } // b的作用域结束 // 仅a可见,b不可见} // a的作用域结束 1.2 函数作用域 (Function...

