Showing posts with label OOPS Concept. Show all posts
Showing posts with label OOPS Concept. Show all posts

Tuesday, June 5, 2012

What do you mean by Object Slicing?


When a derived class object is assigned to a base class, only the base class's part of content in the derived object are copied to the base class, leaving behind the derived class specific contents. This is referred as Object Slicing.

Class BaseClass
{
public int i;
};

class DerivedClass : public BaseClass
{
public int j;
};

int main()
{
BaseClass ObjectOfB;
DerivedClass ObjectOfD;
ObjectOfB = ObjectOfD;
//Here ObjectOfD contains both i and j.
//But only i is copied to ObjectOfB.
}

Tuesday, March 27, 2012

Where is pass by reference is useful?

1.
Suppose you have large object to pass to a function, better to pass by reference instead of passing bye value or the object itself.

2.
Least Memory Management.

3.
No 0 values possible -> Called function will know that the object have been created and you are passing address of that object.

4.
New variable are not created, rather already created objects or variable are used.

5.
We can return multiple values from a function.

6.
It allows us to have the function change the value of the argument, which is sometimes useful.

7.


more practical reason will come soon ......

Thursday, March 1, 2012

Difference between ref and out parameters in .NET


The out and the ref parameters are used to return values in the same variables, that you pass an an argument of a function. These both parameters are very useful when your function needs to return more than one values.


                The out Parameter
The out parameter can be used to return the values in the same variable passed as a parameter of the function. Any changes made to the parameter will be reflected in the variable.

Tuesday, January 24, 2012

What is an ABC?

