Posts

Polymorphism in C++ (part 2)

               In the previous blog post, the definition,types and function overloading concepts are included. Here, is the link. https://rajeeva84.blogspot.com/2025/11/polymorphism-in-c.html in this post, operator overloading and run time polymorphism using virtual functions are explained. Operator overloading:               This concept deals with operator. An operator can be overloaded with another definition. For example, “%” is used as two purposes. One is finding ‘ mod ’ functions and another one is ‘ percentage ’. Let us create a c++ program as follows. #include < iostream > using namespace std ; class ModIt {     int m_value; public:     ModIt(int v) : m_value(v) {}        // let us Overload % operator     ModIt operator%(const ModIt& x) const {    ...

Polymorphism in C++

  What is polymorphism?               Poly means ‘Many’. Polymorphism means having more than one form. This concept makes the function can be used in different situations. Let us categories the types. 1.       Compile time Polymorphism 2.       Run time Polymorphism 1.Compile time Polymorphism:               This is a static one, which can be done in the code itself. It occurs in compile time. This can be achieved by two ways. ·        Function overloading ·        Operator overloading Let us create the c++ programs as follows. · Function Overloading:               In short, same function name can be used for different functionalities and different number of...

Encapsulation in C++

                Encapsulation is one of the object oriented programming concepts which binds the data and functions together. It makes the whole thing into a single unit. Generally, this single unit is considered as a class . Encapsulation is achieved by using access specifiers as follows.. ‘ public ’: member variables can be accessible for anywhere in the class. ‘ private ’: this type of variable accessed by current class only. ‘ protected ’: it can be accessible by class and derived class. Let us create a c++ program to display the student details. C++ program: Here, ‘ Student ’ is the class. It has set() method to display the data. The get() method gets the data. Using the object, we call the set() and get() function. Code: #include <iostream> using namespace std; class Student { private: string s_name; string s_id; int s_age; public: // Constructor Student(string s_name,string s_id ,int s_age) { ...

Abstraction in C++

              It is one of the Object oriented programming concepts which is the basic one. It deals with data. Abstraction shows the essential information to the user and hides the internal details. For e.g., when you drive a motor bike, you know about bike moving, but the functionality of engine is not known. This is the abstraction. Types of abstraction:               Abstraction is classified into two types as follows… ·        Data abstraction : it shows only necessary data. ·        Control abstraction : it shows only necessary information. Let us create the c++ program to implement abstraction. Code: #include <iostream> using namespace std; // Abstract class class ShapeEg { public:     // It is a virtual function     virtual void find_area() = 0;  ...

OOPS Concepts in C++

                OOP – Object Oriented Programming . It is a programming approach which deals with objects and classes . It creates reusable code to achieve modularity . Why OOPs concepts? It makes the code modular. The code can be reusable any times. It is scalable. It gives you security from unauthorized access. OOPs concepts: The concepts are listed below. Class Object Abstraction Encapsulation Polymorphism Inheritance Each one of the concepts are explained with code as follows… 1.Class:               It is a prototype from which the object are created. It has a collection of variables and methods.you can create multiple objects for single class. The class has following components. Class Components: Class name: Name of the class. Access specifiers: private, public and protected Methods: it defines the functionalities. Variables: it deals with val...

User defined data types in c++(Structure and Union)

                 Basically, the data types are categorised into two types. One is the primitive data types supported by programming language. Another one is user defined datatypes which is created by the user. Some the user defined datatypes in c++ are given below. ·        Structures ·        Unions Let us implement each one of this. Structures:               It is one of the user defined data types in c++, which has collection of members of different datatypes. Syntax: Creating a structure:   Structure structure_name {                 Data type data_member1;                 Data type data_member2;        ...

File handling in C++

                 Files are important feature in any programming language. File handling refers to reading and writing information in the files. Files have some formats like .txt,.csv and so on. C++ has lot of standard library files to handles file management. Eg: < fstream > header file   has ofstream , ifstream ,fstream. It also has ifstream(input file stream) and   ofstream(output file stream). Let us create objects and open the file as follows… Eg: ‘ifstream’ : ifstream filein("sample .txt "); ‘ofstream’ : ofstream fileout("sample.txt"); Where filein ,fileout are objects accordingly. If you are using ‘fstream’, it needs to open a file like this. ‘fstream fstr("sample.text", mode);’ Here, fstr is the object of fstream. “sample.txt” is the file name. Mode is   the purpose of operation going to be performed in the file. It has variety of types. Modes: ‘ ios::app ’ – append the contents to the en...