Object Oriented Programming

C++ Classes

class Box {

public:

double length; // Length of a box

double breadth; // Breadth of a box

double height; // Height of a box

};

Creating objects:

Box Box1;

Accessing data members (dot operator):

Box1.length

C++ Classes and Objects in detail: here

Abstraction

Using abstract class/Interface we express the intent of the class rather than the actual implementation.

Encapsulation

Encapsulation — private instance variable and public accessor methods.

Inheritence

Polymorphism

Polymorphism occurs when there is a hierarchy of classes and they are related by inheritance.

C++ polymorphism means that a call to a member function will cause a different function to be executed depending on the type of object that invokes the function. (Overriding)

Virtual Function

This could be fixed by using 'virtual' keyword for the parent function.

Defining a virtual function for a base class, with another version in a derived class, signals to the compiler that we don't want static linkage for this function. Instead, we want the function to be called at any given point in the program to be based on the kind of object for which it is called. This sort of operation is referred to as dynamic linkage, or late binding.

Pure Virtual Function

Defining virtual function in a base class so that it may be redefined in a derived class to suit the objects of that class, but that there is no meaningful definition you could give for the function in the base class.

virtual int area() = 0;

Overloading

More than one definition for an operator or a function with different parameters and usage ; used in the same scope.

Function overloading:

void print(double f) {

cout << "Printing float: " << f << endl;

}

void print(char* c) {

cout << "Printing character: " << c << endl;

}

Operator overloading:

class Box {

...

Box operator+(const Box& b) {

Box box;

box.length = this->length + b.length;

box.breadth = this->breadth + b.breadth;

box.height = this->height + b.height;

return box;

}

...

};

...

Box3 = Box1 + Box2;