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

DSA Practical 4

The document explains the concepts of classes and objects in programming, using a 'Dog' class as an example. It details the class structure, including attributes and methods, and demonstrates how to create an object of the class and use its methods. The provided Python code illustrates the functionality of the class through a specific instance named 'Buddy'.
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)
7 views3 pages

DSA Practical 4

The document explains the concepts of classes and objects in programming, using a 'Dog' class as an example. It details the class structure, including attributes and methods, and demonstrates how to create an object of the class and use its methods. The provided Python code illustrates the functionality of the class through a specific instance named 'Buddy'.
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

1. Classes: A class is a blueprint for creating objects.

It defines a set of attributes and methods that the


created objects will have.

2. Objects: An object is an instance of a class. It is a specific realization of the class with its own data and
behavior.

Here's a basic example to demonstrate these concepts:

### Python Code Example

# Define a class named `Dog`

class Dog:

# Constructor method to initialize attributes

def __init__(self, name, age):

self.name = name # Attribute to store the dog's name

self.age = age # Attribute to store the dog's age

# Method to make the dog bark

def bark(self):

return f"{self.name} says woof!"

# Method to get the dog's age in dog years

def age_in_dog_years(self):

return self.age * 7

# Method to display dog's information

def display_info(self):

return f"Name: {self.name}, Age: {self.age} (which is {self.age_in_dog_years()} in dog years)"


# Create an object (instance) of the Dog class

my_dog = Dog(name="Buddy", age=5)

# Access the object's methods and attributes

print(my_dog.bark()) # Output: Buddy says woof!

print(my_dog.age_in_dog_years()) # Output: 35

print(my_dog.display_info()) # Output: Name: Buddy, Age: 5 (which is 35 in dog years)

### Explanation

1. **Class Definition**:

- `class Dog`: Defines a new class named `Dog`.

- `__init__(self, name, age)`: The constructor method, called when a new object is created. It initializes
the `name` and `age` attributes of the object.

- `self.name` and `self.age`: These are attributes of the class. `self` refers to the current instance of the
class.

- `bark(self)`, `age_in_dog_years(self)`, and `display_info(self)`: Methods that define behaviors of the


`Dog` class. They operate on the attributes of the class.

2. Creating an Object:

- `my_dog = Dog(name="Buddy", age=5)`: Creates an instance of the `Dog` class with the name
"Buddy" and age 5.

3. Using the Object:

- `my_dog.bark()`: Calls the `bark` method, which returns a string indicating that the dog is barking.

- `my_dog.age_in_dog_years()`: Calls the `age_in_dog_years` method to convert the dog's age to dog
years.

- `my_dog.display_info()`: Calls the `display_info` method to get a formatted string with the dog's
information.
### Output

When you run the code, you'll get the following output:

Buddy says woof!

35

Name: Buddy, Age: 5 (which is 35 in dog years)

You might also like