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

Classes and Objects

Uploaded by

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

Classes and Objects

Uploaded by

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

Ferdauzar S. Rayyan A. Mohammad M.

Jarah Mentang Jeukarnin


Overview

This lesson introduces Object-Oriented Programming (OOP)


focusing on Classes and Objects in C#. It explains the core
concepts of how classes serve as blueprints for creating
objects, which represent real-world or abstract entities in
software. By learning about class structure, fields,
methods, properties, and constructors, you will understand
how to build scalable and organized applications.
Class
Class
Classes serve as essential blueprints for creating objects, which are
instances of those classes.

Define a class

In C#, a class can be defined using


the class keyword.
Example of class with object
Example of class with object
Class
In the C# programming language, classes hold a central position and
provide a powerful toolset for modeling real-world entities or abstract
concepts within your software.

Real-world Entity (Dog) Abstract Concept (Calculator)


Class

By utilizing classes, you can define attributes (member variables)


and behaviors (member functions/methods) for objects, fostering
the creation of reusable and well-structured code.
Class

 A class can contain one or more fields, methods, and


properties. They are called class members.

 A class and its members can have access modifiers


such as public, private, protected, and internal, to
restrict access from other parts of the program.
 public: Accessible anywhere in the program.
 private: Only accessible within the class.
 protected: Accessible within the class and any derived classes.
 internal: Accessible within the same project or
assembly.
Class Members
(Fields and Members)

 A field is a variable of any type that us declared directly


in a class or structure.

 A class can have one or more fields. It is a class-level


variable that holds a value. Generally, field members
should have a private access modifier used with property.
Add a field to the Student
class
Example of simple
class with fields
Fields represent the state or
characteristics of an object. They
are defined within the class and
store the object’s data.
Class Members
(Fields and Members)

Methods
 Methods define the behavior of the objects.
 They allow you to perform actions or
calculations related to the object’s state.
Let’s add some methods to the Student class
Property
A property encapsulates a private field using setter and
getter and retrieve underlying field value
You can also apply some
additional logic to get and set.
As seen in the example.

Note:
Get – used to return the
property value
Set – used to assign new
value
Value Keyword – defines
the value being assigned in
the set (Parameter)
Objects
 Are instances of a class created with
specifically defined data. Objects can
correspond to real-world objects or an abstract
entity.

 They may represent a person, place, or any item


that the program must handle.
Example

An Object A Car

Data Members - Model


- Year of Mfg
- Colour

Functions - Start
- Move
- Stop
Objects of a Class

 You can create one or more objects of a  An object of a class can be crated using the
class. Each object can have different values new keyword and assign that object in a
of properties and fields but methods and variable of a data type.
events behave the same.
You can now access public members of a class using the
object.MemberName notation
You can create multiple objects of a class with different values
of properties and fields.
Thank You for
Listening
Activity: Creating a Class and Object in C#
Instructions:

1. Use the provided Car class code as a guide.


2. Create a new class based on a real-world entity (e.g., Animal, Laptop, Book).
- Include at least one attribute and one method.
3. Instantiate the object:
- Assign a value to the attribute.
- Call the method to perform an action.
4. Ensure your code has a Main method for execution.

You might also like