An abstract base class.
At the design level, an abstract base class (ABC) corresponds to an abstract concept. If you asked a mechanic if he repaired vehicles, he'd probably wonder what kind-of vehicle you had in mind. Chances are he doesn't repair space shuttles, ocean liners, bicycles, or nuclear submarines. The problem is that the term "vehicle" is an abstract concept (e.g., you can't build a "vehicle" unless you know what kind of vehicle to build). In C++, class Vehicle would be an ABC, with Bicycle, SpaceShuttle, etc, being derived classes (an OceanLiner is-a-kind-of-a Vehicle). In real-world OO, ABCs show up all over the place. 

At the programming language level, an ABC is a class that has one or more pure virtual member functions. You cannot make an object (instance) of an ABC.

What is a "virtual constructor"?

An idiom that allows you to do something that C++ doesn't directly support.
You can get the effect of a virtual constructor by a virtual clone() member function (for copy constructing), or a virtual create() member function (for the default constructor).
 class Shape {
 public:
   virtual ~Shape() { }                 
// A virtual destructor
   virtual void draw() = 0;             
// A pure virtual function
   virtual void move() = 0;
   
...
   virtual Shape* clone()  const = 0;   
// Uses the copy constructor
   virtual Shape* create() const = 0;   
// Uses the default constructor
 };

 class Circle : public Shape {
 public:
   Circle* clone()  const;   
// Covariant Return Types; see below
   Circle* create() const;   
// Covariant Return Types; see below
   
...
 };

 Circle* Circle::clone()  const { return new Circle(*this); }
 Circle* Circle::create() const { return new Circle();      } 
 

When should my destructor be virtual?

When someone will delete a derived-class object via a base-class pointer.
In particular, here's when you need to make your destructor virtual:
  • if someone will derive from your class,
  • and if someone will say new Derived, where Derived is derived from your class,
  • and if someone will say delete p, where the actual object's type is Derived but the pointer p's type is your class.

How can a member function in my derived class call the same function from its base class?

Use Base::f();
Let's start with a simple case. When you call a non-virtual function, the compiler obviously doesn't use the virtual-function mechanism. Instead it calls the function by name, using the fully qualified name of the member function. For instance, the following C++ code...
 void mycode(Fred* p)
 {
   p->goBowling();  
← pretend Fred::goBowling() is non-virtual
 }
...might get compiled into something like this C-like code (the p parameter becomes the this object within the member function):
 void mycode(Fred* p)
 {
   __Fred__goBowling(p);  
← pseudo-code only; not real
 } 
 

What happens in the hardware when I call a virtual function? How many layers of indirection are there? How much overhead is there?

The answer is entirely compiler-dependent, so your mileage may vary, but most C++ compilers use a scheme similar to the one presented here.
Let's work an example. Suppose class Base has 5 virtual functions: virt0() through virt4().
 // Your original C++ source code

 class Base {
 public:
   virtual 
arbitrary_return_type virt0(...arbitrary params...);
   virtual 
arbitrary_return_type virt1(...arbitrary params...);
   virtual 
arbitrary_return_type virt2(...arbitrary params...);
   virtual 
arbitrary_return_type virt3(...arbitrary params...);
   virtual 
arbitrary_return_type virt4(...arbitrary params...);
   
...
 }; 

What's the difference between how virtual and non-virtual member functions are called?

Non-virtual member functions are resolved statically. That is, the member function is selected statically (at compile-time) based on the type of the pointer (or reference) to the object. 

In contrast, virtual member functions are resolved dynamically (at run-time). That is, the member function is selected dynamically (at run-time) based on the type of the object, not the type of the pointer/reference to that object. This is called "dynamic binding." Most compilers use some variant of the following technique: if the object has one or more virtual functions, the compiler puts a hidden pointer in the object called a "virtual-pointer" or "v-pointer." This v-pointer points to a global table called the "virtual-table" or "v-table." 

How can C++ achieve dynamic binding yet also static typing?

When you have a pointer to an object, the object may actually be of a class that is derived from the class of the pointer (e.g., a Vehicle* that is actually pointing to a Car object; this is called "polymorphism"). Thus there are two types: the (static) type of the pointer (Vehicle, in this case), and the (dynamic) type of the pointed-to object (Car, in this case). 

What is a "virtual member function"?

A virtual function allows derived classes to replace the implementation provided by the base class. The compiler makes sure the replacement is always called whenever the object in question is actually of the derived class, even if the object is accessed by a base pointer rather than a derived pointer. This allows algorithms in the base class to be replaced in the derived class, even if users don't know about the derived class.

Monday, January 16, 2012

Tell Something about class/object life cycle...





  1. Load the assembly
  2. Execute static initialisers
  3. "new" call:
    • allocate memory
    • execute non-static initialisers
    • execute constructor
  4. the instance is now ready to be used
  5. after the last reference to the object has vanished: if the object has no finalizer, it is now ready for collection; if the object has a finalizer, it is put on the finalizer queue.
  6. (optional) the objects from the finalizer queue have their finalizer called in a special thread; if there is still no reference from the application to the object, it too becomes now eligible for garbage collection
  7. the garbage collector deallocates memory

Sunday, January 15, 2012

What is Singleton Class in C++?

A class whose number of instances that can be instantiated is limited to one is called a singleton class. Thus, at any given time only one instance can exist, no more.

 The singleton design pattern is used whenever the design requires only one instance of a class. Some examples:
  • Application classes. There should only be one application class. 
  • Logger classes. For logging purposes of an application there is usually one logger instance required.
There are several ways of creating a singleton class. The most simple approach is shown below:


class Singleton
{
public:
static Singleton& Instance()
{ static Singleton singleton;
return singleton;
}
// Other non-static member functions
private:
Singleton() {}; // Private constructor
Singleton(const Singleton&); // Prevent copy-construction
Singleton& operator=(const Singleton&); // Prevent assignment
};



Saturday, January 14, 2012

How page request is handled in Asp.Net

ASP.NET Page Handling








Click here for larger image.




ms972976.viewstate_fig02(en-us,MSDN.10).gif


When to use Abstract Class and when to use Interface in project?

Use interface when you do not want to give any implementation and use abstract class when you want to give some common implementation that would be used by classes derived from that abstract class. An abstract class can be skeleton implementation of existing interface. 




Use abstract clas for "is A" relationship,  while use interfaces for "Can do" relationship.

Example:



Live

Your Ad Here