Skip to content

Instantly share code, notes, and snippets.

@hasinur1997
Last active March 23, 2019 14:53
Show Gist options
  • Save hasinur1997/3ec0757798d9c266355cf9b2df414fcf to your computer and use it in GitHub Desktop.
Save hasinur1997/3ec0757798d9c266355cf9b2df414fcf to your computer and use it in GitHub Desktop.

OOP

What is OOP(Object Oriented Programming) ?

Object-oriented programming (OOP) is a programming paradigm based on the concept of "objects", which may contain data, in the form of fields, often known as attributes; and code, in the form of procedures, often known as methods.

Object

Objects are basic run-time entities in an object oriented system, objects are instances of a class these are defined user defined data types.

Example:

class Person { 
  char name[20]; 
  int id; 
  public: 
      void getdetails(){} 
}; 

int main(){ 
 Person p1;   
} 

Here p1 is an object.

Class

Class is a blueprint of data and functions or methods. Class does not take any space.Class is a user defined data type like structures and unions in C.

Syntax for class

class class_name { 
  private: 
     //data members and member functions declarations 
  public: 
     //data members and member functions declarations 
  protected: 
     //data members and member functions declarations 
};

ENCAPSULATION:

Combining data and functions into a single unit called class and the process is known as Encapsulation.Data encapsulation is important feature of a class. Class contains both data and functions.

Inheritance

it is the process by which object of one class aquire the properties or features of objects of another class. The concept of inheritance provide the idea of reusability means we can add additional features to an existing class without Modifying it.

Polymorphism:

A greek term means ability to take more than one form. An operation may exhibite different behaviours in different instances. The behaviour depends upon the types of data used in the operation.

Data Abstraction:

Abstractions refer to the act of representing essential features without including background details or explanation. They are commonly known as Abstraction Data Type(ADT).

Exception handling:

Exception handling is the process of responding to the occurrence, during computation, of exceptions – exceptional conditions requiring special processing – often changing the normal flow of program execution.

Template

Template is simple and yet very powerful tool in C++. The simple idea is to pass data type as a parameter so that we don’t need to write same code for different data types.

Access Modifiers

Access modifiers or Access Specifiers in a class are used to set the accessibility of the class members. That is, it sets some restrictions on the class members not to get directly accessed by the outside functions.

There are three kinds of access modifiers. Public, Private and Protected.

Public:

All the class members declared under public will be available to everyone. The data members and member functions declared public can be accessed by other classes too. The public members of a class can be accessed from anywhere in the program using the direct member access operator (.) with the object of that class.

Private:

Private: The class members declared as private can be accessed only by the functions inside the class. They are not allowed to be accessed directly by any object or function outside the class. Only the member functions or the friend functions are allowed to access the private data members of a class.

Protected:

Protected access modifier is similar to that of private access modifiers, the difference is that the class member declared as Protected are inaccessible outside the class but they can be accessed by any subclass(derived class) of that class.

Member function:

Those function are decleared inside the class are called member function.

Functions

function:

A function is a block of organized, reusable code that is used to perform a single, related action.

Example

int add(int a, int b) {
  int result;
  result = a + b;
  return result;
}

This is a function.

There are three kinds of function in OOP (Object oriented programming).

  1. Inline function
  2. Outline function
  3. Stand alone function

Inline function:

The function that decleared with body inside the class is called inline function.

Example of inline function

class Animal {
    void getAlnimal() {
      int a, b;
    }
};

Advantage of inline function

It faster in execution.

Disadvantage of inline function

It needs more memory space.

Outline function:

The function that decleared without body iside the class is called outline function.

Example of outline function

class Animal {
 voi show();
 };
 
 void Animal::show() {
  int a, b;
 }
Advantage of outline function

It needs less memory space.

Disadvantage of outline function

It is slow in execution.

Stand Alone function

A stand-alone function is just a normal function that is not a member of any class and is in a global namespace.

Constructor function

The function with the same name of class decleared inside the class is called constructor function. Constructor function is execute when object created.

Example of constructor function

class Student {
   int student() {
    int id;
    char name[40];
   }
};

Variable

Variable is a container to store data. Variable are differents type. Integer, character, float, double. differents types of variable store differents types data.

Local Variable

The varible which is decleared inside a function or as a function parameter is called local variable.

Example of local variable
int call(int a){
  char name;
}

In the following function a is local variable as a function parameter and name is also local variable decleared inside the body of the function.

Global Variable

The variable is decleared in outside of all function and it should be called any where of scope is called global variable.

Example of gloabl variable
int data;

int callIt() {
 data = 10;
 printf(data);
}

int do() {
 data = 30;
 printf(data);
}

In the following example we can say data is the global variable. Because it is called from differets function.

Member variable

The variable decleared inside the class is called member variable.

Example of member variable

class Student {
   public:
   int data;
 }
};

In the following example data is a member variable.

Constant member function

When const keyword is used to declear a function and the value of member variable can not be changed within the function is called constant member function.

Why use constant member function ?

When member variable can't be modified then we need to decleare constant member function.

Example of constant member function

class Beautiful {
 public:
  void see() const {
   cout << "I can see beautiful nature";
  }
}

In the following class, can see that the see() function is constant member function.

Static member variable

In computer programming, a static variable is a variable that has been allocated "statically", meaning that its lifetime is the entire run of the program.

Why we use static member variable ?

We can define class members static using static keyword. When we declare a member of a class as static it means no matter how many objects of the class are created, there is only one copy of the static member.

A static member is shared by all objects of the class. All static data is initialized to zero when the first object is created, if no other initialization is present.

Example of static member variable

class Beautiful {
 public:
  int static count;
  
