public

Access Specifiers in C++

The access specifiers private, protected, and public are used to set the access of a class’s members. We can make members private, visible only to code belonging to the same class, protected, visible to code only in the same class or classes derived from it, or else public, visible to all. By default, all members of a class are private.

#include <iostream>

using namespace std;


class CExmp{

    private:
        int _number;
    public:
        void incrementNumber();
        int getNumber();
        CExmp(int i);

};

CExmp::CExmp(int i){
    this->_number = i;
}

void CExmp::incrementNumber(){
    this->_number++;
}

int CExmp::getNumber(){
    return this->_number;
}


int main(void){


    CExmp objEmpOne = CExmp(47);
    
    objEmpOne.incrementNumber();

    cout << objEmpOne.getNumber() << endl;

    objEmpOne.incrementNumber();

    cout << objEmpOne.getNumber() << endl;

    return 0;

}

Note that as members are private by default, the private declaration isn’t, strictly speaking, necessary.

An important aspect of object oriented programming is data hiding. Data stored in an object should only be accessed via specific data access methods. In that way, we can control access to the data, ensuring that no illegal values are stored, and making sure the functionality of the object is predictable. We usually implement data access methods in pairs, as get and set methods, where the set method stores the data, and the get method retrieves the data.

#include <iostream>
#include <string>

using namespace std;

class CBikes{
    private:
        int _inStock;
        string _brand;
    public:
        void setInStock(int n);
        int getInStock();

        bool sellBikes(int n);
        
        string getBrand();
        
        CBikes(string s);
        

};

CBikes::CBikes(string s){
    this->_brand = s;
}

void CBikes::setInStock(int n){
    this->_inStock = n;
}

int CBikes::getInStock(){
    return this->_inStock;
}

bool CBikes::sellBikes(int n){
    if(this->_inStock - n >= 0){
        this->_inStock -= n;
        return true;
    } else {
        return false;
    }
}

string CBikes::getBrand(){
    return this->_brand;
}

int main(void){

    CBikes objSchwinn = CBikes("Schwinn");
    objSchwinn.setInStock(10);

    CBikes objGiant = CBikes("Giant");
    objGiant.setInStock(15);

    cout << "We have " << objSchwinn.getInStock() << " " << objSchwinn.getBrand() << "(s)" << endl;
    cout << "We have " << objGiant.getInStock() << " " << objGiant.getBrand() << " (s)" << endl;

    cout << "Selling 5 of the " << objSchwinn.getBrand() << "(s)" << endl;
    cout << "We now have " << objSchwinn.getInStock() << " of the " << objSchwinn.getBrand() << "(s)" << endl;

    cout << "Can we fill an order for 20 of the " << objGiant.getBrand() << "(s) ?" << endl;
    if(objGiant.sellBikes(20)){
        cout << "Yes! Sold. We now have " << objGiant.getInStock() << " left." << endl;
    } else {
        cout << "Nope. Not enough bikes." << endl;
    }

    return 0;

}

We use constructors to initialize data in an object as that object is being created. A constructor is a special method that is invoked automatically when the object is created. A constructor has the same name as the class itself, and not return value. When we declare objects, we can pass values to the constructor to initialize the state of the object.

#include <iostream>
#include <string>

using namespace std;

class CPet{
    private:
        string _name;
        string _type;
        int _age;
    public:
        string getName();
        void setName(string s);

        string getType();

        int getAge();
        void setAge(int n);

        CPet(string name, string type, int age);
};


string CPet::getName(){
    return this->_name;
}

void CPet::setName(string s){
    this->_name = s;
}

string CPet::getType(){
    return this->_type;
}

int CPet::getAge(){
    return this->_age;
}

void CPet::setAge(int n){
    this->_age = n;
}

CPet::CPet(string name, string type, int age){
    this->_name = name;
    this->_type = type;
    this->_age = age;
}


int main(void){

    CPet objDog = CPet("Fido", "dog", 5);
    CPet objBird = CPet("Polly", "bird", 3);

    cout << objDog.getName() << " is a " << objDog.getType() << " that is " << objDog.getAge() << " years old." << endl;
    cout << objBird.getName() << " is a " << objBird.getType() << endl;

    return 0;

}

If a constructor for the class is not provided, the compiler will provide one for us; this constructor is known as the default constructor.