peri hokiperihokiclarustologisticsrimashaop.compola taktis menang gates of olympus x1000 perihokipemain perihoki bongkar trik jepep starlight princess x1000perihoki beri panduan lengkap bermain mahjong ways 2 pgsofttips cara penghasil uang pgsoft mahjong ways 2 perihokimahjong wins 3 perihoki memberikan penggali terbesarfenomena gates of olympus buka peluang besar bagi duta76cara dapat pola emas rahasia di duta7 wild west goldtrik menang pgsoft mahjong ways 2 modal receh maxwin duta76duta76 main pgsoft mahjong ways 2 di sela istirahatscatter hitam mahjong wins 3 spin pasti jepe duta76perihoki spin mahjong ways 2 pgsoft scatter hitam maxwinsimbol mahjong wins 3 yang sering muncul di awal perihokitrik gampang menang mahjong winscuan mahjong wins 3 tanpa hentipola spin taruhan kecil mahjong winsstrategi kunci scatter hitam beruntungacor total mahjong wayskemenangan maksimal mahjong wayskemenangan pria pekanbaru mahjong waysmahjong wins3 fenomena baru sumatera baratmenangkan mahjong dengan lincahmenembus batas keberuntungan mahjong waysmenjajal keberuntungan di mahjong wayspola gratis mahjong ways1scatter hitam senjata rahasia mahjong winsteknik spinning menguntungkan untuk scatter hitamanalisis rtp tertinggi mahjong winsfenomena scatter hitam mahjong winsibu rumah tangga dan keberuntungan mahjongkeseruan main mahjong wins3menang mahjong pengguna baru di sosial mediamenghargai setiap momen di mahjong waysserunya main mahjong di warung internetspin slow motion mancing scatterstrategi jitu maxwin mahjong waysstrategi jitu menang mahjong modal kecil jackpot besaranak kampung papua menggemparkan mahjongkeajaiban scatter hitam mahjong wayskeberhasilan joni mahjong winskejaiban mahjong ways di ujung jalanketua mahjong wins3 kehadiran kemenangan dimulai dari balimahjong kehadiran yang menguntungkanmahjong menggila adat88panduan kilat raih 108 jutatips memenangkan mahjongbarista kopi menang 110 juta spin mahjongdakota76 scatter hitam mahjong wins 3gagal cpns menang mahjong wins 3 rp120 jutakisah karyawan minimarket dapat thr lebih awal mahjong winskisah pelajar sma kuasai mahjong wins 3 inspirasi sekolahkisah penjaga parkir tanah abang motor baru mahjong winskisah sukses ibu rumah tangga bandung mahjong winsmahasiswa kkn beli iphone15 mahjong wins3 dakotamantan anak band influencer mahjong scatter hitampola rtp senin gates of olympus guru matematikatukang cuci motor menemukan keberuntungan di mahjong

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 282

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: session_set_cookie_params(): Cannot change session cookie parameters when headers already sent

Filename: Session/Session.php

Line Number: 294

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 304

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 314

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 315

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 316

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 317

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 375

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: drivers/Session_files_driver.php

Line Number: 108

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: session_set_save_handler(): Cannot change save handler when headers already sent

Filename: Session/Session.php

Line Number: 110

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: session_start(): Cannot start session when headers already sent

Filename: Session/Session.php

Line Number: 143

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

Access modifier trong C++ - Freetuts
STARTING
CONTROL STATEMENT
FUNCTION
ARRAY & POINTER
OOP
STL
ITERATORS
OTHER FEATURES
CÁC CHỦ ĐỀ
BÀI MỚI NHẤT
MỚI CẬP NHẬT

Access modifier trong C++

Bài học hôm nay chúng ta sẽ tìm hiểu về access modifier trong C++? Vậy access modifier trong C++ là gì? Chúng ta sẽ cùng tìm hiểu trong nội dung sau đây.

test php

banquyen png
Bài viết này được đăng tại freetuts.net, không được copy dưới mọi hình thức.

1. Access modifier trong C++

Access modifier trong C++  được sử dụng để triển khai một tính năng quan trọng của Lập trình hướng đối tượng được gọi là Data Hiding.

Chúng ta cùng xem xét một ví dụ sau: 

Giả sử công ty của chúng ta có lưu trữ thông tin của nhân viên như số điện thoai, email, năm sinh, phòng ban, chức vụ, địa điểm văn phòng làm việc,... Tất cả nhân viên của công ty có thể thấy các thông tin của nhau để tiện liên lạc thông qua trang web intrannet và có tài khoản để đăng nhập.

