M08_COP3502_Kapoor_Studyguide

docx

School

University of Florida *

*We aren’t endorsed by this school

Course

3502

Subject

Computer Science

Date

Feb 20, 2024

Type

docx

Pages

7

Uploaded by DukeDiscovery9372

Report
PROGRAMMING FUNDAMENTALS 1 Module 8: Class & Inheritance Overview Classes : Used to create and design objects Constructor: Used to define and create an object __init__ is called, self is always the first parameter, then other arguments ex. class Dog: def __init__(self, name): #constructor self.name = name corgi = Dog("Chewy") #creates new Dog object with name "Chewy" Default Values Reminder! Constructors can have default values: if no value is given for a parameter, use the default one ex. class Pikachu: def __init__(self, level=0): self.level = level pika = Pikachu() #creates Pikachu object with default level 0 print(pika.level) #prints 0 Class Attributes/Instance Attributes Class Attributes are part of the class and shared amongst all instances of class Instance Attributes are part of each individual object and are unique ex. class alien: home = "outer space" #Class Attribute def __init__(self, eyes): self.eyes = eyes #Instance Attribute - unique to each instance alien1 = alien(1) #define alien1 and alien2 instances, alien 1 has 1 eye and alien 2 has 2 eyes 1
PROGRAMMING FUNDAMENTALS 1 Module 8: Class & Inheritance alien2 = alien(2) # (Object).(attribute) - goes to unique object and goes to attribute in object print(alien1.eyes) #print 1 print(alien.home) #print "outer space" - class attribute print(alien2.home) #print "outer space" *CANNOT be referenced just with "home", must be attached to class name or instance ex. class student: grade = 85 #Class Attribute update = 10 def __init__(self, age): #Constructor self.age = age #instance attribute def new_grade(self): #method inside class student.grade += update #ERROR: update cannot be called on its own, CHANGE to student.update Tom = student(20) Tom.new_grade() #call method print(Tom.grade) #once error is resolved, prints 95 Static Methods/Class Methods BOUND to the class and not the individual object Class Methods: Can access class state and take in cls as a first argument Static Methods: cannot change class state ex. class Clothes: types = 0 def __init__(self, num): self.num = num Clothes.types+=1 @classmethod def num_types(cls): 2
PROGRAMMING FUNDAMENTALS 1 Module 8: Class & Inheritance return cls.types @staticmethod def new_clothes(types): return types < 2 tshirt = Clothes(2) #Clothes.types goes up by 1 hat = Clothes(3) #Clothes.types goes up by 1 print(Clothes.new_clothes(1)) #prints True, compares 1 < 2 print(Clothes.num_types()) #prints 2, looks at types attribute for the class Derived Classes Utilizes inheritance to create a subclass with the parent class' methods ex. class Animal: def __init__(self, legs = 2): self.legs = legs class Cat(Animal): def __init__(self, fur): super().__init__() self.fur = fur Meow = Cat("brown") print(Meow.fur) #prints "brown" print(Meow.legs) #prints 2 from parent class Practice Questions 1. What is the output of the following program? class Circle: pi = 3.14 def __init__(self, radius): self.radius = radius 3
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
PROGRAMMING FUNDAMENTALS 1 Module 8: Class & Inheritance def area(self): return pi * self.radius ** 2 c = Circle(2) print(c.area()) A. 2 B. 6.28 C. 12.56 D. Error 2. What is the output of the following program? class MyClass: x = 9 def __init__(self, x=2, y=1): self.y = y self.x = x def sum_nums(self): self.y += self.x return self.y m = MyClass(4) print(m.sum_nums()) A. 3 B. 5 C. 10 D. Error 3. What is the output of the following program? 4
PROGRAMMING FUNDAMENTALS 1 Module 8: Class & Inheritance class Monster: count = 0 def __init__(self, health): self.health = health self.count += 1 @classmethod def get_count(cls): return cls.count m1 = Monster(11) m2 = Monster(3) print(m2.count, Monster.count) A. 1 0 B. 2 2 C. 1 2 D. Error 4. What is the output of the following program? class Monster: def __init__(self, health): self.health = health @staticmethod def increase_health(monster, health): monster.health += 20 health = 40 5
PROGRAMMING FUNDAMENTALS 1 Module 8: Class & Inheritance m = Monster(10) health = 30 Monster.increase_health(m, health) print(m.health, health) A. 10 40 B. 10 30 C. 30 40 D. 30 30 5. What is the output of the following program? If you think this program will generate an error, please list “error” as your output. class Parent: def __init__(self, x, y=3): self.x = x self.y = y class Child(Parent): def __init__(self, a, b): super().__init__(a) self.b = b c = Child(5, 7) print(c.b, c.x, c.y) Answer: 1. D 2. B 3. A 4. D 5. 7 5 3 6
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
PROGRAMMING FUNDAMENTALS 1 Module 8: Class & Inheritance 7