Access controls in C++ Classes

Access Controls in C++ Classes

Classes are considered to be backbone in Object Oriented Programming (OOP). OOP is a way of programming being used in modern computer programming.

Always all the modern computer languages support this way of programming and C++ has no exception. In fact the main difference between C and C++ is that C++ supports object oriented programming while C doesn’t.

There is no concept of object oriented programming without classes. A class is like the structure or blueprint of anything. It contains attributes and behaviors of the entity about which the class is created. For example the class of vehicle might have attributes like name, model, engine type, color etc. The behaviors might be ‘start vehicle’, ‘change gear’, ‘speed acceleration’, ‘stop vehicle’ and so forth.

The main difference between attributes and behaviors of a class is that attributes represent the properties while behaviors represent the processing. In technical terms, attributes are variables and behaviors are functions. In OOP, they are called data members and member functions respectively.

Anything which might have attributes, behaviors or both can be written in the form of a class. Class is the advanced form of structures being used and defined in C. A class works like a user-defined data type. Once a class is defined then it can be used to define variables (objects) of a class.

Objects

Objects are the instances of a class. A class itself doesn’t occupy any space in memory but if an object of a class is defined then it will need a space in memory to hold its data. Objects are actual representation of a class. For example if vehicle is a class then Mercedes is an object of vehicle class. Similarly sparrow, peacock, eagle can be called objects of bird class.

A class might have as many objects as required. Having said earlier, if we define a class then it acts like a data type and we can define one or more than one variables of this data type. Variables of a class are different from normal variables and are called objects of that class. Objects of a class can access all the variables and functions defined in that class.

Syntax of a class

class class_name
{
data_members;
member_functions();
}
Example

class Example
{
int a; //data member
void setA(int x) //member function
{
a=x;
}
void display() //member function
{
cout<<a;
}
}
void main()
{
Example e; //object of class
e.setA(99); //calling member function of the class
e.display();//calling member function of the class
}

More Related Articles For You

    C++ Tutorial

    C++ Quizzes