Difference between revisions of "OOP Class4"

esse quam videri
Jump to: navigation, search
(Video)
 
(9 intermediate revisions by 3 users not shown)
Line 1: Line 1:
[[Category:Object Oriented Programming]]
+
[[Category:IAM Classes]]
 
=Classes and Objects=
 
=Classes and Objects=
 
==Objects==
 
==Objects==
Line 13: Line 13:
  
 
Classes are sections of code that describe real world objects. Classes define what '''properties''' and '''methods''' an object contains.  
 
Classes are sections of code that describe real world objects. Classes define what '''properties''' and '''methods''' an object contains.  
Properties
 
:define data members of classes. Nouns
 
  
Methods  
+
'''Properties''': define data members of classes. Nouns
:define what classes can do. Verbs
+
 
 +
'''Methods''': define what classes can do. Verbs
  
 
===Abstraction===
 
===Abstraction===
Line 33: Line 32:
 
* flexibility
 
* flexibility
 
:specialized objects are often less flexible
 
:specialized objects are often less flexible
* performance
 
 
* portability
 
* portability
 +
:encapsulated objects make it easier to port to other languages
  
 
==Encapsulation==
 
==Encapsulation==
Line 44: Line 43:
 
a simple example of class syntax in c#
 
a simple example of class syntax in c#
  
<csharp>public class Dog{ }</csharp>
+
<syntaxhighlight lang="csharp">public class Dog{ }</syntaxhighlight>
  
 
class syntax full
 
class syntax full
Line 56: Line 55:
  
 
 
 
 
<csharp>// Instantiate Dog objects
+
<syntaxhighlight lang="csharp">// Instantiate Dog objects
 
// Declare an instance of the Dog class
 
// Declare an instance of the Dog class
 
Dog firstDog;
 
Dog firstDog;
Line 64: Line 63:
  
 
// Declare and Allocate in one like
 
// Declare and Allocate in one like
Dog secondDog = new Dog();</csharp>
+
Dog secondDog = new Dog();</syntaxhighlight>
  
 
The '''new''' keyword call the classes '''constructor''' which allocated memory on the stack for the object.  
 
The '''new''' keyword call the classes '''constructor''' which allocated memory on the stack for the object.  
Line 121: Line 120:
 
A classes constructor is the method that is run when an instance of the class is created. If you do not provide a constructor the complier provides one for you. The constructor is used to initiate all member properties. Unlike intrinsic types variables that are not initialized will be set to 0 or "" or null by the compiler in the default constructor.
 
A classes constructor is the method that is run when an instance of the class is created. If you do not provide a constructor the complier provides one for you. The constructor is used to initiate all member properties. Unlike intrinsic types variables that are not initialized will be set to 0 or "" or null by the compiler in the default constructor.
  
The constructor allows you to set default values for the properties of the class. If you do not provide a constructor then a default constructor will be provided. The default constructor will initialize all of the properites in the class to empty/zero/null values.
+
The constructor allows you to set default values for the properties of the class. If you do not provide a constructor then a default constructor will be provided. The default constructor will initialize all of the properties in the class to empty/zero/null values.
  
 
constructor example
 
constructor example
  
<csharp>public class Dog
+
<syntaxhighlight lang="csharp">public class Dog
 
{
 
{
public Dog()
+
//Same name as the Class and no return type
 +
        public Dog()
 
{
 
{
//constructor code
+
//constructor code set initial value for local objects
 
}
 
}
}</csharp>
+
}</syntaxhighlight>
  
 
Simple Dog class with constructor
 
Simple Dog class with constructor
  
<csharp>//Dog simple class definition
+
<syntaxhighlight lang="csharp">//Dog simple class definition
 
public class Dog
 
public class Dog
 
