Simple GUI Notepad Using Ruby

GUI Notepad Using Ruby Code require 'tk' class Notepad def saveFile file = File.open("note", "w") ...

Thursday, January 29, 2015

Constructors

Constructor

In class based object oriented programming constructor is a special member function (or an subroutine) that is called to create object of a particular class. It prepares the object for use.

Properties

> A constructor will have same name as the class (exceptions: python, php, ruby ...)
> It does not have any explicit return type at all not even void. (as they always return the object of the existing class.).
> The constructor is called each time the object declaration is encountered.
> Like other functions constructor also have parameter list, and a (possibly empty) body.
> A class can have multiple constructor.
>Unlike other member function constructor must not declare as const.

Constructors Types
Constructor Types

Default Constructor

Default constructor (or synthesized default constructor) is a constructor generated automatically by the C++ compiler in the absence of any programmers defined constructor. A use defined no argument constructor is also called default. A parametrized constructor with all its arguments as default arguments is also called a default constructor.



Programming Problem Two

Problem Statement:   Shashank likes strings in which consecutive characters are different. For example, he likes ABABA, while he dosen't like ABAA. Given a string congaing characters A and B only, he wants to change it to a string he likes. To do so he is allowed to delete the characters in the string. The problem is to fin d the minimum number of deletion required.

For example:

ABBAB -> ABAB (Requires 1 deletion)

AAAAAA -> NULL (Requires 6 deletion)
ABABA -> ABABA (Requires 0 deletion)

C++ Code:

    1. #include <iostream>
    2. #include <string>
    3. #include <cstring>
    4. using namespace std;

    5. int delRequired(string testString)
    6. {
    7. size_t size = testString.length();
    8. char arr[size+1];
    9. strcpy(arr,testString.c_str());
    10. int i, noOfDel = 0;
    11. for(i = 0; i < size; ++i) {
    12. if(arr[i] == arr[i+1]) {
    13. noOfDel += 1;
    14. }
    15. }
    16. return noOfDel;
    17. }

    18. int main()
    19. {
    20. int testCase, i = 0;
    21. cin >> testCase;
    22. int delArr[testCase];
    23.  
    24. while(testCase--) {
    25. string testString;
    26. cin >> testString;
    27. delArr[i++] = delRequired(testString);
    28. }
    29. delArr[i] = -1;
    30. i = 0;
    31. while(delArr[i] != -1) {
    32. cout << delArr[i] << endl;
    33. i += 1;
    34. }
    35. return 0;
    36. }

    Programming Problem One

    Problem Statement:   Given two numbers (int) L (low) and H (high) , The problem is to find the maximum value of A xor B such that  L ≤ A ≤ B ≤ U.

    C++ Code:

    1. #include <iostream>
    2. using namespace std;

    3. int maxXor(int l, int r) 
    4. {
    5. int max = 0;
    6. for (int i = l; i <= r; ++i) {
    7. for (int j = l; j <= r; ++j) {
    8. if((i ^ j) >= max) {
    9. max = i ^ j;
    10. }
    11. }
    12. }
    13. return max;
    14. }
    15.  
    16. int main() 
    17. {
    18. int res, l, r;
    19.  
    20. cin >> l;
    21. cin >> r;
    22.  
    23. res = maxXor(l, r);
    24. cout << res;
    25.   return 0;
    26. }