Posts C/C++ Tricky Programs (Set 5)
Post
Cancel

C/C++ Tricky Programs (Set 5)


  • Can virtual functions be private in C++?

    In C++, virtual functions can be private and can be overridden by the derived class. For example, the following program compiles and runs fine.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<iostream> 
using namespace std; 
  
class Derived; 
  
class Base { 
private: 
    virtual void fun() { cout << "Base Fun"; } 
friend int main(); 
}; 
  
class Derived: public Base { 
public: 
    void fun() { cout << "Derived Fun"; } 
}; 
  
int main() 
{ 
   Base *ptr = new Derived; 
   ptr->fun(); 
   return 0; 
} 

Output

1
Derived fun()

Note: int main() is a friend of Base. If we remove this friendship, the program won’t compile. Read Me


  • When should we write our own assignment operator in C++?

    The answer is same as Copy Constructor. If a class doesn’t contain pointers, then there is no need to write assignment operator and copy constructor. The compiler creates a default copy constructor and assignment operators for every class. The compiler created copy constructor and assignment operator may not be sufficient when we have pointers or any run time allocation of resource like file handle, a network connection..etc.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35

#include<iostream> 
using namespace std; 
  
class Test 
{ 
    int *ptr; 
public: 
    Test (int i = 0)      { ptr = new int(i); } 
    void setValue (int i) { *ptr = i; } 
    void print()          { cout << *ptr << endl; } 
    Test & operator = (const Test &t); 
}; 
  
Test & Test::operator = (const Test &t) 
{ 
   // Check for self assignment 
   if(this != &t) 
     *ptr = *(t.ptr); 
  
   return *this; 
} 
  
int main() 
{ 
    Test t1(5); 
    Test t2; 
    t2 = t1; 
    t1.setValue(10); 
    t2.print(); 
    return 0; 
} 


Output

1
5

Read Me


  • What Is The Memory Structure Of An Object?

    Usually C++ objects are made by concatenating member variables.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
     class Test

     {

      int i;

      float j;

     };

is represented by an int  followed by a float.

     class TestSub: public Test

     {

      int k;

     };

The above class is represented by Test and then an int(for int k). So finally it will be int, float and int.
In addition to this each object will have the vptr(virtual pointer) if the class has virtual function, usually as the first element in a class.


  • C++ program to recursively calculate the sum of digits of a number until it becomes a single-digit number.

    Calculating recursive sum by adding all digits of the number and then see if it is single digit then stop otherwise recalculate the sum until the sum becomes single digit.
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
using namespace std;
int main() {
   int a = 534;
   int sum;
   if(a)
      sum = a % 9 == 0 ? 9 : a % 9 ;
   else
      sum = 0;
   cout<<"The final sum is "<<sum;
   return 0;
}

Output

1
The final sum is 3

Source

GeeksforGeeks
Quora
Software Testing Help
Tutorialspoint
This post is licensed under Ravi Jain by the author.