Constructor
Constructor
C# Destructor
-A destructor works opposite to constructor.
-It destructs the objects of classes.
-It can be defined only once in a class.
-Like constructors, it is invoked automatically at the end of program
-No need of (.) operator for calling a destructor
Stud_Demo.java
using System;
class Student
{
int rno;
string sname;
double per;
public Student()//non parameterized constructor
{
/*eid=101;
name="Sam";
salary=9876.90;*/
}
public ~Student()
{
Console.WriteLine("Memory Destroy");
}
s1.display();
Student s2=new Student();
s2.display();
Student s3=new Student();
s3.display();
2)Book
bid
bname,author
price
3)Vehicle
int vid
string vanme,compname,color,ownern
double price
4)
using System;
public class Area_Demo
{
double r,A;
public Area_Demo (double r)
{
this.r=r;
}
public double cal_area()
{
A=3.14*r*r;
return(A);
}
public ~Area_Demo ()
{
Console.WriteLine("Memory Destroy");
}
}
class Area_Ex
{
public static void Main(string[] args)
{
double r1,A;
A=a1.cal_area();
Console.WriteLine("Area={0}",A);
}
using System;
public class Employee
{
public int id;
public String name;
public float salary;
public Employee(int id, String name,float salary)
{
this.id = id;
this.name = name;
this.salary = salary;
}
public void display()
{
Console.WriteLine(id + " " + name+" "+salary);
}
}
class TestEmployee{
public static void Main(string[] args)
{
e2.display();
}
}
7)
using System;
public class ParaMethodDemo
{
int flag=0,n1,n,x,sum=0,p,f1=1,i;
public ParaMethodDemo (int n)
{
this.n=n;
}
public void pattern()
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=i;j++)
{
Console.Write("{0} ",j);
}
Console.WriteLine();
}
}
public void prime()
{
for(i=2;i<=(n/2);i++)
{
if(n%i==0)
{
flag=1;
break;
}
}
if(flag==0)
Console.WriteLine("No is prime");
else
Console.WriteLine("No is not prime");
}
public string pal()
{
p=n;
while(p>0)
{
n1=p%10;
p=p/10;
sum=(sum*10)+n1;
}
if(sum==n)
return "No is pal";
else
return "No is not pal";
}
}
public static void Main(string[] args)
{
int n,x;
a1.prime();
Console.WriteLine("{0}",a1.pal());
Console.WriteLine("Enter value of x");
x=Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Power={0}",a1.power(x));
}
}
8)Any 4 favourite functions add in class use any three types of userdefine function
9)Any 4 favourite functions add in class use any three types of userdefine function
* Array of an Object
using System;
class Student
{
int rno;
string sname;
double per;
public Student ()
{
}
}
s1[i].display();
}
2)
using System;
public class Area_Demo
{
double r,A;
public Area_Demo (double r1)
{
r=r1;
}
public double cal_area()
{
A=3.14*r*r;
return(A);
}
}
class Area_Ex
{
public static void Main(string[] args)
{
double r;
int n,i;
Console.WriteLine("Enter no of records");
n=Convert.ToInt32(Console.ReadLine());
Task
Convert all above programs into Array of an object type using constructor
C# Properties
C# Properites doesn't have storage location. C# Properites are extension of fields
and accessed like fields.
The Properties have accessors that are used to set, get or compute their values.
Usage of C# Properties
C# Properties can be read-only or write-only.
We can have logic while setting values in the C# Properties.
We make fields of the class private, so that fields can't be accessed from outside
the class directly. Now we are forced to use C# properties for setting or getting
values.
C# Properties Example
using System;
public class Employee
{
private string name1;
}
}
Output:
}
}
}
class TestEmployee{
public static void Main(string[] args)
{
Employee e1 = new Employee();
e1.Name = "Sonoo";
Console.WriteLine("Employee Name: " + e1.Name);
}
}
Output:
Employee Name: Sonoo JavaTpoint
C# Properties Example 3: read-only property
using System;
public class Employee
{
private static int counter;
public Employee()
{
counter++;
}
public static int Counter
{
get
{
return counter;
}
}
}
class TestEmployee{
public static void Main(string[] args)
{
Employee e1 = new Employee();
Employee e2 = new Employee();
Employee e3 = new Employee();
//e1.Counter = 10;//Compile Time Error: Can't set value