Dynamic memory allocation in C++

Dynamic memory allocation:- In C, we can allocate dynamic memory by using malloc, calloc ande realloc library functions. All the above functions are applicable in C++. But there are some disadvantages in C++. To avoid those disadvantages C++ provides its own memory allocation and de-allocation mechanism For dynamic memory allocation in C++ we have to use ‘new’ operator and to de-allocate that memory we have to use ‘delete’ operator. When we allocate a memory by using ‘new’ then we can not de-allocate a memory by using free(). ‘new’ operator internally calls malloc and…Read more

Virtual Destructors and function chaining in C++

Virtual Destructor:- As like a normal function we can create a destructor as virtual. If the class contains at least a single virtual destructor then VPTR is created and it is initialized with appropriate v-table. Virtual destructors are necessary if there is an inheritance and up casting concept is used. If virtual destructors are not written then there is a chance of a memory leak. e.g. class base { public: base() { cout<<”base class constructor.”; } ~base() { cout<<”base class destructor.”; } }; class derived:public base { derived() { cout<<”derived class constructor.”; }…Read more

Operator overloading in C++

Operator overloading:- Operator overloading is a syntactic sugar. It is the type of compile time polymorphism. Overloaded operators are used for class objects or other data members other than basic data types. When we overload an operator we do not have to call explicitly that overloaded function. It is a rule that, 1)    Do not change the meaning of previously defined operator (but we can change the meaning). 2)    Do not change precedence and associatively of that operator. We can provide an overloaded function of an operator as a member function or a…Read more

Friend Concept and ‘mutable’ Keyword in C++

Friend:- By using a friend concept we can access a private member of a class. There are multiple types of a members can be friend of our class. 1)    A friend can be any member function of a class. 2)    A friend can be any function which is not a part of any class. 3)    Whole class can be a friend of our class. Before using friend we have to use a forward declaration means we have to declare friend class at the start of a program. class friend_class; void fun(friend_class *fp) {…Read more

Inheritance in C++

Composition:- A Composition is similar as an inheritance. A Composition means writing an object of some another class inside some another class. This gives a same effect as an inheritance but there are some functional differences in an inheritance and a composition. e.g. class demo { public: int i; void fun() { cout<<”inside the fun of a demo class.” } }; class hello { public: int x; demo d; void gun() { cout<<”inside the gun of a hello class.”; } }; int main() { demo d1; hello d2; d2.d.fun(); d1.fun(); return 0; }…Read more

File I/O Solution in C

This is a full  file I/O  solution where user enters some information on the terminal and this user input is then stored into the file. Also this is a structured file operation program which supports : 1) Insertion of a record 2) Deletion of a record 3) Updation of a record 4) Searching of a record Note: This program assumes that user enters the data in the correct specified format and order as required for further processing. i.e. Where the input is number and user enters a character , the program will go…Read more

Local, Nested Class and ‘this’ pointer in C++

Local Class:-   • If a class is defined inside any function then that class is called as local class. • This local class is used only inside a particular function. • We can not create an object of this local class outside that function. • According to above point local class works same as a local variable inside a function. e.g. class hello { void fun() { class demo { public: int i; }; demo d; d.i=10; } // function ends. }; • In an above example, demo class is considered as…Read more

Constructors, Destructors and Inline Function in C++

Constructors:- Constructor is a user defined function whose name is same as the class name. Constructor is called as a user defined function, because, a programmer can provide its own constructor with its own functionality. Constructor is a first function which is called implicitly in the objects lifetime. In the constructor , memory is not allocated for the object but constructor is responsible for following things, 1)    It is responsible for initializing memory allocated for that object. 2)    Constructor is responsible for setting appropriate environment for a class. 3)    If our class contains…Read more

Function Overloading (C++)

Function Overloading:-   Function overloading is compile time polymorphism. Appropriate function call is resolved at compile time only. This means writing a function with a same name but with the different definations. There are some rules to write a n overloaded functions. Functions are overloaded on to the basis of number of parameters and type of parameters. C++ compilers differentiate the overloaded function by using naming decoration or name mangling. We can also overload constructor but not destructor. We can not overload a function depend on its return type value because at the…Read more

Constant concepts (C++) with Examples

Constant concepts C++:- Constant is a data type qualifier which provide extra quality to our normal variable and that quality is :- After initializing a constant vairable with appropriate value which we can not change that initialized value. In C and C++, rules about constant variable is that we have to initialize that constant variable at the time of declaration of that variable. We can not initialize the variable after declaring it.   const int I; i=10; // Error   In this case, if the variable I is the local variable then at…Read more