C++类中的封装-9

一。类通常分为以下两个部分

  1.类的实现细节

  2.类的使用方式

二。C++中类的封装

  1.成员变量

    c++中用于表示类属性的变量

  2.成员函数

    c++中用于表示类的行为的函数

  3.在C++中可以给成员变量和成员函数定义访问级别

    -public

      成员变量和成员函数可以在类的内部和外界访问和调用

    -private

      成员变量和成员函数智能在类的内部被访问和调用

#include <stdio.h>

struct Biology 
{
    bool living;
};

struct Animal : Biology 
{
    bool movable;
    void findFood() 
    { 
    }
};

struct Plant : Biology 
{
    bool growable;
};

struct Beast : Animal 
{
    void sleep()
    { 
    }
};

struct Human : Animal 
{
    void sleep() 
    {
        printf("I'm sleeping...
");
    }
    
    void work()
    {
        printf("I'm working...
");
    }
};

struct Girl : Human
{
private:
    int age;

public:
    void play()
    {
        printf("I'm girl, I'm playing...
");
    }
    
    void print()
    {
        age = 22;
        
        printf("Girl's age is %d
", age);
         
        play();
        sleep();
        work();
    }
};

struct Boy : Human
{
public:
    int age;
    
    void play()
    {
        printf("I'm boy, I'm playing...
");
    }
    
    void print()
    {
        age = 23;
        
        printf("Boy's age is %d
", age);
        
        play();
        sleep();
        work();
    }
};

int main(int argc, char *argv[])
{
    Girl girl;
    
   
    girl.print();

    Boy boy;
    boy.print();
   
    printf("Press any key to continue...");
    getchar();
    return 0;
}

  4.类成员的作用域都只在类的内部,外部无法直接访问

    成员函数可以直接访问成员变量和调用其他成员函数

  5.类的外部可以通过类变量访问public成员

    类成员的作用域与访问级别没有关系 

  6.c++中用struct定义的类的所有成员默认为public。

      用Class定义的类的所有成员都默认为private 

三。类的作用域

#include <stdio.h>

int i = 1;

struct Test
{
private:
    int i;

public:
    int j;
        
    int getI()
    {
        i = 3;
        
        return i;
    }
};

int main()
{
    int i = 2;
    
    Test test;
    
    test.j = 4;
    
    printf("i = %d
", i);
    printf("::i = %d
", ::i);
  //  printf("test.i = %d
", test.i);
    printf("test.j = %d
", test.j);
    printf("test.getI() = %d
", test.getI());
    
    printf("Press any key to continue...");
    getchar();
    
    return 0;
}

输出结果 2 1 4 3 

四。一个运算类的实现:

  1. 提供 setOperator 函数设置运算类型,如加、减、乘、除。

  2.提供 setParameter 函数设置运算参数,类型为整型。

  3.提供 result 函数进行运算,其返回值表示运算的合法性,通过引用参数返回结果。  

  operator.c

#include"operate.h"

bool Operator::setOperator(char op)
{
    bool ret = false;
    if( (op == '+')||(op == '-')||(op == '*')||(op == '/') )
    {
        ret = true;    
        mOp = op;
    }
    return ret;   
}
void Operator::setParameter(double p1, double p2)
{
    mP1 = p1;
    mP2 = p2;    
}
bool Operator::result(double& r)
{
    bool ret = true;
   
    switch(mOp)
    {
        case '/':
            if((-0.000001 < mP2) && (mP2 <0.0000001))
            {
                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;
}

   operator.h

#ifndef _OPERATE_H_
#define _OPERATE_H_

class Operator
{
    public:
        bool setOperator(char op);
        void setParameter(double p1, double p2);
        bool result(double& r);
    private:  
        char mOp;
        double mP1;
        double mP2;
    
};



#endif 

  main.c

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

int main(int argc, char *argv[])
{
    Operator op;
    double r = 0;
    op.setOperator('/');
    op.setParameter(6,0);
    if( op.result(r) )
    {
        printf("Result is %f
",r);   
    }
    
    printf("Press any key to continue...");
    getchar();
    return 0;
}
原文地址:https://www.cnblogs.com/lvxiaoning/p/7591807.html