Learning C ++ (8) - Package

C ++ object-oriented three properties: encapsulation , inheritance , polymorphism

C ++ believe that everything can be an object, the objects have their properties and behavior

Objects of the same nature, can abstract class

1. significance package

Packaging is one of the three characteristics of object-oriented C ++

Package significance:

  • The properties and behavior as a whole, the performance of things in life
  • The controlled properties and behavior
  1. Meaning a package:

In the design category, attributes and behaviors write together, showing things

Syntax :class 类名{访问权限:属性 / 行为};

Example 1 : Design of a class circle, the circumference of circular

#include<iostream>
using namespace std;

//圆周率
const double PI = 3.14;

//设计一个圆类,求圆的周长
//圆求周长的公式:2*PI*半径
class Circle{
    //访问权限
public:
    //属性
    int m_r;
    //行为
    //获取圆的周长
    double calculateZC(){
        return 2*PI*m_r;
    }
};

int main(){
    //通过圆类创建具体的圆
    Circle c1;
    c1.m_r = 10;

    cout << "圆的周长为:" << c1.calculateZC() << endl;

    return 0;
}

Example 2 : Design a student class, attribute names and student number, name and student number to be assigned, you can display the student's name and student number

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

class Student{
    //访问权限
public:
    string s_name;
    string s_id;

    void showStudent(){
        cout << "姓名:" << s_name << "\n学号:" << s_id << endl;
    }

    void setName(string name){
        s_name = name;
    }
};

int main(){
    //实例化对象
    Student s1;
    //s1.s_name = "张三";
    s1.setName("张三");
    s1.s_id = "001";

    s1.showStudent();

    Student s2;
    s2.s_name = "李四";
    s2.s_id = "002";

    s2.showStudent();

    return 0;
}
  1. Significance of the package:

Class at design time, you can put attributes and behavior on the different rights under controlled

There are three access rights:

  • public public authority
  • protected rights protection
  • private proprietary rights
#include<iostream>
#include<string>
using namespace std;

// public     公共权限  成员 类内可以访问,类外可以访问
// protected  保护权限  成员 类内可以访问,类外不可以访问  子类可以访问父类的保护内容
// private    私有权限  成员 类内可以访问,类外不可以访问  子类不可以访问父类的保护内容

class Person{
public:
    string m_Name; //姓名

protected:
    string m_car;  //汽车

private:
    int m_Password;  //银行卡密码

public:
    void func(){
        m_Name = "张三";
        m_car = "奔驰";
        m_Password = 123456;
    }
};

int main(){
    Person p1;

    p1.m_Name = "李四";
    //p1.m_car = "宝马";
    //p1.m_Password = 123;

    return 0;
}

2. struct and class differences

The only difference between struct and class in C ++ is that different default access

the difference:

  • struct default permissions for the public
  • The default permissions for the private class
#include<iostream>
#include<string>
using namespace std;

class C1{
    int m_A;
};

struct C2{
    int m_A;
};

int main(){
    C1 c1;
    //c1.m_A = 100;
    C2 c2;
    c2.m_A = 200;

    return 0;
}

3. The members of the property to private

Advantage 1 : The members of the attribute set to private, you can control your own read and write permissions

Advantage 2 : For written permission, we can check the validity of data

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

class Person{
public:
    //写姓名
    void setName(string name){
        m_Name = name;
    }
    //读姓名
    string getName(){
        return m_Name;
    }
    //读年龄
    int getAge(){
        return m_Age;
    }
    //写年龄————————有效性验证
    void setAge(int age){
        if(age < 0 || age > 150){
            cout << "年龄范围错误" << endl;
            return ;
        }
        m_Age = age;
    }
    //写爱人
    void setLover(string name){
        m_Lover = "NoBody";
    }

private:
    string m_Name;   //姓名 可读可写
    int m_Age = 20;    //可读可写,但范围必须在0~150之间
    string m_Lover;   //只写
};

int main(){
    Person p;
    p.setName("张三");

    cout << "姓名为:" << p.getName() << endl;


    return 0;
}

Guess you like

Origin www.cnblogs.com/maeryouyou/p/12008175.html
Recommended