0% found this document useful (0 votes)
10 views5 pages

10 Marks

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views5 pages

10 Marks

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Serialisation

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
}

Implementing an Overloaded Operator: Matrix.cs


using System;
class Matrix
{
public const int DimSize = 3;
private double[,] m_matrix = new double[DimSize, DimSize];
// allow callers to initialize
public double this[int x, int y]
{
get { return m_matrix[x, y]; }
set { m_matrix[x, y] = value; }
}
// let user add matrices
public static Matrix operator +(Matrix mat1, Matrix mat2)
{
Matrix newMatrix = new Matrix();
for (int x=0; x < DimSize; x++)
for (int y=0; y < DimSize; y++)
newMatrix[x, y] = mat1[x, y] + mat2[x, y];
return newMatrix;
}
}
class MatrixTest
{
// used in the InitMatrix method.
public static Random m_rand = new Random();
// test Matrix
static void Main()
{
Matrix mat1 = new Matrix();
Matrix mat2 = new Matrix();
// init matrices with random values
InitMatrix(mat1);
InitMatrix(mat2);
// print out matrices
Console.WriteLine("Matrix 1: ");
PrintMatrix(mat1);
Console.WriteLine("Matrix 2: ");
PrintMatrix(mat2);
// perform operation and print out results
Matrix mat3 = mat1 + mat2;
Console.WriteLine();
Console.WriteLine("Matrix 1 + Matrix 2 = ");
PrintMatrix(mat3);
Console.ReadLine();
}
// initialize matrix with random values
public static void InitMatrix(Matrix mat)
{
for (int x=0; x < Matrix.DimSize; x++)
for (int y=0; y < Matrix.DimSize; y++)
mat[x, y] = m_rand.NextDouble();
}
// print matrix to console
public static void PrintMatrix(Matrix mat)
{
Console.WriteLine();
for (int x=0; x < Matrix.DimSize; x++)
{
Console.Write("[ ");
for (int y=0; y < Matrix.DimSize; y++)
{
// format the output
Console.Write("{0,8:#.000000}", mat[x, y]);
if ((y+1 % 2) < 3)
Console.Write(", ");
}
Console.WriteLine(" ]");
}
Console.WriteLine();
}
}
// let user add matrices
public static Matrix operator +(Matrix mat1, Matrix mat2)
{
Matrix newMatrix = new Matrix();
for (int x=0; x < DimSize; x++)
for (int y=0; y < DimSize; y++)
newMatrix[x, y] = mat1[x, y] + mat2[x, y];
return newMatrix;
}
The operator is static, which is the only way it can and should be declared because an
operator belongs to the type and not a particular instance. There are just a few rules you
have
to follow when implementing operator overloads. What designates this as an operator is the
use of the keyword operator, followed by the + symbol. The parameter types are both of the
enclosing type, Matrix. The implementation of the operator overload creates a new instance
of the return type and performs a matrix add.

Inheritance and Polymorphism

Inheritance is one of the primary concepts of object-oriented programming. It allows you to


reuse existing code. Through effective employment of reuse, you can save time in your
programming.
Inheritance: BaseClass.cs
using System;
public class ParentClass
{
public ParentClass()
{
Console.WriteLine("Parent Constructor.");
}
public void print()
{
Console.WriteLine("I'm a Parent Class.");
}
}
public class ChildClass : ParentClass
{
public ChildClass()
{
Console.WriteLine("Child Constructor.");
}
public static void Main()
{
ChildClass child = new ChildClass();
child.print();
}
}
Output:

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:

Another primary concept of object-oriented programming is Polymorphism. It allows you


to invoke derived class methods through a base class reference during run-time. This is
handy
when you need to assign a group of objects to an array and then invoke each of their
methods.
They won't necessarily have to be the same object type. However, if they're related by
inheritance, you can add them to the array as the inherited type. Then if they all share the
same
method name, that method of each object can be invoked.

A Base Class With a Virtual Method:

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.

Derived Classes With Override Methods:

Line.cs, Circle.cs, and Square.cs


using System;
public class Line : DrawingObject
{
public override void Draw()
{
Console.WriteLine("I'm a Line.");
}
}
public class Circle : DrawingObject
{
public override void Draw()
{
Console.WriteLine("I'm a Circle.");
}
}
public class Square : DrawingObject
{
public override void Draw()
{
Console.WriteLine("I'm a Square.");
}
}
This shows three classes. These classes inherit the DrawingObject class. Each class has a
Draw() method and each Draw() method has an override modifier. The override modifier
allows
a method to override the virtual method of its base class at run-time.
Program Implementing Polymorphism:

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.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy