Professional Documents
Culture Documents
Defining a Class
A class definition starts with the keyword class followed by the class
name; and the class body enclosed by a pair of curly braces.
Access specifiers specify the access rules for the members as well as
the class itself. If not mentioned, then the default access specifier for a
class type is internal. Default access for the members is private.
Data type specifies the type of variable, and return type specifies the
data type of the data the method returns, if any.
To access the class members, you use the dot (.) operator.
The dot operator links the name of an object with the name of a
member.
Constructors
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication2
{
classArea
{
public Area(int a)
{
Console.WriteLine("Area of Square="+a*a);
}
public Area(float r)
{
Console.WriteLine("Area of circle=" +3.14*r*r);
}
public Area(double s)
{
Console.WriteLine("Volume of a cube= " +s*s*s);
}
public Area(double l,double b)
{
Console.WriteLine("Area of Rectangle= " +l*b);
}
}
classProgram
{
publicstaticvoid Main(string[] args)
{
Area z1=newArea(6);
Area z2=newArea(10.0f);
Area z3=newArea(6.0);
Area z4=newArea(20.0,15.0);
Console.ReadKey();
}
}
}
OUTPUT
Area of a square=36
Area of a circle=314
Volume of a cube=216
Area of a Rectangle=300
Destructors
CONSTANT
READONLY MEMBERS
PROPERTIES
EXAMPLE
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication3
{
classnumber
{
privateint num;
publicvoid setNumber(int x)
{
num = x;
}
classProgram
{
staticvoid Main(string[] args)
{
setNumber(100);
Console.WriteLine("num="n.getnumber);
{
n.number();
}
}
}
}
}
INDEXERS
EXAMPLE
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
publicclassperson
{
String[]={"aa","bb","cc","dd"};
publicobjectthis[int index]
{
get
{
returnperson[index];
}
set
{
person(index)=value;
}
}
}
classProgram
{
staticvoid Main(string[] args)
{
person p=newperson();
Console.writeline(p[2]);
}
}
}
Inheritance
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
classa
{
protectedstring name;
publicvoid Name()
{
Console.WriteLine("Enter your Name:");
name = Console.ReadLine();
}
}
classb : a
{
protectedstring regno;
publicvoid RegNo()
{
Console.WriteLine("Enter Your RegNo:");
regno = Console.ReadLine();
}
}
classc : b
{
protectedstring major;
publicvoid Major()
{
Console.WriteLine("Enter Your Major:");
major = Console.ReadLine();
}
}
classd : c
{
protectedstring college;
publicvoid College()
{
Console.WriteLine("Enter Your College:");
college = Console.ReadLine();
}
publicvoid show()
{
Console.WriteLine("\n\n Data Entered \n\n");
Console.WriteLine(name);
Console.WriteLine(regno);
Console.WriteLine(major);
Console.WriteLine(college);
}
}
classProgram
{
publicstaticvoid Main(string[] args)
{
d z = newd();
z.Name();
z.RegNo();
z.Major();
z.College();
z.show();
Console.ReadKey();
}
}
}
Polymorphism
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
classShape
{
protectedint width, height;
public Shape(int a, int b)
{
width = a;
height = b;
}
publicvirtualint area()
{
Console.WriteLine("Parent Class");
return 0;
}
}
classRectangle : Shape
{
public Rectangle(int a, int b)
: base(a, b)
{
}
publicoverrideint area()
{
Console.WriteLine("Rectangle class area:");
return (width * height);
}
}
classTriangle:Shape
{
public Triangle(int a,int b):base(a,b)
{
}
publicoverrideint area()
{
Console.WriteLine("Triangle class area:");
return (width*height/2);
}
}
classCaller
{
publicvoid CallArea(Shape s)
{
int a;
a=s.area();
Console.WriteLine("Area {0}",a);
}
}
classDyPoly
{
staticvoid Main(string[] args)
{
Caller c = newCaller();
Rectangle r = newRectangle(10, 7);
Triangle t = newTriangle(10, 5);
c.CallArea(r);
c.CallArea(t);
Console.ReadKey();
}
}
}
ABSTRACT CLASSES
Sealed classes are used to restrict the users from inheriting the class. A class can
be sealed by using the sealed keyword. The keyword tells the compiler that the
class is sealed, and therefore, cannot be extended. No class can be derived from
a sealed class.
EXAMPLE
// C# code to define
// a Sealed Class
using System;
// Sealed class
sealedclassSealedClass
{
// Calling Function
publicint Add(int a, int b)
{
return a + b;
}
}
classProgram
{
// Main Method
staticvoid Main(string[] args)
{
Method Overloading
EXAMPLE
using System;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
namespace ConsoleApplication20
{
class A1
{
public virtual void abc()
{
Console.WriteLine("IN A");
}
}
class B1 : A1
{
public override void abc()
{
Console.WriteLine("IN B");
}
}
class C1 : B1
{
public override void abc()
{
Console.WriteLine("IN C");
}
}
class D1 : C1
{
public override void abc()
{
Console.WriteLine("IN D");
}
}
class Ride
{
public static void Main()
{
A1 z1 = new A1();
B1 z2 = new B1();
C1 z3 = new C1();
D1 z4 = new D1();
z1.abc();
z2.abc();
z3.abc();
z4.abc();
Console.ReadLine();
}
}
}
Method overriding
• Creating the method in a derived class with same name, same parameters
and same return type as in base class is called as method overriding.
• Method overriding is only possible in derived class not within the same
class where the method is declared.
• Only those methods are overrides in the derived class which is declared in
the base class with the help of virtual keyword or abstract keyword.
EXAMPLE
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication4
{
classA1
{
publicvirtualvoid abc()
{
Console.WriteLine("In A");
}
}
classB1 : A1
{
publicoverridevoid abc()
{
Console.WriteLine("In B");
}
}
classC1 : B1
{
publicoverridevoid abc()
{
Console.WriteLine("In C");
}
}
classRide
{
staticvoid Main(string[] args)
{
A1 z1 = newA1();
B1 z2 = newB1();
C1 z3 = newC1();
z1.abc();
z2.abc();
z3.abc();
Console.ReadKey();
}
}
}