15关于友元和一些小练习

来源:互联网 发布:重庆市干部网络 编辑:程序博客网 时间:2024/05/21 09:55

关于友元和一些小练习

  • 关于友元和一些小练习
    • 友元函数
    • 友元类
    • 设计一个数组类的最初模型
    • 小结

本人博客收集自网上前人资料,只为做笔记复习,而且好的东西应该和大家分享才是!如有冒犯,告知鄙人定会删除

1.友元函数

class A1{public:    A1()    {        a1 = 100;        a2 = 200;    }    int getA1()    {        return this->a1;    }    //声明一个友元函数    friend void setA1(A1 *p, int a1); //这个函数是这个类的好朋友protected:private:    int a1;    int a2;};void setA1(A1 *p, int a1){    p->a1 = a1;}void main(){    A1 mya1;    cout<<mya1.getA1()<<endl;     setA1(&mya1, 300); //通过友元函数 修改A类的私有属性    cout<<mya1.getA1()<<endl;    system("pause");}

友元函数不属于类的成员函数,所以只在类里面声明,实现的时候不需要加命名空间!

2.友元类

  • 若B类是A类的友员类,则B类的所有成员函数都是A类的友员函数
  • 友员类通常设计为一种对数据操作或类之间传递消息的辅助类
#include <iostream>using namespace std;class A{public:    friend class B;//B类 是 A的好朋友 ,在B中可以访问A类的私有成员 私有函数    //1 声明的位置 和 public private没有关系    friend void modifyA(A *pA, int _a); //2 函数modifyA 是 类A的好朋友    A(int a=0, int b=0)    {        this->a = a;        this->b = b;    }    int getA()    {        return this->a;    }private:    int a;    int b;};//  void modifyA(A *pA, int _a){    //pA->a = 100;    pA->a = _a;}//class B{public:    void Set(int a)    {        Aobject.a = a;    }    void printB()    {        cout<<Aobject.a <<endl;    }private:    A Aobject;};//为什么设计友元类函数// 1.java--->1.class(字节码) ==》反射机制分析1.class 找到类对象。直接修改类的私有属性。。。//反射机制 成为一种标准。。。。jdk ...sun 做成标准 。。。jdk 的 api函数中有体现 //AOP//2 1.cpp===>汇编// 预编译 编译  连接  生成 。。gcc -E //gcc -s  -//gcc -o 1.exe 1.c // 汇编往会找。。。。很难。。。。//3 开了一个后门 。。。friend/*gcc -E hello.c -o hello.i(预处理)gcc -S hello.i -o hello.s(编译)gcc -c hello.s -o hello.o(汇编)gcc hello.o -o hello(链接)以上四个步骤,可合成一个步骤gcc hello.c -o hello(直接编译链接成可执行目标文件)gcc -c hello.c或gcc -c hello.c -o hello.o(编译生成可重定位目标文件)*/void main(){    B b1;    b1.Set(300);    b1.printB();    system("pause");}void main2101(){    A a1(1, 2);    cout<< a1.getA()<<endl;    modifyA(&a1, 300);    cout<< a1.getA()<<endl;    cout<<"hello..."<<endl;    system("pause");    return ;}

3.设计一个数组类的最初模型

  • 数组类头文件
#ifndef MYARRAY_H#define MYARRAY_H#include <iostream>using namespace std;class MyArray{public:    MyArray();    MyArray(int _len);    MyArray(MyArray & obj);    ~MyArray();    void setData(int index,int var);    int getData(int index);    int length();private:    int m_length;    int *m_space;};#endif // MYARRAY_H
  • 数组类实现文件
#include "myarray.h"MyArray::MyArray(){    m_space = NULL;    m_length = -1;}MyArray::MyArray(int _len){    if(_len < 0)        _len = 0;    m_length = _len;    m_space = new int[m_length];}MyArray::MyArray(MyArray & obj){    this->m_length = obj.m_length;    this->m_space = new int[this->m_length];    for(int i = 0;i < this->m_length;i++)    {        m_space[i] = obj.m_space[i];    }}MyArray::~MyArray(){    if(m_space != NULL){        delete []m_space;        m_space = NULL;        m_length = -1;    }}void MyArray::setData(int index,int var){    m_space[index] = var;}int MyArray::getData(int index){    return m_space[index];}int MyArray::length(){    return m_length;}
  • 测试文件
#include "myarray.h"int main(){    MyArray  a1(10);    for (int i=0; i<a1.length(); i++)    {        a1.setData(i, i);    }    cout<<"\na1: ";    for (int i=0; i<a1.length(); i++)    {        cout<<a1.getData(i)<<" ";    }    cout<<endl;    MyArray a2 = a1;    cout<<"\na2: ";    for (int i=0; i<a2.length(); i++)    {        cout<<a2.getData(i)<<" ";    }    cout<<endl;    cout<<"hello..."<<endl;    return 1;}

4.小结

  • 类通常用关键字class定义。类是数据成员和成员函数的封装。类的实例称为对象。
  • 结构类型用关键字struct定义,是由不同类型数据组成的数据类型。
  • 类成员由private, protected, public决定访问特性。public成员集称为接口。
  • 构造函数在创建和初始化对象时自动调用。析构函数则在对象作用域结束时自动调用。
  • 重载构造函数和复制构造函数提供了创建对象的不同初始化方式。
  • 静态成员是局部于类的成员,提供一种同类对象的共享机制。
  • 友员用关键字friend声明。友员是对类操作的一种辅助手段。一个类的友员可以访问该类各种性质的成员。
  • 链表是一种重要的动态数据结构,可以在程序运行时创建或撤消数据元素。
原创粉丝点击