1 of 23

Object Oriented Programming

  • OOP is a programming style that is focused on objects
  • Important Features of OOP
    • Abstraction
    • Encapsulation
    • Inheritance
    • Polymorphism

2 of 23

Abstraction

  • Data abstraction refers to providing only essential information to the outside world and hiding their background details.
  • Real life example of a TV, which you can turn on and off, change the channel, adjust the volume, and add external components such as speakers, VCRs, and DVD players, BUT you do not know its internal details, that is, how it receives signals over the air or through a cable, how it translates them, and finally displays them on the screen.

3 of 23

Abstraction

  • In C++, classes provides great level of data abstraction. They provide sufficient public methods to the outside world to play with the functionality of the object and to manipulate object data, i.e., state without actually knowing how class has been implemented internally.
  • In C++, we use classes to define our own abstract data types (ADT). You can use the cout object of class ostream to stream data to standard output .
  • you don't need to understand how cout displays the text on the user's screen. You need to only know the public interface and the underlying implementation of ‘cout’ is free to change.

4 of 23

Encapsulation

  • Encapsulation is an Object Oriented Programming concept that binds together the data and functions that manipulate the data, and that keeps both safe from outside interference and misuse.
  • Data encapsulation led to the important OOP concept of data hiding.
  • Data encapsulation is a mechanism of bundling the data, and the functions that use them and data abstraction is a mechanism of exposing only the interfaces and hiding the implementation details from the user.

5 of 23

Inheritance

  • Feature that enables the characteristics or properties of a parent to reach its child
  • C++ supports inheritance
  • A class can inherit one or more classes
  • Inherited class is called as parent class or super class or base class
  • Class that inherits a parent class is called as child class or sub class or derived class

6 of 23

Polymorphism

  • Poly – Many
  • Morph – Form
  • Polymorphism is the characteristic that enables an entity to exist in more than one form
  • C++ supports function overloading and operator overloading to implement polymorphism

7 of 23

C++ Classes �&�Objects�

8 of 23

Classes & Objects

  • Object-oriented programming (OOP)
    • Encapsulates data (attributes) and functions (behavior) into packages called classes.

  • So, Classes are user-defined (programmer-defined) types.
    • Data (data members)
    • Functions (member functions or methods)

9 of 23

Classes in C++

  • A class definition begins with the keyword class.
  • The body of the class is contained within a set of braces, { } ; (notice the semi-colon).

class class_name

{

….

….

….

};

Class body (data member + methods)

Any valid identifier

10 of 23

Classes in C++

  • Within the body, the keywords private: and public: specify the access level of the members of the class.
    • the default is private.

  • Usually, the data members of a class are declared in the private: section of the class and the member functions are in public: section.

11 of 23

Classes in C++

class class_name

{

private:

public:

};

Public members or methods

private members or methods

12 of 23

Classes in C++

  • Member access specifiers

    • public:
      • can be accessed outside the class directly.
        • The public stuff is the interface.

    • private:
      • Accessible only to member functions of class
      • Private members and methods are for internal use only.

13 of 23

Class Example

  • This class example shows how we can encapsulate (gather) a circle information into one package (unit or class)

class Circle

{

private:

double radius;

public:

void setRadius(double r); double getDiameter();

double getArea();

double getCircumference();

};

No need for others classes to access and retrieve its value directly. The

class methods are responsible for

that only.

They are accessible from outside

the class, and they can access the

member (radius)

14 of 23

Creating an object of a Class

  • Declaring a variable of a class type creates an object. You can have many variables of the same type (class).

  • Once an object of a certain class is created, a new memory location is created for it to store its data members and code.

  • You can create many objects from a class type.
    • Ex) Circle c1,c2;

15 of 23

Special Member Functions

  • Constructor:
    • Public function member
    • called when a new object is created (instantiated).
    • Initialize data members.
    • Same name as class
    • No return type
    • Several constructors
      • Function overloading

16 of 23

Special Member Functions

class Circle

{

private:

double radius;

public:

Circle();

Circle(int r);

void setRadius(double r);

double getDiameter();

double getArea();

double getCircumference();

};

Constructor with no argument

Constructor with one argument

17 of 23

Implementing class methods

  • Class implementation: writing the code of class methods.
  • There are two ways:
    1. Member functions defined outside class
      • Using scope resolution operator (::)

    • Format for defining member functions

ReturnType ClassName::MemberFunctionName( ){

}

18 of 23

Implementing class methods

  1. Member functions defined inside class
    • Do not need scope resolution operator, class name;

19 of 23

Accessing Class Members

  • Operators to access class members
    • Identical to those for structs
    • Dot member selection operator (.)

20 of 23

Destructors

  • Destructors
    • Special member function
    • Same name as class
      • Preceded with tilde (~)
    • No arguments
    • No return value
    • Cannot be overloaded
    • Before system reclaims object’s memory
      • Reuse memory for new objects
      • Mainly used to de-allocate dynamic memory locations

21 of 23

Structure vs class in C++�

22 of 23

Members of a class

  • Members of a class are private by default

And

  • Members of a structure are public by default.

23 of 23

// Program 1

  class Test

{

    int x; // x is private

};

int main()

{

  Test t;

  t.x = 20;

// compiler error because x is private

 

  return 0;

}

  

struct Test

{

    int x;

// x is public

};

int main()

{

  Test t;

  t.x = 20; // works fine because x is public

  

  return 0;

}