Professional Documents
Culture Documents
Virtual Function:
In object-oriented programming, a virtual function or virtual method is a
function or method whose behaviour can be overridden within an inheriting class by
a function with the same signature. This concept is a very important part of the
polymorphism portion of object-oriented programming (OOP).
Friend function:
A friend function is used in object-oriented programming to allow access to private or protected
data in a class from the outside. Normally, a function that is not a member of a class cannot
access such information; neither can an external class. Occasionally, such access will be
advantageous for the programmer. Under these circumstances, the function or external class can
be declared as a friend of the class using the friend keyword. The function or external class will
then have access to all information public, private, or protected within the class.
This procedure should be used with caution. If too many functions or external classes are
declared as friends of a class with protected or private data, it lessens the value of encapsulation
of separate classes in object-oriented programming.
2Raja Danish
Listing 1 contains two class definitions written in VB.NET, with the second class definition
starting with the partial keyword. Both class definitions may reside in two different physical
files. Functionally, Listing 1 is equivalent to Listing 2.
Listing 1
'---File1.vb--Public Class Class1
Public Sub method1()
End Sub
End Class
File2.vb
Partial Public Class Class1
Public Sub method2()
End Sub
End Class
Listing 2
3Raja Danish
In reality, you can mix and match properties and method definitions in any of the files, but for
clarity it is a good idea to group properties definitions in one file and methods definitions in
another. The usual rules of OO apply: If there is a method1 in both files, then both method1s
must have unique signatures.
The syntax of partial classes in VB.NET and C# differs slightly. The following shows the
implementation of partial classes in C#:
// In C#, the partial keyword must
// appear in all class definitions
public partial class MyClass1
{
public MyClass1()
{
4Raja Danish
//implementation here
}
public partial class MyClass1
{
//implementation here
}
Besides the order in which the "partial" keyword is placed, the most significant difference is the
strict enforcement of the use of the "partial" keyword in all partial classes in C#. It is mandatory,
whereas in VB.NET, not all of the partial classes have to have the "partial" keyword. This has
caused a significant amount of newsgroup discussion about the rationale for the difference. My
advice is that you should always prefix partial classes with the "partial" keyword. At least this
will give you a visual clue that part of the implementation of the class lies somewhere else, and it
is definitely useful when it comes to debugging.
While partial classes allow you to split the definition of a class into multiple files, you cannot
mix languages. That is, all partial classes must be written in the same language. Besides using the
"partial" keyword for classes, you can also use it for structures and interfaces.
If your class implements multiple interfaces, it is a good idea to use partial classes to contain the
implementation for each interface.
3) Abstraction In OOP?
class program
{
abstract class animal
{
public abstract void eat();
public void sound()
{
Console.WriteLine("dog can sound");
}
}
5Raja Danish
Take an abstract class or an interface for example, it gives the user some method
descriptions, but not the internal implementaion.
Abstraction just gives the functional description but not the implementaion part.
Here you can see we have 2 methods in the Abstract Base Class, the method eat() has no
implementation that is why it is being declared as 'abstract' while the method sound() has its own
body so it is not declared as 'abstract'.
In the derived class we have the same name method but this method has got its body.
6Raja Danish
Abstraction VS Encapsulation:
1) Abstraction:
Take an abstract class or an interface for example, it gives the user some method
descriptions, but not the internal implementation.
Abstraction just gives the functional description but not the implementation part.
2) Encapsulation:
Encapsulation is also called information hiding and is used to hide those parts
of your class you do not wish others to see (and hence change). In C++/C# you
hide these parts of your class by using the 'private' keyword. Anything declared
as private will be hidden from developers using your class.
Its mean when you will make an object of this class from out-side this class, then
u cannot access its private methods.
For example in C# we can use properties inside the class to manipulate or access the
private data declared in the same class, but cannot do so from outside functions.
public string x
{
set
7Raja Danish
{this.fName = value;}
get
{return fName;}
}
---------------------
Generalization VS Sepecialization:
The term "insect" describes a general type of creature with various
characteristics. Because grasshoppers and bumblebees are insects, they have all
the general characteristics of an insect. In addition, they have special
characteristics of their own. For example, the grasshopper has its jumping ability,
and the bumblebee has its stinger. Grasshoppers and bumblebees are specialized
versions of an insect. This is illustrated in the Figure (given below).
8Raja Danish
Association
9Raja Danish
Association is a relationship between two objects. In other words, association defines the
multiplicity between objects. You may be aware of one-to-one, one-to-many, many-to-one,
many-to-many all these words define an association between objects. Aggregation is a special
form of association. Composition is a special form of aggregation.
Aggregation
Aggregation is a special case of association. A directional association
between objects. When an object has-a another object, then you have got
an aggregation between them. Direction between them specified which
object contains the other object. Aggregation is also called a Has-a
relationship.
Composition
Composition is a special case of aggregation. In a more specific manner, a
restricted aggregation is called composition. When an object contains the
other object, if the contained object cannot exist without the existence of
container object, then it is called composition.
Example: A Library contains students and books. Relationship between library and
student is aggregation. Relationship between library and book is composition. A
student can exist without a library and therefore it is aggregation. A book cannot
exist without a library and therefore its a composition.
Abstraction
See above. No image is given for it.
10Raja Danish
Generalization
Generalization uses a is-a relationship from a specialization to the
generalization class. Common structure and behaviour are used from the
specializtion to the generalized class. At a very broader level you can
understand this as inheritance. Why I take the term inheritance is, you can
relate this term very well. Generalization is also called a Is-a relationship.
Realization
You can understand this as the relationship between the interface and the
implementing class.
Dependency
Change in structure or behaviour of a class affects the other related class,
then there is a dependency between those two classes. It need not be the
same vice-versa. When one class contains the other class it this happens.
11Raja Danish
Structure VS Classes?
Similarities:
1. Both have members, which can include constructors, methods, properties,
fields, constants, enumerations, events, and event handlers.
2. Both members can be declared Public and another Private.
3. Both can implement interfaces.
4. Both can declare and raise events, and both can declare delegates.
Diffrences:
1. Structures are value types; classes are reference types.
2. Structures use stack allocation; classes use heap allocation.
3. All structure elements are Public by default; class variables and constants
are Private by default, while other class members are Public by default.
4. Structure elements cannot be declared as Protected; class members can.
5. Structures are not inheritable; classes are.
6. A structure does not require a constructor; a class does.
12Raja Danish
What is enum?
An enum is a value type with a set of related named constants often referred to as an
enumerator list. The enum keyword is used to declare an enumeration. It is a primitive data
type, which is user defined.
Enums type can be integer (float, int, byte, double etc.). But if you used beside int it has to
be cast.
The default underlying type of the enumeration elements is int. By default, the first
enumerator has the value 0, and the value of each successive enumerator is increased by 1.
// keyword_enum.cs
// enum initialization:
using System;
public class EnumTest
{
enum Days {Sat=1, Sun, Mon, Tue, Wed, Thu, Fri};
Output
Sun = 2
Fri = 7