Skip to main content

Posts

Showing posts from December, 2019

Operator Overloading

What is operator overloading ? Operator overloading is the process of customizing C++ operators for operands of user defined types.   When you have two objects of a class- num1 and num2 , you can write a function to add them such as  ans = add(num1,num2); That does not look neither simple nor intuitive. You would prefer to write      ans = num1+num2; as you would write expressions for basic data types like integers, floats etc.    This can be done using Operator overloading. Operator overloading lets you write such statements. That is, it lets you call your functions on objects using  +, - ,* etc.    + operator will call addition function on the object (when you write op. overloading function for +). * will call multiply on objects etc. Names of overloaded operator functions start with keyword operator followed by  symbol of the operator. e.g. +, - etc. Unary operator functions take 0 parameters for members. The operand for these function is the object cal

MemoryAce for sharper mind

Remembering faces : We tend to forget faces and their names - unlike you have an excellent memory. MemoryAce app shows you some faces for few seconds and then shows the list with one face missing you need to identify the missing face. To avoid any problems with privacy laws, I have used all faces from Simpsons TV serial. So if you need an app to keep your mind and memory sharp, you must download the MemoryAce app from Hegdeapps .

Destructor

Similar to constructor, a class has another special member function called destructor . Destructor cleans up the object.   When an object goes out of scope, the destructor is automatically called. Destructor is also called when the pointer to the object is released using delete operator. Destructor has a same name as class and is preceded by tilde (~) symbol.   Class A has a destructor ~A()   Destructor should be used to clean up the object -  release memory and other resources, close files, stop threads etc. #include <iostream> using namespace std; class Arr { int * elements; int len; public: Arr( int len =5 ); ~ Arr(); //destructor int & operator []( int index); }; Arr :: Arr( int len) { elements = new int [len]; } Arr ::~ Arr() { delete []elements; } int & Arr :: operator []( int index) { return elements[index]; } int main() { Arr obj( 10 ); for ( int i =0 ;i <10 ;i ++ ) obj[i] = i * i; } In the

Find the error in C++ program

This C++ program is not compiling. What do you think is the error with the program? #include<iostream> using namespace std; int main() {    int arr[10];    arr={1,2,3,4,5,6,7,8};    cout<<"arr[0]="<<arr[0];    return 0; } Is the error due to Not using printf Initialising the array with only 8 elements instead of 10 Initialising array in the next statement instead of in definition. None of the above  Now if you like this question, there are plenty more questions like this and programs and notes in my app Simplified C++. Download the Simplif ied C++   by Hegdeapps now By the way the correct answer is (3)

Ten questions in C/C++

Let us see some questions in C and C++ Write printf statement in C to print - I got 98% in Maths Can you execute a function before main() in C? If yes, how is it done? Can you write a program to find if a number is even without using modulo operator? How do you define a data member which is common to all objects of a class in C++? Can we have a single constructor for a class, but still create objects from the class passing zero/one and two parameters? What problems might occur if a class has no default constructor? Is the following statement correct? fprintf(stdout,"Hello world");   Why do we use the following statement in C++ program? using namespace std;  Can you write a single statement to check if the number is a power of 2?  What does the following statement mean in C/C++?4 if(a) b++;    So we have 10 questions. How many of these can you answer?   Do you need more questions in C and C++?    You can find them in my two apps C Quizard and