C# Slides Part 3
C# Slides Part 3
Class Object
Fruit Apple, Banana, Mango
Car Volvo, Audi, Toyota
Reference refers to the new object but does not contain the
object data itself
Customer object2;
C# Classes & Objects
Creating object references that don't refer to an object will fail
at run time on access that object
// Program.cs
Car myObj1 = new Car();
Car myObj2 = new Car();
Console.WriteLine(myObj1.colour);
Console.WriteLine(myObj2.colour);
C# Classes & Objects
Using Multiple Classes
• You can also create an object of a class and access it in
another class
• This is often used for better organization of classes. One
class has all the fields and methods, while the other class
holds the Main()method (code to be executed)
prog2.cs
public class Car { public string color = "red"; }
prog.cs
Car myObj = new Car();
Console.WriteLine(myObj.color);
C# Class Members
Fields and methods inside classes are often referred to as
"Class Members"
//Program.cs
Car myObj = new Car();
myObj.color = "red";
myObj.maxSpeed = 200;
Console.WriteLine(myObj.color);
Console.WriteLine(myObj.maxSpeed);
C# Constructors
A constructor is a special method that is called when an object
of a class is created and is used to initialize objects
//Car.cs
class Car // Create a Car class
{ public string model; // Create a field
// Create a class constructor for the Car class
public Car()
{ model = "Mustang"; // Set the initial value for model } }
//Program.cs
Car Ford = new Car(); // this will call the constructor
Console.WriteLine(Ford.model); // Print the value of model
// Outputs "Mustang"
C# Constructors
Constructor name must match the class name, it cannot have
a return type (like void or int)
//Program.cs
Car Ford = new Car("Mustang");
Console.WriteLine(Ford.model); // Outputs "Mustang"
C# Constructors
class Car
{
public string model;
public string color;
public int year;
// Create a class constructor with multiple parameters
public Car(string modelName, string modelColor, int modelYear)
{
model = modelName;
color = modelColor;
year = modelYear;
}}
-------------------------
Car Ford = new Car("Mustang", "Red", 1969);
Console.WriteLine(Ford.color + " " + Ford.year + " " + Ford.model);
// Outputs Red 1969 Mustang
C# Properties
Before understanding properties, you should have a basic
understanding of "Encapsulation"
https://docs.microsoft.com/en-
us/dotnet/csharp/fundamentals/types/
C# Inheritance
C# Inheritance
In C#, it is possible to inherit fields and methods from one class
to another. We group the "inheritance concept" into two
categories
• Derived Class (child) - the class that inherits from another
class
• Base Class (parent) - the class being inherited from
When a class is created, you can inherit from any other class
that is not defined as sealed, and other classes can inherit from
your class and override class virtual methods
Access Modifiers
C# Abstract Classes
C# Abstract Classes
A class can be declared abstract
public class D : C
{
public sealed override void DoWork() { }
}
C# Abstract Classes
abstract class Animal
{
public abstract void animalSound();
public void sleep()
{
Console.WriteLine("Zzz");
}
}
//Not possible to create an object of the Animal class
Animal myObj = new Animal();
// Regular method
public void sleep()
{
Console.WriteLine("Zzz");
}
}
C# Abstract Classes
// Derived class (inherit from Animal)
class Pig : Animal
{
public override void animalSound()
{
// The body of animalSound() is provided here
Console.WriteLine("The pig says: wee wee");
}
}
------------------------------------------------------------------------
Pig myPig = new Pig(); // Create a Pig object
myPig.animalSound(); // Call the abstract method
myPig.sleep(); // Call the regular method
C# Interfaces
C# Interfaces
Another way to achieve abstraction in C#, is with interfaces.
// interface
interface IAnimal
{
void animalSound(); // interface method (does not have a body)
void run(); // interface method (does not have a body)
}
C# Interfaces
It is considered good practice to start with the letter "I" at the
beginning of an interface, as it makes it easier for yourself and
others to remember that it is an interface and not a class
OOP Practice
C# OOP PRACTICE
OOP Exercises
C# OOP PRACTICE
Ex 1
Create a C# program that requests three names of people from the user Input
and stores them in an array of objects of type Person. To do this, first Jana
Sara
create a Person class that has a Name property of type string and
Chaitanya
override the ToString() method.
End the program by reading people and executing the ToString() method Output
on the screen. Hello! My name is
Jana
Hello! My name is
Sara
Hello! My name is
Chaitanya
C# OOP PRACTICE
Ex 2
Create a new C # project with three classes plus another class to test the logic in Input
your code. The main classes of the program are the following classes:
• Person
• Student
Output
• Professor
Hello!
The Student and Teacher classes inherit from the Person class. The Student class Hello!
will include a public Study() method that will write I'm studying on the screen. The My age is 21 years
Person class must have two public methods Greet() and SetAge(int age) that will old
assign the age of the person. The Teacher class will include a I'm studying
public Explain() method that will write I'm explaining on the screen. Also create a Hello!
public method ShowAge() in the Student class that writes My age is: x years old on I'm explaining
the screen.
In Program.cs do the following:
• Create a new Person and make him say hello
• Create a new Student, set an age, say hello, displays his/ her age and starts
studying.
• Create a new Teacher, set an age, say hello and start the explanation.
C# OOP PRACTICE
Ex 3
Create a C# program to manage a photo book using object-oriented Input
programming.
To start, create a class called PhotoBook with a private
attribute numPages of type int. It must also have a public
method GetNumberPages that will return the number of photo book Output
pages. 16
The default constructor will create an album with 16 pages. There will be 24
64
an additional constructor, with which we can specify the number of pages
we want in the album.
There is also a BigPhotoBook class whose constructor will create an
album with 64 pages.
In Program.cs perform the following actions:
• Create a default photo book and show the number of pages
• Create a photo book with 24 pages and show the number of pages
• Create a large photo book and show the number of pages
C# OOP PRACTICE
Ex 4
Create a C# program that prompts the user for three names of people and Input
stores them in an array of Person-type objects. There will be two people Jana
of the Student type and one person of the Teacher type. Sara
To do this, create a Person class that has a Name property of type string, Chaitanya
a constructor that receives the name as a parameter and overrides the
ToString () method.
Then create two more classes that inherit from the Person class, they will Output
Explain
be called Student and Teacher. The Student class has a Study method
Study
that writes to the console that the student is studying. The Teacher class
will have an Explain method that writes to the console that the teacher is Study
explaining. Remember to also create two constructors on the child
classes that call the parent constructor of the Person class.
End the program by reading the people (the teacher and the students)
and execute the Explain and Study methods.
C# OOP PRACTICE
Ex 5
Create a C# program that implements an IVehicle interface with two Input
methods, one for Drive of type void and another for Refuel of type bool 50
that has a parameter of type integer with the amount of petrol to refuel.
Then create a Car class with a builder that receives a parameter with the
car's starting petrol amount and implements the Drive and Refuel Output
methods of the car. Driving
The Drive method will print on the screen that the car is Driving, if petrol is
greater than 0. The Refuel method will increase the petrol of the car and
return true.
To carry out the tests, create an object of type Car with 0 of petrol in
Program.cs and ask the user for an amount of petrol to refuel, finally
execute the Drive method of the car.
C# OOP PRACTICE
Ex 6
Create a C# program that implements an abstract class Animal that has a Input
Name property of type text and three methods SetName (string name), Tommy
GetName and Eat. The Eat method will be an abstract method of type
void.
You will also need to create a Dog class that implements the above Output
Animal class and the Eat method that says the dog is Eating. Tommy
To test the program ask the user for a dog name and create a new Dog Eating
type object in Program.cs, give the Dog object a name, and then execute
the GetName and Eat methods.
C# OOP PRACTICE
Ex 7
Create a class to store details of student like rollno, name, course joined Input
and fee paid so far. Assume courses are C# and RDBMS with course fees 1
being 20000 and 15000 respectively. Akshat Shastri
Provide a constructor to take rollno, name and course. C#
Provide the following methods:
Output
Payment(amount) to pay part/ full fees which will be saved. Hello, Akshat
Print() to print details of the student with fees paid. Shastri. Your
DueAmount property course fee is
TotalFee property Rs__.
Add a static member to store Service Tax, which is set to 12.3%. Also
allow a property through which we can set and get service tax.
Modify TotalFee and DueAmount properties to consider service tax.
C# OOP PRACTICE
Ex 8
Create the classes required to store data regarding different types of Input
Courses in an institute. All courses have course serial number, name,
duration in months, number of students and course fee per student.
Courses may be part-time or full-time where you must store the time per Output
day for that course. Part-time courses will run for 4 hours per day and full-
time courses run for 8 hours in a day.
Some courses are onsite where you must store the company name and
the number of candidates for the course.
For onsite course charge 10% more on the course fee. For part-time
course, offer 10% discount to the course fee.
Provide constructors and the following methods.
Print() to print relevant details of the course.
GetTotalFee() that prints the fee received from running a particular
course.