Class definition, declaration & access specifiers

Now lets dig in a bit deeper.
 
What is a class definition and what is a class declaration? Are they different and if yes then how? Well, declaration is basically an incomplete definition of a class (a definition without the body). A full class definition is made out of the keyword class, user-defined name and class body (as said in the Introduction).
Class body contains 2 types of elements: data members (attributes) and member functions (methods)
class Car1; //declaration

class Car1{//class definition
  float speed;
  int seats;
  int gas;
  int cost;
  float getSpeed(){return speed;}
  void setSpeed(float s){speed=s;}
};

Access specifiers

Within the class body there are sections class members exist in, that represent 3 levels of access restrictions that can be labeled with public, private and protected. This is a very important feature of Object-Oriented Programming and these 3 sections are called the access specifiers.

class Name{
public:
  //here exist public members

private:
//here exist private members

protected:
//here exist protected members

}
public – NO restrictions (you can access public class members from anywhere within the program). You can change and use public class attributes without any need for a public member function
 
private – HEAVY restrictions. Class members cannot be accessed outside that class. The only exception to that are friend functions (which will be discussed some other time)
 
protected – similar to private, has all the restrictions as private but also allows class members to be accessed in classes derived from that class, aka, child classes(will also be discussed some other time, let’s focus on this right now)
 
These sections can be written multiple times throughout the class.
class Car2{
  private:
    float speed;
    int gas;

  public:
    float getSpeed(){return speed;}
    void setSpeed(float s){speed=s;}

  private:
    int seats;

  public:
    int cost;
    
}

Let’s see how those restrictions impact our use of class members:

int main(){
  Car2 c;//creating an object(more about creating objects later)
  
  c.cost=3500;//OK! attribute cost of class Car2 is public 
  c.speed=15;//NO!! attribute speed of class Car2 is private
  c.setSpeed(110);/*OK! method setSpeed() of class Car2 is public and it can access the private
  attribute speed */
  float k=c.getSpeed();/*OK! method getSpeed() of class Car2 is public and it can access the
  private attribute speed*/
  int p=c.seats;//NO!! private member

}

Maybe some of you may have noticed, the class Car1 from Example 1 does not have any labeled section but is still legally defined. Now, If we made an object of Class1 and tried to do something with its attributes and methods outside of the class, what would happen? Well there is no explicit restriction so it’s probably okay to access it, right? NO! (!!!) PRIVATE SECTION IS THE DEFAULT SECTION! Meaning, everything in the class that is defined or declared BEFORE the FIRST EXPLICITLY WRITTEN SECTION is considered a private member.

class Car3{//class definition
//private section
  float speed;//private memeber
  int seats;

public: //now the private section can end
  int gas;
  int cost;
  float getSpeed(){return speed;}
  void setSpeed(float s){speed=s;}
}

int main(){
  Class3 c3;
  c3.speed=100;//NO!!!private member
  c3.setSpeed(100); // OK!!
}

Leave a Reply