  void show() {
   cout << count;
   }
 };
 
 int Beautiful::count;
 
 int main() {
  Beautiful B;
  B.count;
 }
 

Some Code for practice of various functions

Constructor function

class A {
 public:
  A(){
   cout << "This is constructor function";
  }
};

int main() {
 A a;
}

Destructor function

Destructor is a member function which destructs or deletes an object.

Why use destructor function ?

When need to delete or destruct an object it should be decleared desctructor function.

Example of desctructor function

class Test {
 public:
  ~Test() {
    cout << "This is destructor function";
   }

When is destructor called?

A destructor function is called automatically when the object goes out of scope: (1) the function ends (2) the program ends (3) a block containing local variables ends (4) a delete operator is called

Inline function

class A {
 public:
  void get_roll() {
   cout << "This is inline funciton";
  }
};

int main() {
 A a;
 a.get_roll();
}

Outline function

class A {
  public:
   void show_something();
};

void A::show_something() {
 cout << "This is outline function";
}

int main() {

 A a;
  a.show_something();
}

Constructors

Classification of constructors

1. Non parameterized constructor
2. Parameterized Constructor 
3. Copy Constructor

Why use constructor function ?

To initialize member variable, then constructor function should be decleare. If we need to autometically call any member function it should be called inside the constructor function.

Non parameterized constructor

When a function is decleared with same name of class and autometically called when object created of that class is called constructor function. The constructor function which is delceared without parameter is called non parameterized cosntructor function.

Why use non parameterized constructor

When it doesn't need to pass any value withing the object, then it should be decleared non parameterized constructor.

Example of non parameterized constructor

class A {
  public:
  A() {
  
  }
};

int main() {
 A a;
}

Parameterized Constructor

When a constructor function is decleared with parameter and when it is called it need to accept parameter value that is called parameterized constructor.

Why use parameterized constructor ?

When we nedd to pass any value within the object, it should be decleared parameterized constructor.

Advantages of Parameterized Constructor

  1. Initialize the member variable
  2. It is possible to change default value of constructor parameter

Example of parameterized constructor

class A {
 public:
   A(int a, int b) {
     cout <<a<<b<<endl;
   }
};

int main() {
 A a(10, 20);
}

Copy constructor

A copy constructor is a member function which initializes an object using another object of the same class.

Why use copy constructor ?

  1. When instantiating one object and initializing it with values from another object (as in the example above).
  2. When passing an object by value.
  3. When an object is returned from a function by value.
The rules of writing copy constructor

ClassName (ClassName &old_obj);

Example of copy constructor

class Point { 
   private: 
   int x, y; 
   public: 
       Point(int x1, int y1) { 
         x = x1; y = y1; 
       } 

       // Copy constructor 
       Point(Point &p2) {
         x = p2.x; y = p2.y; 
       }
 }; 
 
int main() 
{ 
   Point p1(10, 15);
   Point p2 = p1; 
 
}

Function overloading

Function overloading is a feature in C++ where two or more functions can have the same name but different parameters. Function overloading can be considered as an example of polymorphism feature in C++.

Example of function overloading

#include <iostream> 
using namespace std; 
  
void print(int i) { 
  cout << " Here is int " << i << endl; 
} 
void print(double  f) { 
  cout << " Here is float " << f << endl; 
} 
void print(char const *c) { 
  cout << " Here is char* " << c << endl; 
} 
  
int main() { 
  print(10); 
  print(10.10); 
  print("ten"); 
  return 0; 
}

After Midterm Exam

What is inheritance ?

The capability of a class to derive properties and characteristics from another class is called Inheritance.

Why user inheritance ?

To reduce the duplication of code then inheritance is the best.

Sub Class:

The class that inherits properties from another class is called Sub class or Derived Class.

Super Class:

The class whose properties are inherited by sub class is called Base Class or Super class.

Modes of Inheritance

Public mode:

If we derive a sub class from a public base class. Then the public member of the base class will become public in the derived class and protected members of the base class will become protected in derived class.

Protected mode:

If we derive a sub class from a Protected base class. Then both public member and protected members of the base class will become protected in derived class.

Private mode:

If we derive a sub class from a Private base class. Then both public member and protected members of the base class will become Private in derived class.

Types of Inheritance:

Single Inheritance: In single inheritance, a class is allowed to inherit from only one class. i.e. one sub class is inherited by one base class only.

Example of single inheritance

#include <iostream> 
using namespace std; 
  
// base class 
class Vehicle { 
  public: 
    Vehicle() 
    { 
      cout << "This is a Vehicle" << endl; 
    } 
}; 
  
// sub class derived from two base classes 
class Car: public Vehicle{ 
  
}; 
  
// main function 
int main() 
{    
    // creating object of sub class will 
    // invoke the constructor of base classes 
    Car obj; 
    return 0; 
} 

Multiple Inheritance

Multiple Inheritance is a feature of C++ where a class can inherit from more than one classes. i.e one sub class is inherited from more than one

Example of Multiple inheritance

// multiple inheritance 
#include <iostream> 
using namespace std; 
  
// first base class 
class Vehicle { 
  public: 
    Vehicle() 
    { 
      cout << "This is a Vehicle" << endl; 
    } 
}; 
  
// second base class 
class FourWheeler { 
  public: 
    FourWheeler() 
    { 
      cout << "This is a 4 wheeler Vehicle" << endl; 
    } 
}; 
  
// sub class derived from two base classes 
class Car: public Vehicle, public FourWheeler { 
  
}; 
  
// main function 
int main() 
{    
    // creating object of sub class will 
    // invoke the constructor of base classes 
    Car obj; 
    return 0; 
} 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment