Interview Questions(OOP)
Contents
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
#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;
}