{
 
{
Line 154: Line 154:
 
//put eat code here  
 
//put eat code here  
 
}
 
}
}</csharp>
+
}</syntaxhighlight>
  
 
Console Example [http://iam.colum.edu/oop/gbrowser.php?file=/classsource/class4/dog1/Dog.cs Dog.cs] <br />
 
Console Example [http://iam.colum.edu/oop/gbrowser.php?file=/classsource/class4/dog1/Dog.cs Dog.cs] <br />
Line 172: Line 172:
 
Dog class with overloaded constructor
 
Dog class with overloaded constructor
  
<csharp>//Dog overloaded class definition
+
<syntaxhighlight lang="csharp">//Dog overloaded class definition
 
public class Dog
 
public class Dog
 
{
 
{
Line 213: Line 213:
  
  
}</csharp>
+
}</syntaxhighlight>
  
 
Console Example [http://iam.colum.edu/oop/gbrowser.php?file=/classsource/class4/dogOverload.cs dogOverload.cs]
 
Console Example [http://iam.colum.edu/oop/gbrowser.php?file=/classsource/class4/dogOverload.cs dogOverload.cs]
Line 234: Line 234:
 
* [http://www.csharphelp.com/archives/archive135.html Operator Overloading In C#]
 
* [http://www.csharphelp.com/archives/archive135.html Operator Overloading In C#]
  
<csharp>
+
<syntaxhighlight lang="csharp">
 
using System;
 
using System;
 
using System.Collections.Generic;
 
using System.Collections.Generic;
Line 333: Line 333:
  
 
}
 
}
</csharp>
+
</syntaxhighlight>
  
 
==Video==
 
==Video==
Line 343: Line 343:
 
;so.addParam('allowscriptaccess','always');
 
;so.addParam('allowscriptaccess','always');
 
so.addParam('wmode','opaque');
 
so.addParam('wmode','opaque');
so.addVariable('file','http://iam.colum.edu/screenz/FA13/OOP_MT/OOP_Class4_MT_Tue/OOP_Class4_MT-Tue.mp4');
+
so.addVariable('file','http://iam.colum.edu/screenz/FA13/OOP_MT/OOP_Class4_MT-Tue/OOP_Class4_MT-Tue.mp4');
 
so.addVariable('autostart','false');
 
so.addVariable('autostart','false');
 
so.write('mediaspace2');
 
so.write('mediaspace2');

Latest revision as of 16:29, 10 June 2019

Classes and Objects

Objects

Object-oriented programs are made up of objects.

"An object packages both data and the procedures that operate on that data. The procedures are typically called methods or operations. An object performs an operation when it receives a request(or message) from a client. " - Design Patterns Addison Wesley

Reasons for organizing code into Classes and Objects

  • avoid spaghetti
  • organize code into logical sections
  • simplify program designs
  • reuse

Classes are sections of code that describe real world objects. Classes define what properties and methods an object contains.

Properties: define data members of classes. Nouns

Methods: define what classes can do. Verbs

Abstraction

Humans are always abstracting and naming objects. It is how we identify things. Objects are identified by two things: properties(what the have) and methods (what they do).

Classes are used to define objects.

The hardest part about object oriented programming is choosing how to build and implement these abstractions. There are many factors that affect these decisions. It can often be very difficult to find a balance between all of the factors.

  • granularity
the size and acuracy of you abstraction
  • re usability
simpler objects are more reusable
  • flexibility
specialized objects are often less flexible
  • portability
encapsulated objects make it easier to port to other languages

Encapsulation

Allows us to use objects with out completely understanding how everything inside the object works. Encapsulation also allows the internal state of objects to be changed easily without affecting other objects or interfaces. For example you don't need to know how the phone system works in order to use a telephone. All you really need to know to use the phone system is how to dial a phone Classes

Classes are used to create objects. a simple example of class syntax in c#

public class Dog{ }

class syntax full

  [attributes] [access modifiers} class identifier [:base class]
  {class body}

Objects are instances of classes. For instance a Dog class may describe what dogs are like.

All dogs have specific properties like height, weight, color. Dog also have methods like bark. My dog rover is a specific instance of the dog class.


// Instantiate Dog objects
// Declare an instance of the Dog class
Dog firstDog;

// Allocate memory for the dog object
firstDog = new Dog();

// Declare and Allocate in one like
Dog secondDog = new Dog();

The new keyword call the classes constructor which allocated memory on the stack for the object.

access modifier restrictions

public No restrictions available to all members of the class.
private Members marked private are only available to methods of that class.
protected Members marked protected are available to methods of thay class and of classes dreived from that class.
internal Members marked as internal are available to any class within that classes assembly.
protected internal Same as protected and internal

Chair Class

Fields

  • int Height
  • int NumLegs
  • bool HasBack
  • bool HeightAdjustable

Methods

  • RaiseHeight
  • LowerHeight

Chair Class UML

ChairSimpleUML.png

  In class do phone class.

Dog Class

Fields

Microsoft has started call ing public variables fields

  • string Name
  • int Age
  • int Weight
  • string BarkSound

Methods

  • Bark
  • Eat

Dog Class UML

DogSimpleUML.png

Constructors

A classes constructor is the method that is run when an instance of the class is created. If you do not provide a constructor the complier provides one for you. The constructor is used to initiate all member properties. Unlike intrinsic types variables that are not initialized will be set to 0 or "" or null by the compiler in the default constructor.

The constructor allows you to set default values for the properties of the class. If you do not provide a constructor then a default constructor will be provided. The default constructor will initialize all of the properties in the class to empty/zero/null values.

constructor example

public class Dog
{
	//Same name as the Class and no return type
        public Dog()
	{
		//constructor code set initial value for local objects
	}
}

Simple Dog class with constructor

//Dog simple class definition
public class Dog
{
 	public string Name;		// the dog's name
	public int Age;			// the dog's age
	public int Weight;	// the dog's weight
	public string BarkSound;	// the sound of the dog's bark
	
	public Dog()
	{
		BarkSound = "Woof!!!";
	}

	public void Bark() { 
		//put bark code here
	}
	public void Eat() {
		//put eat code here 
	}
}

Console Example Dog.cs

In Class

  Make and compile a Cat class with a test class.
  *Draw UML for the Cat Class
  *Write test class
  *Compile and test test class
  *Make Dog class
  *Test Dog class with test class

OverLoading Constructors

Overloading a constructor allows object creation to happen in different ways with different parameters. The class can be overloaded to accept different parameters but each constructor method must have a unique parameter signature. Dog class with overloaded constructor

//Dog overloaded class definition
public class Dog
{
 	public string Name;		// the dog's name
	public int Age;			// the dog's age
	public int Weight;			// the dog's weight
	public string BarkSound;	// the sound of the dog's bark
	
	public Dog()
	{
		BarkSound = "Woof!!!";
	}
	public Dog(string newName)
	{
		Name = newName;
		BarkSound = "Woof!!!";
	}
	public Dog(string newName, string newBarkSound )
	{
		Name = newName;
		BarkSound = newBarkSound;
	}

	public void Bark() { 
		//put bark code here
	}
	public void Eat() {
		//put eat code here 
	}
        public string About() {
           string strAbout = "";
           strAbout += this.Name;
           strAbout += " " + this.Age;
           strAbout += " " + this.Weight;
           strAbout += " " + this.BarkSound;
           return strAbout;
        }

}


}

Console Example dogOverload.cs

HomeWork

2 own classes 1 course class

Build two of your own classes that describes some real world objects then build a test class to demonstrate how your class works. Your own class should have properties, methods, and constructors.

The classes should be in the same visual studio project. The use the program class to create new instances of your classes and set properties and call methods. Use Console.Write to show how properties have been changed and the results of calling the methods.

3 pts. It can be anything be creative. 2 pts. Create the course class UML below.

CourseUML-VS.JPG CousreUML-Dia.png

Links

using System;
using System.Collections.Generic;
using System.Text;

namespace Cat
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello I'm a cat program");

            Cat punkin = new Cat();
            Cat bella = new Cat();

            punkin.NAME = "punkin";
            punkin.WEIGHT = 5;

            bella.NAME = "bella";
            bella.WEIGHT = 20;

            Console.WriteLine(punkin.About());

            punkin.Meow();
            punkin.Eat();

            Console.WriteLine(punkin.About());

            Console.WriteLine(bella.About());

            Console.ReadKey();
        }
    }

    public class Cat
    {

        //Properties
        public string NAME;
        public int AGE;
        public int WEIGHT;
        public bool isUGLY;
        public bool isStoopid;

        public string MEOWSOUND;

        //Constructor
        public Cat()
        {
            //set seom default values
            this.NAME = "some cats name";
            this.AGE = 0;
            this.WEIGHT = 47;
            this.isUGLY = true;
            this.isStoopid = true;

            this.MEOWSOUND = "Meow!!!!";
        }

        //Methods
        public void Meow()
        {
            //write meow sound to the console
            Console.WriteLine(this.MEOWSOUND);
        }

        public void Eat()
        {
            this.WEIGHT++;
            Console.WriteLine("MMMMMM yummy");
        }

        public string About()
        {
            string strAbout = "";
            strAbout += this.NAME;
            strAbout += "\n" + this.WEIGHT;
            strAbout += "\n" + this.AGE;
            if (this.isUGLY)
            {
                strAbout += "\nI'm an Ugly cat";
            }
            else
            {
                strAbout += "\nI'm an nice looking cat";
            }

            if (this.isStoopid)
            {
                strAbout += "\nI'm an stoopid cat";
            }

            return strAbout;
        }

    }

}

Video

This text will be replaced