10 Marks
10 Marks
Serialization: System.Runtime.Serialization
Objects commonly have state. An instance of a class, for example, can have one
or more fields, each of which contains some value. It's often useful to extract this
state from an object, either to store the state somewhere or to send it across a
network. Perform ing this extraction is called serializing an object,
Operator Overloading:
The syntax required to implement an overloaded operator is much the same as a static
method with a couple exceptions. You must use the operator keyword and specify the
operator
symbol being overloaded. Here's a skeleton example of how the dot product operator could
be
implemented:
public static Matrix operator *(Matrix mat1, Matrix mat2)
{
// dot product implementation
}
Parent Constructor.
Child Constructor.
I'm a Parent Class.
The top class is named ParentClass and the main class is called ChildClass. What we
want to do is create a child class, using existing code from ParentClass. First we must
declare our
intention to use ParentClass as the base class of ChildClass. This is accomplished through
the
ChildClass declaration public class ChildClass : ParentClass. The base class is specified by
adding a colon, ":", after the derived class identifier and then specifying the base class
name.
POLYMORPHISM:
DrawingObject.cs
using System;
public class DrawingObject
{
public virtual void Draw()
{
Console.WriteLine("I'm just a generic drawing object.");
}
}
This will be the base class for other objects to inherit from. It has a single method named
Draw(). The Draw() method has a virtual modifier. The virtual modifier indicates to derived
classes that they can override this method.
DrawDemo.cs
using System;
public class DrawDemo
{
public static int Main( )
{
DrawingObject[] dObj = new DrawingObject[4];
dObj[0] = new Line();
dObj[1] = new Circle();
dObj[2] = new Square();
dObj[3] = new DrawingObject();
foreach (DrawingObject drawObj in dObj)
{
drawObj.Draw();
}
return 0;
}
}
Output:
I'm a Line.
I'm a Circle.
I'm a Square.
I'm just a generic drawing object.