Class and encapsulation

Time:2022-5-9

Similarities and differences between struct and class

  • Similarities
    • In C + +, struct can be used to define a class, but it can define its own functions and access permissions, which is not allowed in C. C + + is compatible with everything in C language as the name suggests, and then develops its own things. However, struct in C + + changes the original meaning, so we must find other methods to replace struct, so c + + provides a new keyword class for class definition, The usage of class and struct (which should be avoided as much as possible) is exactly the same.
  • difference
    • When defining a class with struct, the default access level of all members is public
    • When defining a class with class, the default access level of all members is private
#include <stdio.h>
struct A
{
    // defualt to public
    int i;
    // defualt to public
    int getI()
    {
        return i;
    }
};
class B
{
    // defualt to private
    int i;
    // defualt to private
    int getI()
    {
        return i;
    }
};
int main()
{
    A a;
    B b;
    a.i = 4;
    printf("a.getI() = %d\n", a.getI());
    b.i = 4;
    printf("b.getI() = %d\n", b.getI());
    return 0;
}

Class and encapsulation

compile
  • It can be seen that the default level of our class is private, and we will make an error when accessing it outside the class

The class in C + + supports the separation of declaration and implementation, separating the implementation and definition of the class. There is only the declaration of the class in the header file. The declaration of the class represents the declaration of member variables and member functions required by the user. Other implementations of the class and the specific implementation of member functions are completed in the source file

operation.h

#ifndef _OPERATION_H
#define _OPERATION_H
class operation
{
  private:
    char   Mop;
    double Mp1;
    double Mp2;
  public:
    bool setOpeartion(char mop);
    void setPatamter(double mp1,double mp2);
    bool  result(double &r); 
};
#endif

operation.c

#include "operation.h"
bool operation::setOpeartion(char mop)
{
  bool ret = false;
  if((mop=='+')||(mop=='-')||(mop=='*')||(mop=='/'))
  {
     ret = true;
     Mop = mop;
  }
  else
  {
    Mop = '
#include "operation.h"
bool operation::setOpeartion(char mop)
{
bool ret = false;
if((mop=='+')||(mop=='-')||(mop=='*')||(mop=='/'))
{
ret = true;
Mop = mop;
}
else
{
Mop = '\0';
}
return ret;
};
void operation::setPatamter(double mp1,double mp2)
{
Mp1  = mp1;
Mp2  = mp2;
}
bool operation::result(double &r)
{
bool ret = true;
switch(Mop)
{
case '/':
if((-0.000000001<Mp1)&&(0.000000001>Mp2))
{
ret = false;
}
else
{
r = Mp1/Mp2;
}
break;
case '+':
r = Mp1 + Mp2;
break;
case '*':
r = Mp1 * Mp2;
break;
case '-':
r = Mp1 - Mp2;
break;
default:
ret = false;
break;
}
return ret;
}
'; } return ret; }; void operation::setPatamter(double mp1,double mp2) { Mp1 = mp1; Mp2 = mp2; } bool operation::result(double &r) { bool ret = true; switch(Mop) { case '/': if((-0.000000001<Mp1)&&(0.000000001>Mp2)) { ret = false; } else { r = Mp1/Mp2; } break; case '+': r = Mp1 + Mp2; break; case '*': r = Mp1 * Mp2; break; case '-': r = Mp1 - Mp2; break; default: ret = false; break; } return ret; }

test.c

#include <stdio.h>
#include "operation.h"

int main()
{
    operation op;
    double r = 0;
    
    op.setOpeartion('/');
    op.setPatamter(9, 3);
    
    if( op.result(r) )
    {
        printf("r = %lf\n", r);
    }
    else
    {
        printf("Calculate error!\n");
    }
    return 0;   
}

The above code adopts the object-oriented method to separate the class declaration and definition, and calculate a four arithmetic operation. It can be seen that the object-oriented method makes our code easier to expand, shift values and have clear layers

reference: D. T.software, warrior guard, helmet brother, keep learning records only