黑马程序员匠心之作-4.4友元

本文介绍了C++中友元函数的概念及应用,包括全局函数、类和成员函数作为友元的不同方式,并展示了如何让友元函数访问类的私有成员。

 

 4.4.1全局函数做友元

#include <iostream>
#include<string>
#include<ctime>
using namespace std;

class Building
{
    friend void goodfriend(Building* b);
public:
    string m_sittingroom;
    Building()
    {
        m_sittingroom = "客厅";
        m_bedromm = "卧室";
    }

private:
    string m_bedromm ;
};
//全局函数访问类中的私有成员
void goodfriend(Building*b)
{
    
    cout << "正在访问客厅" << b->m_sittingroom << endl;
    cout << "正在访问卧室" << b->m_bedromm << endl;
}
void test01()
{
    Building b;
    goodfriend(&b);
}
int main()
{
    test01();
    
    system("pause");
    return 0;
}

4.4.2类做友元

#include <iostream>
#include<string>
#include<ctime>
using namespace std;
class Building;
class goodfriend
{public:
    goodfriend();
    void visit();
    Building* b;

};
class Building
{
    friend class goodfriend;
public:
    string m_sittingroom;
    Building();
    

private:
    string m_bedromm ;
};
//注意类外初始化构造函数的方式
Building::Building()
{
    m_bedromm = "卧室";
    m_sittingroom = "客厅";
}
goodfriend::goodfriend()
{
    b = new Building;//在堆区创建一个对象,初始化成员变量Building *b
}
//类外初始化成员函数的方式
void goodfriend::visit()
{
    cout << "building 对象正在访问:" << b->m_sittingroom << endl;
    cout << "building 对象正在访问:" << b->m_bedromm << endl;
}
void test()
{
    goodfriend g;
    g.visit();
}
int main()
{
   
    test();
    system("pause");
    return 0;
}

4.4.3成员函数做友元

#include <iostream>
#include<string>
#include<ctime>
using namespace std;
class Building;
class goodfriend
{public:
    goodfriend();
    void visit();
    void visit02();
    Building* b;

};
class Building
{
    //成员函数作为友员的方式;
    friend void goodfriend::visit();
public:
    string m_sittingroom;
    Building();
    

private:
    string m_bedromm ;
};
//注意类外初始化
Building::Building()
{
    m_bedromm = "卧室";
    m_sittingroom = "客厅";
}
goodfriend::goodfriend()
{
    b = new Building;//在堆区创建一个对象,初始化成员变量Building *b
}
void goodfriend::visit()
{
    cout << "building 对象正在访问:" << b->m_sittingroom << endl;
    cout << "building 对象正在访问:" << b->m_bedromm << endl;
}
void goodfriend::visit02()
{
    cout << "building 对象正在访问:" << b->m_sittingroom << endl;
    //cout << "building 对象正在访问:" << b->m_bedromm << endl;
}
void test()
{
    goodfriend g;
    g.visit();
    g.visit02();
}
int main()
{
   
    test();
    system("pause");
    return 0;
}

 

```cpp #include <iostream> #include <string> #include <chrono> #include <thread> // 模拟学习进度条(致敬初学者的成长之路) void show_progress(const std::string& topic) { std::cout << "📖 正在学习: " << topic << "...\n"; for (int i = 0; i <= 100; i += 25) { std::cout << " [" << std::string(i/10, '=') << std::string(10 - i/10, ' ') << "] " << i << "%\r"; std::this_thread::sleep_for(std::chrono::milliseconds(30)); // 模拟学习过程 } std::cout << "\n✅ 完成: " << topic << "\n\n"; } int main() { std::cout << "🚀 欢迎大一同学开启 C++ 学习之旅!\n\n"; std::string topics[] = { "C++与C语言区别、开发环境搭建", "数据型、变量、运算符", "流程控制(if/switch/循环)", "数组、字符串处理", "函数重载、引用、内联函数", "指针增强:const指针、多级指针", "面向对象:与对象、封装", "构造与析构函数、拷贝控制", "this指针、静态成员", "友元函数与友元", "运算符重载", "继承与多态", "虚函数与纯虚函数", "模板编程(函数模板、模板)", "STL基础:string/vector/map/set", "异常处理", "智能指针与RAII", "Lambda表达式与函数对象" }; int total_hours = 0; for (const auto& topic : topics) { show_progress(topic); total_hours += 3; // 每个主题平均投入约3小时(含练习) } std::cout << "🎉 恭喜你完成黑马程序员C++从0到1课程!\n"; std::cout << "📊 总耗时估算: " << total_hours << " 小时\n"; std::cout << "💡 建议周期: 6~8 周(每天学习 2~3 小时)\n"; std::cout << "📌 温馨提示: 真正掌握 = 学习 + 大量编码实践 + 复盘\n"; return 0; } ``` --- ### 回答问题:学完《黑马程序员C++教程从0到1入门编程》需要多久? 对于一个**已经掌握C语言的大一学生**,学习这套课程通常需要: > ✅ **6~8周(约45~60天)** #### 📅 具体时间分配建议如下: | 阶段 | 内容 | 所需时间 | 每日建议 | |------|------|----------|---------| | 第1-2周 | 基础语法过渡(C to C++)、数据型、流程控制、函数增强 | 15~20小时 | 2小时/天 | | 第3-4周 | 复合结构、数组、字符串、指针进阶、引用、内联函数 | 15~20小时 | 2.5小时/天 | | 第5-6周 | 面向对象核心:、对象、封装、构造/析构、this指针 | 20小时 | 3小时/天 | | 第7周 | 继承、多态、虚函数、友元、运算符重载 | 20小时 | 3小时/天 | | 第8周 | 模板、STL初步(vector/string等)、异常、智能指针 | 20~25小时 | 3小时/天 | > ⚠️ 注意:这是**有效学习时间**,不包括走神、看视频但不动手写代码的时间。 --- ### 🔍 影响学习速度的关键因素 1. **是否动手敲代码?** - 只看不练 → 掌握度不足30% - 边看边敲 → 掌握度可达70%以上 2. **是否有项目实践?** - 学完每章后尝试写小例子(如学生管理系统、图书管理系统),能极大巩固知识。 3. **数学和逻辑基础如何?** - C语言已掌握说明你具备基本编程思维,这是巨大优势。 4. **能否坚持每日学习?** - 连续学习效率远高于“三天打鱼两天晒网”。 --- ### 💡 给大一学生的建议 - ✅ 利用好你已有的 C 语言基础:指针、内存模型、函数设计思想都可迁移。 - ✅ 把重点放在 **C++ 特有机制** 上:、对象、构造函数、拷贝语义、RAII、STL。 - ✅ 学到 `vector` 和 `string` 时,立刻抛弃 C 风格数组和 `char*`。 - ✅ 多用 `std::cin/cout` 替代 `scanf/printf`,体会流操之美。 - ✅ 不必一开始就深挖模板元编程或移动语义,先建立整体认知。 --- ###
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值