Interview Questions(OOP)

From Hawk Wiki
Jump to: navigation, search

Object-oriented programming interview questions

Encapsulation

restricting access to some of the object's components
bundling of data with the methods
public and private data and methods.
A benefit of encapsulation is that it can reduce system complexity, and thus increases robustness, reduce risk of conflict between components.

Polymorphism

In C++, that type of polymorphism is called overloading. If a class has inherited a parent class, it can redefine a method and thus each class has a method with the same name but different functionality.

Inheritance

Inheritance is a way to reuse code of existing objects, establish a subtype from an existing object.
Allow replace the implementation of an inherited method or data.

Public Private Protected

A member (either data member or member function) declared in a private section of a class can only be accessed by member functions and friends of that class
A member (either data member or member function) declared in a protected section of a class can only be accessed by member functions and friends of that class, and by member functions and friends of derived classes
A member (either data member or member function) declared in a public section of a class can be accessed by anyone

Design Texas poker

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include "stdafx.h"
#include <iostream>
#include <stdio.h>
#include <vector>
#include <string.h>
#include <ctime>
using namespace std;
struct Card{
    enum suits{
    spades,
    hearts,
    clubs,
    diamonds
    };
    int suit;
    int rank;
    Card(int s, int r){
        suit=s;
        rank=r;
    };
    string toString(){
        char c[10];
        sprintf(c,"(%d,%d)",suit,rank);
        string s(c);
        return s;
    }
};
template <class t>
void MySwap(t &a,t &b){
    t tmp=a;
    a=b;
    b=tmp;
};
struct Deck{
    vector<Card> deck;
    Deck(){
        newDeck();
    }
    void newDeck(){
        deck.clear();
        for(int j=0;j<4;j++){
            for(int i=1;i<=13;i++){
                deck.push_back(Card(j,i));
            }
        }
        shuffle();
    }
    void shuffle(){
        srand(time(NULL));
        for(int i=0;i<deck.size();i++){
            int r = rand()%deck.size();
            MySwap(deck[i],deck[rand()%deck.size()]);
        }
    }
    Card drawOne(){
         Card ret = deck[deck.size()-1];
         deck.pop_back();
         return ret;
    }
    string toString(){
        string s;
        for(int i=0;i<deck.size();i++){
            s+=deck[i].toString();
        }
        return s;
    }
};
struct Hand{
    vector<Card> cards;
    void add(Card c){
        cards.push_back(c);
    }
    void remove(int i){
        cards.erase(cards.begin()+i);
    }
    string toString(){
        string s;
        for(int i=0;i<cards.size();i++){
            s+=cards[i].toString();
        }
        return s;
    }
};
int _tmain(int argc, _TCHAR* argv[])
{
    Deck deck1;
    Hand community;
    Hand hand1;
    Hand hand2;
    deck1.drawOne(); //texas, drop one card at beginning
    hand1.add(deck1.drawOne());
    hand1.add(deck1.drawOne());
    hand2.add(deck1.drawOne());
    hand2.add(deck1.drawOne());
    community.add(deck1.drawOne());//now draw 5 desk community cards
    community.add(deck1.drawOne());
    community.add(deck1.drawOne());
    community.add(deck1.drawOne());
    community.add(deck1.drawOne());
 
    cout<<"Player 1: "<<hand1.toString().c_str()<<endl;
    cout<<"Player 2: "<<hand2.toString().c_str()<<endl;
    cout<<"Community: "<<community.toString().c_str()<<endl;
    cout<<"Deck"<<deck1.toString().c_str()<<endl;
    return 0;
}