0% found this document useful (0 votes)
28 views8 pages

Constructor

The document discusses constructors and destructors in C#, explaining that constructors are called automatically when an object is created and destructors are called automatically at the end of a program to destruct objects, and it provides an example C# program that demonstrates creating Student objects with a constructor and destructor.

Uploaded by

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

Constructor

The document discusses constructors and destructors in C#, explaining that constructors are called automatically when an object is created and destructors are called automatically at the end of a program to destruct objects, and it provides an example C# program that demonstrates creating Student objects with a constructor and destructor.

Uploaded by

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

Constructor

-Class name & method name must be same


-it does not contain return datatype
-When object is created then it is automatically called or invoked
-No need of (.) operator for calling a constuctor
-it doenot return a value
-there are 2 types of constructor
1)Non-parameterized/default constructor
2)parameterized

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;*/

Console.WriteLine("Enter rollno ,name & per");


rno=Convert.ToInt32(Console.ReadLine());
sname=Console.ReadLine();
per=Convert.ToDouble(Console.ReadLine());
}
public void display()
{
Console.WriteLine("Rollno={0}",rno);
Console.WriteLine("Name={0}",sname);
Console.WriteLine("Per={0}",per);

}
public ~Student()
{
Console.WriteLine("Memory Destroy");
}

public class Stud_Demo {

public static void Main(string[] args) {


Student s1=new Student();

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;

Console.WriteLine("Enter value of r");


r1=Convert.ToDouble(Console.ReadLine());
Area_Demo a1=new Area_Demo(r1);

A=a1.cal_area();
Console.WriteLine("Area={0}",A);
}

5)max from 2 number


6)factorial

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)
{

int eid;string name;float salary;


eid=
name=
salary
Employee e1 = new Employee(eid, name, salary); e1.display();
Employee e2 = new Employee(102, "Mahesh", 490000f);

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 int power(int x)


{
f1=1;

this.x=x;//this means acces the member of itself


for(i=1;i<=n;i++)
{
f1=f1*x;
}
return (f1);

}
public static void Main(string[] args)
{
int n,x;

Console.WriteLine("Enter value of n");


n=Convert.ToInt32(Console.ReadLine());
ParaMethodDemo a1=new ParaMethodDemo(n);
a1.pattern();

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 ()
{

Console.WriteLine("Enter rollno ,name & per");


rno=Convert.ToInt32(Console.ReadLine());
sname=Console.ReadLine();
per=Convert.ToDouble(Console.ReadLine());
}
public void display()
{
Console.WriteLine("Rollno={0}",rno);
Console.WriteLine("Name={0}",sname);
Console.WriteLine("Per={0}",per);

}
}

public class Stud_Demo {

public static void Main(string[] args) {


int n,i;
Console.WriteLine("Enter no of students");
n=Convert.ToInt32(Console.ReadLine());
Student []s1=new Student[n];//Array creation
for(i=0;i<n;i++)
{
s1[i]=new Student();//object creation

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());

Area_Demo []a1=new Area_Demo[n];//array creation


for(i=0;i<n;i++)
{

Console.WriteLine("Enter value of r");


r=Convert.ToDouble(Console.ReadLine());
a1[i]=new Area_Demo(r);//object creation
Console.WriteLine("Area={0}",a1[i].cal_area());
}

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;

public string Proper


{
get
{
return name1;
}
set
{
name1 = value;
}
}
}
class TestEmployee{
public static void Main(string[] args)
{
Employee e1 = new Employee();
e1.Proper = "Sonoo Jaiswal";
Console.WriteLine("Employee Name: " + e1.Proper );

}
}
Output:

Employee Name: Sonoo Jaiswal

C# Properties Example 2: having logic while setting value


using System;
public class Employee
{
private string name;

public string Name


{
get
{
return name;
}
set
{
name = value+" JavaTpoint";

}
}
}
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

Console.WriteLine("No. of Employees: " + Employee.Counter);


}
}
Output:

You might also like