Date: Tuesday March 4th
Topics Covered:
Constructors
Destructors
Partial Classes
Pillars of OOP
Solid
Software Installation (UML)
What’s the point of a constructor?
Tells you to how set the parameters for an object
Constructor w/ Parameters v.s. Constructor w/o Parameters
A constructor w/ parameters —>
A constructor w/o parameters
namespace Week8Lec1ConstructorRecap
{
class Cat
{
private string name; //Encapsulated (aka)
private double weight;
}
public void setName(string name)
{
this.name = name;
}
public string getName()
{
}
public void setWeight(double weight)
{
this.weight = weight;
}
internal class Program
{
static void Main(string[] args)
{
Cat c = new Cat() //parameter-less constructor
c.setName("Joe");
c.setWeight(10);
Console.WriteLine($"{c.getName()} weights {c.getWeight()} 1lbs");
Console.Read();
}
}
}
namespace Week8
{
class Cat
{
private string name; //Encapsulated (aka)
private double weight;
}
public void setName(string name)
{
this.name = name;
}
public string getName()
{
}
public void setWeight(double weight)
{
this.weight = weight;
}
internal class Program
{
static void Main(string[] args)
{
//Constructor w/ 2 parameters
Cat c;
c = new Cat();
Cat c2;
c2 = new Cat("George", 12);
c.setName("Joe");
c.setWeight(10);
Console.WriteLine($"{c.getName()} weights {c.getWeight()} 1lbs");
Console.WriteLine($"{c2.getName()} weights {c2.getWeight()} 1lbs");
Console.Read();
}
}
}
namespace Week8Lec1Destructors
{
internal class Person
{
static void Main(string[] args)
{
Person pA = new Person("Fred", "Jones");
Person pB = new Person("Scooby", "Do");
pB = null;
//GC.Collect(); <--- Forces garbage collector to free up memory from the heap (NTS: un-comment this line to see its effect on code)
Console.ReadLine();
}
private string firstName;
private string lastName;
public Person(string firstName, string lastName)
{
//Constructor
this.firstName = firstName;
this.lastName = lastName;
}
// The following is a destructor
// Some coders choose to use more often than others (*prof said he rarely uses it)
// Automatically invoked whenever an object is freed from memory
// Using a destructor = Making choice to explicitly free up memory space
~Person() {
Console.WriteLine(firstName + " " + lastName);
Console.WriteLine("Taking out the trash");
}
}
}
Output —> should be nothing/empty
scooby doo
taking out the trash
namespace Week8Lec1Destructors
{
internal class Person
{
static void Main(string[] args)
{
Person pA = new Person("Fred", "Jones");
Person pB = new Person("Scooby", "Do");
pB = null;
GC.Collect(); <--- Forces garbage collector to free up memory from the heap (NTS: un-comment this line to see its effect on code)
Console.ReadLine();
}
private string firstName;
private string lastName;
public Person(string firstName, string lastName)
{
//Constructor
this.firstName = firstName;
this.lastName = lastName;
}
// The following is a destructor
// Some coders choose to use more often than others (*prof said he rarely uses it)
// Automatically invoked whenever an object is freed from memory
// Using a destructor = Making choice to explicitly free up memory space
~Person() {
Console.WriteLine(firstName + " " + lastName);
Console.WriteLine("Taking out the trash");
}
}
}
Output is:
Scooby Doo
Taking out the trash
**This will be asked on final exam
Inheritance (DRY)
Abstract Classes (enforce certain behaviors)
Interfaces (enforce certain behaviors)
Polymorphism (powerful)
*Remember: Abstraction ≠ Abstract Classes
**Must know definition of abstraction for final exam
DRY = Don’t Repeat Yourself
Code reuse —> beneficial for cutting down amount of code writing you actually do (DC if my explanation is correct)
class Student {
int ID;
string name;
string DOB;
string citizenship;
string gender;
}
class Employee {
int ID;
string name;
string DOB;
string citizenship;
string gender;
}
class Athelete{
int ID;
string name;
string DOB;
string citizenship;
string gender;
}
// Notice that all 3 classes contain the same variables, therefore you are repeating the same code. This does not make for good code. To fix this, create a parent class that each of these classes inherit from (they are the child classes).
class Person {
int ID;
string name;
string DOB;
string citizenship;
string gender;
}
class Student: Person{
double GPA;
//Class Student has all the same variables & methods as its parent class Person does, but also has the variable 'GPA'
}
class Employee: Person{
double salary;
}
class Athlete: Person{
string sport;
}
// Now our code has been improved so that class Person is the parent class & classes student, emplyoee, and athlete are its subclasses (therefore inherit all of its variables & methods). However, it's important to note
All classes inherit from the class Object
Which programming languages allow a class to have multiple parent classes?
C++
Which programming languages don’t allow a class to have multiple parent classes?
C#
Java
Class A ← Class B ← Class C
Above is an example of multi-level inheritance where Class B inherits from Class A, class C inherits from Class B & Class A
Muilti-level inheritance isn’t permitted in C#
Sample Code of Interfaces being used:
interface A{
int doCalculation();
int doCalculation2(int x, int y);
}
class XYZ: interfaceA
int doCalculation()
{
return 0;
}
int do Calculation(int x, int y)
{
return x + y;
}
namespace Week8_Lec1_OOP_Basics
{
abstract class AbstractClass1
{
puublic void print() //Concrete method
{
Console.WriteLine("Hello there");
}
public abstract void personsays(); //Abstract method
}
internal interface Interface1
{
// No implementation, whoever uses interface 1 will provide the implementation
void DoStuff(); //declares a method belonging to class AbstracClass1
void DoStuff2(); //declares another method
}
internal interface Interface2
{
void DoStuff3() //declares a method belong to class AbstractClass1
}
internal class Person:AbstractClass1, Interface1, Interface2
{
//All of the abstract methods belong to AbstractClass1 is implemented below
public override void personsays()
{
Console.WriteLine("I am a human");
}
public void DoStuff()
{
Console.WriteLine("I am reading");
}
public void DoStuff2()
{
Console.WriteLine("I am eating");
}
}
class Program
{
Person person = new Person()
person.DoStuff =
person.DoStuff2 =
person.DoStuff3 =
}
}
'sealed' is a keyword used to declare that a class cannot have children (subclasses)
If you want a child class to be modify a parent method…
Use the keyword ‘virtual’ within the parent class
Use the keyword ‘override’ within the child class
namespace Week8_Lec1_OOP_Basics
{
class Animal
{
public string type = "animal";
public void Greet()
{
Console.WriteLine("Hello I am an animal");
}
internal class Dog: Animal
{
public string type = "doberman";
// Keyword 'override' used in child class to override parent class method
public override void Greet()
{
base.Greet();
//'base' means you are referring to the Greet() in the parent class
Console.WriteLine("Referring to the dog class " + type);
//Refers to the version of the method in child class
Console.WriteLine("Referring to the Animal class " + base.type);
}
}
internal class Cat: Animal
{
public string type = "calico";
}
internal class Program
{
static void Main(string[] args)
{
Animal a = new Animal();
Dog dog = new Dog();
Cat c = new Cat();
a.Greet();
dog.Greet();
c.Greet();
Console.Read();
}
}
}
}
/* OUTPUT
Hello I am an animal
Hello, I am doberman!
Hello I am an animal
Referring to the dog class: doberman
Referring to the Animal class: animal
hello, I am a tabby! */