0% found this document useful (0 votes)
4 views

Addition 2

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Addition 2

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

1.

Classes:
In C#, a class is a blueprint or template for creating objects that share similar
characteristics and behavior. A class contains fields (also known as data members
or attributes) and methods that define the object's data and behavior,
respectively.
Fields are variables that store data within an object, and methods are functions
that can manipulate the object's data or perform some other action. Together, the
fields and methods define the object's properties and behavior.
Here is an example of a simple class definition in C#:
public class Person
{
// Fields (variables and data containers)
public string name;
public int age;

// Methods (yaane functions)


public void SayHello()
{
Console.WriteLine("Hello, my name is " + name + " and I am " + age + " years old.");
}
}
In this example, the Person class has two fields (name and age) and one method
(SayHello). The name field stores a string value representing the person's name,
and the age field stores an integer value representing the person's age. The
SayHello method simply prints a message to the console introducing the person by
name and age.
Once a class is defined, you can create objects (also known as instances) of that
class using the new keyword. For example, to create a Person object and set its
name and age fields, you could write the following code:
Person person = new Person();
person.name = "John";
person.age = 30;
person.SayHello();
This would create a new Person object with the name "John" and the age 30, and then
call the SayHello method to print a message introducing the person.
Overall, classes are a powerful tool in C# that allow you to define complex data
structures and encapsulate behavior in reusable code.

2.Access Modifiers public and private are used to control the visibility
of classes and their members (such as fields and methods) outside of the class
itself.
A public class is one that can be accessed from any other code within the same
project or assembly. This means that any other code can create an instance of the
class and access its public members (fields and methods). In other words, a public
class has no restrictions on its accessibility.
Here is an example of a public class:
public class Person
{
public string name;
public int age;

public void SayHello()


{
Console.WriteLine("Hello, my name is " + name + " and I am " + age + " years old.");
}
}
In this example, the Person class is declared as public, so it can be accessed from
any other code within the same project or assembly. The name and age fields and the
SayHello method are also declared as public, which means they can be accessed from
outside the Person class as well.
On the other hand, a private class is one that can only be accessed from within the
same class. This means that any other code outside of the class cannot create an
instance of the class or access any of its members. In other words, a private class
has limited accessibility.
Here is an example of a private class:
public class BankAccount
{
private decimal balance;

public BankAccount(decimal initialBalance)


{
balance = initialBalance;
}

private void DeductFees()


{
balance -= 10;
}

public void Withdraw(decimal amount)


{
balance -= amount;
DeductFees();
}

public void PrintBalance()


{
Console.WriteLine("Current balance: " + balance);
}
}
In this example, the BankAccount class is declared as public, but the balance field
and the DeductFees method are declared as private. This means that only other
members of the BankAccount class (such as the Withdraw method and the constructor)
can access the balance field and the DeductFees method.
Overall, the use of public and private classes and members allows for better
encapsulation of code and more control over its accessibility. It is good
programming practice to limit the visibility of classes and members to only what is
necessary for their proper functioning.

3.Constructor is a special method that is called when an object of a class


is created using the new keyword. The purpose of a constructor is to initialize the
object's fields or properties to their initial values.
The syntax for defining a constructor in C# is similar to that of a method, but
with the same name as the class and no return type. Here is an example of a simple
constructor:
public class Person
{
public string name;
public int age;

public Person(string personName, int personAge)


{
name = personName;
age = personAge;
}

public void SayHello()


{
Console.WriteLine("Hello, my name is " + name + " and I am " + age + " years old.");
}
}
In this example, the Person class has a constructor that takes two parameters
(personName and personAge) and assigns them to the name and age fields,
respectively. This constructor can be used to create a new Person object with the
specified name and age, like this:
Person person = new Person("John", 30);
The constructor is called automatically when the new keyword is used to create the
object. The constructor initializes the object's fields to the values passed in as
parameters, so that when the SayHello method is called, it can display the correct
name and age.
Constructors can also be overloaded, which means that multiple constructors can be
defined for a class, each with a different set of parameters. This allows objects
to be created with different initial values depending on the situation. Here is an
example of an overloaded constructor:
public class Person
{
public string name;
public int age;

public Person()
{
name = "Unknown";
age = 0;
}

public Person(string personName)


{
name = personName;
age = 0;
}

public Person(string personName, int personAge)


{
name = personName;
age = personAge;
}

public void SayHello()


{
Console.WriteLine("Hello, my name is " + name + " and I am " + age + " years old.");
}
}
In this example, the Person class has three constructors: one with no parameters,
one with a personName parameter, and one with both personName and personAge
parameters. Each constructor initializes the object's fields to a different set of
values, depending on the parameters passed in.
Overall, constructors are an essential part of creating objects in C#, as they
ensure that the object is properly initialized with the correct values. By
overloading constructors, you can provide different ways of initializing objects to
suit different needs.

Exercises:
1. Write a program that creates a Circle class with properties for radius and diameter. The
program should create an instance of the Circle class and calculate and print out the
circumference and area of the circle.
2. Write a program that creates a Book class with properties for title and author. The
program should create an array of Book objects and print out the title and author of
each book, sorted by author's last name in ascending order.
3. Write a program that creates a Movie class with properties for title, director, and
release year. The program should create an array of Movie objects and print out the
title, director, and release year of each movie, sorted by release year in descending
order.
4. Write a program that creates a Triangle class with properties for base and height. The
program should create an instance of the Triangle class and calculate and print out the
area of the triangle.
5. Write a program that creates a Time class with properties for hours, minutes, and
seconds. The program should create an instance of the Time class and print out the time
in 24-hour format, as well as the time in 12-hour format.

You might also like