Bài viết này được đăng tại [free tuts .net]

Người ở ngoài công ty thì không có tài khoản và không thể truy cập vào trang web intrannet để thấy thông tin của nhân viên trong công ty. Chúng ta gọi đó là hidding data.

Access modifier hay Access Specifier trong một lớp được sử dụng để đặt khả năng truy cập của các thành viên lớp. Đó là, nó đặt ra một số hạn chế đối với các thành viên lớp không được truy cập trực tiếp bởi các hàm bên ngoài.

Trong C++ có hổ trợ 3 loại access modifier đó là:

  • Private
  • Public
  • Protected

Lưu ý: Nếu chúng ta không chỉ rõ bất kỳ access modifier cho thành viên của lớp, thì mặc định nó là Private.

Chúng ta sẽ cùng tìm hiểu từng loại access modifier trong C++ nhé.

2. Private access modifier

Các thành viên lớp được khai báo là private thì chỉ có thể được truy cập bởi các hàm bên trong lớp. Chúng không được phép truy cập trực tiếp bởi bất kỳ đối tượng hoặc hàm nào bên ngoài lớp.

Chỉ các hàm thành viên hoặc các hàm bạn mới được phép truy cập các thành viên dữ liệu riêng tư của một lớp. Hàm bạn là gì? Chúng ta sẽ được tìm hiểu trong bài tiếp theo.

Private Access modifier cũng là access modifier mặc định của lớp.

Ví dụ

Chúng ta cùng xem xét một ví dụ đơn giản về private access modifier trong C++ như sau:

Ví dụ
#include<iostream> 
using namespace std; 
  
class HinhTron {    
    private:  
        double banKinh; 
       
    public:     
        double  TinhDienTich() {   
            return 3.14*banKinh*banKinh; 
        } 
}; 
  
int main() {    

    HinhTron ht; 
    // Chung ta thu truy cap du lieu thanh vien private ben ngoai lop
    ht.banKinh = 3.5; 
      
    cout << "Dien tich cua hinh tron la:" << ht.TinhDienTich(); 
    
    return 0; 
} 

Và kết quả sau khi thực thi chương trình trên như sau:

access p1 JPG

Chương trình trên báo lỗi biên dịch vì chúng ta đang truy cập dữ liệu thành viên bán kính ở dạng private.

Tuy nhiên, chúng ta có thể truy cập gián tiếp các thành viên dữ liệu riêng tư của một lớp bằng cách sử dụng các hàm thành viên công khai của lớp. Chúng ta cùng xem ví dụ sau đây có thể truy cập biến banKinh của lớp HinhTron thông qua hàm TinhBanKinh.

Ví dụ
#include<iostream> 
using namespace std; 
  
class HinhTron {    
    private:  
        double banKinh; 
       
    public:     
        void TinhDienTich(double bk) {
            banKinh = bk;
            double dienTich = 3.14*banKinh*banKinh; 
            cout << "Ban kinh la: " << banKinh << endl;
            cout << "Dien tich la: " << dienTich << endl;
        } 
}; 
  
int main() {    

    HinhTron ht; 
    double banKinh = 2;
    ht.TinhDienTich(banKinh);
    
    return 0; 
}

Và kết quả sau khi thực thi chương trình trên là:

access p2 JPG

3. Public access modifier

Tất cả các thành viên lớp được khai báo dưới dạng public sẽ có sẵn cho tất cả mọi người. Các thành viên dữ liệu và các hàm thành viên được khai báo public cũng có thể được truy cập bởi các lớp khác.

Các thành viên public của một lớp có thể được truy cập từ bất cứ đâu trong chương trình bằng cách sử dụng toán tử truy cập thành viên trực tiếp (.) với đối tượng của lớp đó.

Ví dụ

Chúng ta cùng xem một ví dụ đơn giản về public access modifier trong C++ như sau:

Ví dụ
#include<iostream> 
using namespace std; 
  
class HinhTron {    
    public:
        double banKinh;
        double  TinhDienTich() {   
            return 3.14*banKinh*banKinh; 
        } 
}; 
  
int main() {    

    HinhTron ht; 
    ht.banKinh = 3.5; 
      
    cout << "Ban kinh cua hinh tron la: " << ht.banKinh << endl;  
    cout << "Dien tich cua hinh tron la:" << ht.TinhDienTich(); 
    
    return 0; 
}

