/Greeter

Worksheet 7 OOP Creating our first class and adding some functions to it

Primary LanguagePython

July 2022 Computing programming course by Teesside University

Worksheet 7-Object-Oriented Programming

[ASSESSMENT] Question 1: Greeter (greeter.py) Let’s get started with object-oriented programming by creating our first class and adding some functions to it.

  1. Create a class called Greeter which has a constructor that takes one argument name (in addition to the usual self).
  2. Now, create a function called greet_to_screen() inside this class that greets the user on the command line. Your class should look like this:
class Greeter:
    name = ""
    def __init__(self, name): # The constructor.
        self.name = name
    def greet_to_screen(self):
        print("Hello,", self.name)

greeter = Greeter("Fred")
greeter.greet_to_screen()
  1. Test this file, what happens? See how name is stored at the class level as an attribute of the class when the constructor is called? How many different variables called name are there here?
  2. Now, add another function to your class called greet_to_file() that takes an argument filename and writes the greeting to a file instead. For example, greeter.greet_to_file(“hi.txt”) would write Hello, Fred to the file hi.txt.