You are on page 1of 17

Object Oriented Programming using C++

lecture 08 (Access Specifiers)

Object Oriented Concepts


Compare and contrast the structure, and class in C++ OOP Features What is the concept of CLASS? How to define and implement a class? Can we access the class members?
Why Public or Private?

Structure in C++ vs. Classes


Similarities
Both are derived types. Both present similar features Both are automatic typedefs

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

Classes are more advanced than structures

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.

C++ Class Example


1 class Time { 2 public: Public: and Private: are member-access specifiers.

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.

7 private: 8 9 10 11 }; int hour; int minute; int second; // 0 - 23 // 0 - 59 // 0 - 59

hour, minute, and second are data members.

C++ Class Declaration


To declare a class The class keyword is used The declaration is enclosed in braces {}; The class definition ends with a semicolon Every piece of code inside the braces is called class body The class body thus contains class members

C++ Class Class Members


A classs declaration declares the classs members, which may consist of

The data members


The constructor & destructor functions
Specialized member functions

Other member functions


Include the functionality that can be invoked on the object of this class (or type)

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

C++ Access Modifiers


A class contains access keywords, that group individual members together. Three types of Access Modifiers:
private: public: protected:

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.

Otherwise we require a constructor for this.

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

You might also like