Và kết quả sau khi thực thi chương trình trên như sau:

access p3 JPG

Trong ví dụ trên, thành viên dữ liệu bán kính là công khai nên chúng ta được phép truy cập bên ngoài lớp.

4. Protected access modifier

Protected access modifier tương tự như private access modifier, sự khác biệt là thành viên lớp được khai báo là Protected không thể truy cập bên ngoài lớp nhưng chúng có thể được truy cập bởi bất kỳ lớp con (lớp dẫn xuất) nào của lớp đó.

Ví dụ

Chúng ta sẽ cùng xem một ví dụ đơn giản về protected access modifier trong C++ như sau:

Ví dụ
#include <bits/stdc++.h> 
using namespace std; 
  
//Lop cha
class Cha {    
    protected: 
        string diaChi;
}; 
 
//Lop con  
class Con : public Cha 
{ 
    public: 
        void ThietLapDiaChi(string dc) { 
            diaChi = dc; 
        } 
          
        void HienThi() { 
            cout << "Dia chi la: " << diaChi << endl; 
        } 
}; 
  
int main() { 
    Con cn; 
    cn.ThietLapDiaChi("Thoi Binh, Ca Mau"); 
    cn.HienThi(); 
    return 0; 
} 

Và kết quả sau khi thực thi chương trình trên như sau:

access p4 JPG

5. Kết luận

Như vậy là chúng ta đã tìm hiểu xong về access modifier trong C++ là gì rồi. Các bạn biết cách bảo vệ dữ liệu của mình như thế nào rồi đúng không.

Mình khuyến khích các bạn nên thiết lập private cho dữ liệu thành viên, và thiết lập hàm dạng public để truy xuất dữ liệu thành viên đó. Không nên truy xuất một cách trực tiếp dữ liệu thành viên của lớp.

Mình sẽ kết thúc bài học hôm nay ở đây. Bài tiếp theo chúng ta sẽ cùng tìm hiểu về hàm bạn trong C++. Các bạn nhớ theo dõi nhé.

Cùng chuyên mục:

Các hàm xử lý mảng đa chiều (array.h) trong C/C++

Các hàm xử lý mảng đa chiều (array.h) trong C/C++

Các hàm xử lý ngày tháng (datetime.h) trong C/C++

Các hàm xử lý ngày tháng (datetime.h) trong C/C++

Các hàm xử lý số thực (float.h) trong C/C++

Các hàm xử lý số thực (float.h) trong C/C++

Các hàm xử lý số nguyên lớn (bigint.h) trong C/C++

Các hàm xử lý số nguyên lớn (bigint.h) trong C/C++

Các hàm xử lý thời gian (time.h) trong C

Các hàm xử lý thời gian (time.h) trong C

Các hàm xử lý chuỗi (string.h) trong C/C++

Các hàm xử lý chuỗi (string.h) trong C/C++

Thread Pools và Parallel Algorithms trong C++

Thread Pools và Parallel Algorithms trong C++

Tạo và quản lý các Multithreading trong C++

Tạo và quản lý các Multithreading trong C++

Xử lý ngoại lệ khi làm việc với Memory Allocation trong C++

Xử lý ngoại lệ khi làm việc với Memory Allocation trong C++

Try, Catch, và Throw của Exception Handling trong C++

Try, Catch, và Throw của Exception Handling trong C++

Cách sử dụng Lambda Expressions trong C++

Cách sử dụng Lambda Expressions trong C++

Sử dụng weak_ptr trong C++

Sử dụng weak_ptr trong C++

Sử dụng shared_ptr trong C++

Sử dụng shared_ptr trong C++

Sử dụng unique_ptr trong C++

Sử dụng unique_ptr trong C++

Tổng quan về Smart Pointers trong C++

Tổng quan về Smart Pointers trong C++

Sử dụng Iterators trong STL của C++

Sử dụng Iterators trong STL của C++

[Iterator] Sử dụng Vector trong C++

[Iterator] Sử dụng Vector trong C++

[Iterator] Sử dụng trong List trong C++

[Iterator] Sử dụng trong List trong C++

[STL] Sử dụng Vector trong C++

[STL] Sử dụng Vector trong C++

Tổng quan về Iterators trong C++

Tổng quan về Iterators trong C++

Top