Object Oriented Programming Using C++: (Access Specifiers)
Object Oriented Programming Using C++: (Access Specifiers)
Object Oriented Programming Using C++: (Access Specifiers)
Class
private members by default
Structure
public members by default The data members are thus accessible out of the structure implementation, e.g., Client program
OOP Features
Abstraction Encapsulation and data hiding Inheritance Polymorphism Reusable CODE
Class
Class is an Object-Oriented Programming Concept. A class is a programmer-defined data type. It consists of data members and functions which operate on that data.
Data Members define Object attributes Functions define Object Behavior
C++ Class
Classes are derived types. Class enables the programmer to model objects that have attributes (represented as data members), and behaviors or operations or functionality (represented as member functions). Member functions are sometimes called methods in other object-oriented languages.
3
4 5 6
Time();
void setTime( int, int, int ); void printMilitary(); void printStandard(); setTime, printMilitary, and printStandard are member functions. Time is the constructor.
Data Members
A data member can be of any valid C++ data type. For example
Ordinary variable primitive type An instance variable of another class programmer defined type A pointer or reference variable
The scope of an access modifier continues until another access modifier is mentioned explicitly There is a colon after every access modifier
public:
Public members can be accessed by member functions as well as by functions that declare the objects of that class. A struct has all members public by default. Normally, for classes, those member functions are made public which must interact with the outside world.
private:
Private members can only be accessed by member functions.
This means they can only be accessed in the member function body.
Remember the members mean both variables and functions. By default all class members are private, unless declared otherwise.
public: OR private:
It is not compulsory for all members functions to be public and all data members to be private. Some member functions can also be private. Similarly, data members can also be public.
Violation of Encapsulation principle
protected:
Similar to private they cannot be accessed directly from the outside world Mainly used with inheritance We will study this later Inheritance in C++
Initialization
Class data member cannot be initialized at declaration time. By default all data members have garbage value. If all members are declared public then, they can be initialized at object declaration time.
Similar to structure initialization time.
Member Functions
Member functions are those functions that you declare within the class definition. Definition of these functions must be provided, like any other function. There are a number of categories of functions.
Constructor/Destructor Regular member functions Overloaded operator functions Virtual functions Friend functions