Difference between revisions of "Interview Questions(OOP)"

From Hawk Wiki
Jump to: navigation, search
(Virtual const)
(Public Private Protected)
 
(One intermediate revision by the same user not shown)
Line 15: Line 15:
 
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<br>
 
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<br>
 
A member (either data member or member function) declared in a public section of a class can be accessed by anyone<br>
 
A member (either data member or member function) declared in a public section of a class can be accessed by anyone<br>
===Private Inheritance===
+
===Design Texas poker===
Private inheritance is like making a composition.<br>
+
<pre class="brush:c">
One advantage to using protected/private inheritance instead of composition is that the derived class has access to protected members in the parent class. However this is kind of a double-edged sword, as it becomes easier to misuse the class.
+
#include "stdafx.h"
<pre>
+
#include <iostream>
class Motherboard {};
+
#include <stdio.h>
 
+
#include <vector>
// this makes a "has a" relationship
+
#include <string.h>
class Computer : private Motherboard
+
#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>
// this makes a similar "has a" relationship
+
void MySwap(t &a,t &b){
//  this approach is aka "composition"
+
t tmp=a;
class Computer
+
a=b;
{
+
b=tmp;
private:
+
  Motherboard mobo;
+
 
};
 
};
</pre>
+
struct Deck{
===Virtual Functions===
+
vector<Card> deck;
If the function is designated virtual in the base class then the derived class' function would be called (if it exists). If it is not virtual, the base class' function would be called.
+
Deck(){
To be short, virtual functions will have lowest priority to be called.
+
newDeck();
===Virtual inheritance===
+
}
To avoid ambiguous call of the same 2 levels higher base class. Declare virtual to say that the 2 levels higher base class is the same shared one.
+
void newDeck(){
<pre>
+
deck.clear();
class Animal {
+
for(int j=0;j<4;j++){
public:
+
for(int i=1;i<=13;i++){
  virtual void eat();
+
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{
// Two classes virtually inheriting Animal:
+
vector<Card> cards;
class Mammal : public virtual Animal {
+
void add(Card c){
public:
+
cards.push_back(c);
  virtual void breathe();
+
}
 +
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[])
class WingedAnimal : public virtual Animal {
+
public:
+
  virtual void flap();
+
};
+
+
// A bat is still a winged mammal
+
class Bat : public Mammal, public WingedAnimal {
+
};
+
</pre>
+
To avoid ambiguous between inherited WingedAnimal::Animal and Mammal:Animal
+
From the code above.Animal instance is unambiguous,and we can call Bat::eat().
+
===Virtual const===
+
<pre>
+
class Base
+
 
{
 
{
public:
+
Deck deck1;
    virtual void eval() const
+
Hand community;
    {
+
Hand hand1;
        std::cout<<"Base Const Eval\n";
+
Hand hand2;
    }
+
deck1.drawOne(); //texas, drop one card at beginning
};
+
hand1.add(deck1.drawOne());
 
+
hand1.add(deck1.drawOne());
class Derived:public Base
+
hand2.add(deck1.drawOne());
{
+
hand2.add(deck1.drawOne());
public:
+
community.add(deck1.drawOne());//now draw 5 desk community cards
    void eval()
+
community.add(deck1.drawOne());
    {
+
community.add(deck1.drawOne());
        std::cout<<"Derived Non-Const Eval\n";
+
community.add(deck1.drawOne());
    }
+
community.add(deck1.drawOne());
};
+
int main()
+
{
+
 
+
    Derived d;
+
    Base* pB=&d;
+
 
+
    pB->eval(); //This will call the Base eval()
+
  
    return 0;
+
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;
 
}
 
}
 
</pre>
 
</pre>
The example above base class evel() will be called.
 
In your Derived class, the prototype for eval doesn't match the one for the virtual function in Base. So it won't override the virtual function.
 
If you add the const for Derived::eval(), you should get virtual behavior.
 

Latest revision as of 00:01, 4 January 2013

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;
}