15 About Friends and Some Small Exercises

Keywords: JDK Java

About Youyuan and some exercises

My blog collects information from predecessors on the Internet, only to review notes, and good things should be shared with you! If there is any offence, inform me that it will be deleted.

1. Friend function

class A1
{
public:
    A1()
    {
        a1 = 100;
        a2 = 200;
    }
    int getA1()
    {
        return this->a1;
    }
    //Declare a friend function
    friend void setA1(A1 *p, int a1); //This function is a good friend of this kind.

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); //Modifying the Private Attributes of Class A through Friend Functions
    cout<<mya1.getA1()<<endl;

    system("pause");
}

Friend function does not belong to the member function of the class, so it is only declared in the class, and no namespace is needed to implement it!

2. Friends

  • If Class B is a friend class of Class A, then all member functions of Class B are friend functions of Class A.
  • Friend classes are usually designed as an auxiliary class for data manipulation or message passing between classes.
#include <iostream>
using namespace std;

class A
{
public:
    friend class B;//Class B is a good friend of Class A. In Class B, private member private functions of Class A can be accessed.
    //1 The location of the declaration has nothing to do with public private
    friend void modifyA(A *pA, int _a); //2 function modifyA is a good friend of class 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;
};

//Why Design Friend Class Functions
// 1. Java - > 1.class (bytecode) == Reflection mechanism analysis 1.class finds class objects. Modify the private attributes of the class directly...
//Reflection mechanism becomes a standard... jdk.... sun makes standard... jdk's api functions are embodied 
//AOP
//21. cpp===> assembly
// Pre-compiled compiled connection generation. Gcc-E//gcc-s-
//gcc -o 1.exe 1.c 
// Compilation will look for... It's hard...
//Open a back door... friend

/*
gcc -E hello.c -o hello.i(Pretreatment)
gcc -S hello.i -o hello.s(Compilation)
gcc -c hello.s -o hello.o(Compilation)
gcc hello.o -o hello(Links)
These four steps can be combined into one step.

gcc hello.c -o hello(Direct compilation links to executable object files)
gcc -c hello.c Or gcc-c hello.c-o hello.o (compiled to generate relocatable target files)
*/



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. Designing the initial model of an array class

  • Array class header file
#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
  • Array class implementation file
#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;
}
  • Test files

#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. Summary

  • Classes are usually defined with the keyword class. Class is the encapsulation of data members and member functions. An instance of a class is called an object.
  • Structural type is defined by keyword struct, which is a data type composed of different types of data.
  • Class members are determined by private, protected, and public access features. The public member set is called the interface.
  • Constructors are called automatically when objects are created and initialized. Destructors are called automatically at the end of the object scope.
  • Overload constructors and replication constructors provide different initialization methods for creating objects.
  • Static members are local members of classes, providing a sharing mechanism for similar objects.
  • Friends use the keyword friend to declare. Friends are an auxiliary means of class operation. Friends of a class can access members of this class of all kinds.
  • Linked list is an important dynamic data structure, which can create or revoke data elements while the program is running.

Posted by Z3RatuL on Thu, 27 Jun 2019 15:18:00 -0700