-->

Access Specifiers in OOP

 Access Specifiers in OOP


There are three access specifiers in OOP. But here we only focus on two (private and public). 


Public:

Public data members and member functions can be accessed both inside and outside the class.

e.g

A public member variable is like a public book on a shelf; anyone can see it and use it. Similarly, a public member function is like a public service counter; anyone can come and ask for its help.


class Counter {


public:

    int count; // Public member variable

    void increment() { // Public member function

        count++;

    }

};


int main() {

    Counter obj;

    obj.count = 0; // Accessing the public member variable

    obj.increment(); // Calling the public member function

    return 0;

}


Private:

A private member can only be accessed by functions inside the same class.



Note: that each access specifier is followed by a colon.


Important point: Without 'public' or 'private', everything becomes private by default.


Placement of private and public Members

The order of private and public members doesn't matter in a class. We can arrange them in any order, and they can be mixed together.

The standard practice of listing private members together first, followed by the public members.



Accessing an Object’s Members

Public members of a class object are accessed with the dot operator.

int main() {
A obj;
obj.setTemp(38);
cout << "temperature " << obj.getTemp();
return 0;
}




Complete the given task and share in comments. 



Create a class to represent a Car and use objects of that class to Enter value. 

Define a class named Car with the following attributes:

Make (string)

Model (string)

Year (int)

Price (double)

Implement methods to:

Set the attributes of a car.

Display car information.