[go: up one dir, main page]

0% found this document useful (0 votes)
25 views3 pages

OOPROG - Classes and Object

The document explains the fundamental concepts of Object-Oriented Programming (OOP), focusing on classes and objects. A class serves as a blueprint defining attributes and methods, while an object is an instance of a class with specific attribute values. Key differences between classes and objects include memory allocation and reusability, illustrated through an analogy of a blueprint and a house.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views3 pages

OOPROG - Classes and Object

The document explains the fundamental concepts of Object-Oriented Programming (OOP), focusing on classes and objects. A class serves as a blueprint defining attributes and methods, while an object is an instance of a class with specific attribute values. Key differences between classes and objects include memory allocation and reusability, illustrated through an analogy of a blueprint and a house.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

OOPROG:

Object-Oriented
Programming
1 CLASSES AND OBJECT

In Object-Oriented Programming (OOP), classes and objects are the fundamental building blocks used to create software
applications. Understanding these concepts is crucial for working effectively with OOP languages like C#.

Classes
➢ A class is a blueprint or template that defines the structure and behavior of objects. It encapsulates data
(attributes or properties) and methods (functions or behaviors) that operate on the data.

Attributes (Fields/Properties): These are variables that hold data specific to the object. For example, in a Car class,
attributes might include Make, Model, and Year.
Methods: These are functions defined within a class that describe the behaviors of the objects. For instance, a Car class
might have methods like StartEngine() or Accelerate().

Objects
➢ An object is an instance of a class. It is a specific, concrete entity that is created using the class as a template.
Each object has its own set of attribute values and can use the methods defined in the class.

Instance: When you create an object from a class, you are creating an instance of that class.
State: The attributes (fields) of an object define its state. For example, if you create a Car object with Make = "Toyota",
Model = "Corolla", and Year = 2021, these values define the state of that particular object.
Behavior: The methods define what the object can do. For example, calling StartEngine() on a Car object will simulate
starting the car's engine.

Example: Car Class

Here’s a simple Car class with public fields and methods:


Step 1: Create the Class
//Creating a class First, you define the class that you want to use. In this
public class Car case, we are creating a Car class with some basic fields
{ and methods.
// Public Fields
public string make; 1.1. Define the Car Class
public string model; Location: Typically, you would define the class in a
public int year; separate file, but for simplicity, you can define it within
the same file as your form.
// Public Methods
public void StartEngine()
{ Explanation:
MessageBox.Show("Engine started."); Fields: make, model, and year are public fields that store
} the data related to the car.

public void StopEngine()


{ Methods: StartEngine() and StopEngine() are methods
MessageBox.Show("Engine stopped."); that display messages indicating whether the car's engine
} is started or stopped.
}

Now, you will use the Car class inside the WinForms form. Step 2: Create a WinForms Application
Now, create a WinForms application where you will use
public partial class Form1 : Form the Car class.
{
public Form1() 2.1. Create a New WinForms Project
{ Open Visual Studio.
InitializeComponent(); Create a New Project:
} Go to File > New > Project....
Select Windows Forms App (.NET Framework) and click
private void button1_Click(object sender, EventArgs e) Next.
{ Name your project (e.g., CarWinFormsApp) and click
// Create a new Car object Create.
Car myCar = new Car();
Step 3: Use the Class in the Form
// Set the fields directly 3.1. Implement the button1_Click Event Handle
myCar.make = "Toyota";
myCar.model = "Corolla";
myCar.year = 2021; Explanation:
Step 1: Create an instance of the Car class using Car
// Display car details in the TextBox using myCar = new Car();.
concatenation Step 2: Directly set the values of the fields (make, model,
textBox1.Text = "Make: " + myCar.make + ", Model: " year).
+ myCar.model + ", Year: " + myCar.year; Step 3: Use string concatenation to display the car's
details in the TextBox.
// Call the methods Step 4: Call the methods StartEngine() and StopEngine()
myCar.StartEngine(); to display messages.
myCar.StopEngine();
}
}

Key Differences Between Classes and Objects


• Definition vs. Instance: A class is a definition or blueprint, whereas an object is an actual instance of the class.
• Memory Allocation: A class itself doesn't occupy memory, but each object created from the class does.
• Reusability: You can create multiple objects from the same class, each with different attribute values.
Analogy: Blueprint vs. House
• Class as a Blueprint: Think of a class as a blueprint for a house. The blueprint defines the layout, dimensions,
and design.
• Object as a House: An object is the actual house built using that blueprint. You can build many houses from the
same blueprint, each on a different plot of land (with different attribute values like paint color or garden size).
Conclusion
• Classes provide the structure and behavior that objects will have.
• Objects are instances of classes that have specific values for their attributes and can perform actions defined by
their methods.

You might also like