C# object oriented programming basics

object oriented programming fundamentals and object oriented programming generalization
Dr.MasonHanks Profile Pic
Published Date:23-07-2017
Your Website URL(Optional)
Object Oriented Software Development 2. C object oriented programming basics www.ThesisScientist.comWhat is object oriented programming?  A programming approach which uses objects  An object is a software component which has properties and behaviour  When a program runs objects are created and work together to perform the program’s tasks  Most modern programming languages support object orientation  C, Java, VB.NET, C++, PHP, etc. www.ThesisScientist.comWhere should we use it?  Object-orientation offers some key benefits:  Code re-use – DRY principle  Ability to model real-world environments  Understandability  Many kinds of software application can benefit from object oriented approach  GUI applications, web applications, games, etc. www.ThesisScientist.comObjects  An entity, or thing, is represented as an object in the program  e.g. an object representing an Employee in a company  Objects have attributes to represent state of object, e.g. name, location of an Employee  Objects have methods to define the actions, or behaviour, which object can perform, e.g. an Employee could record that he or she worked some overtime hours www.ThesisScientist.comResponsibilities and collaboration  Objects have responsibilities  This allows objects to interact, or collaborate, with each other  Program consists of objects which interact, just as real world entities interact  For example, in a real-world company each person has a job to do (responsibilities), and people collaborate to achieve the company’s aims www.ThesisScientist.comEncapsulation  Objects are able to collaborate through behaviour and attributes which are public  Objects can also have behaviour and attributes which are private  These are for the object itself to use in performing its responsibilities  Public behaviour may modify private attributes or use private behaviour  Collaborating objects do not need to know about these www.ThesisScientist.comClasses  May have more than one object of the same kind that have common characteristics  Class is a template for creating objects of the same kind  A bit like a job description for a real-world job  Employee class can be used to create many Employee objects  When we write the code we are actually writing the class definitions www.ThesisScientist.comClasses and objects  An object is a specific instance of a class  Class defines the attributes and methods which are common to all instances of class  Each object can have its own specific values for these attributes  Each Employee object will have a Name, but the value is different in each object  Objects are created from the class as the program runs www.ThesisScientist.comWhat’s in a class  A class is written as a named a block of code  Contains declarations of variables to represent the attributes  Contains blocks of code, nested within the class, to define the methods  Each method contains a sequence of steps which carry out the action which that method defines  Usually contains one or more constructors www.ThesisScientist.comC class example code  OOBasicsDemo project  Employee.cs www.ThesisScientist.comClass diagrams private – cannot be accessed by other objects class name attributes methods public – can be accessed by other objects www.ThesisScientist.comObject diagrams emp1 : Employee name = Michael each object here is an instance username = michael of the Employee class with its currentLocation = loc own values for the attributes phoneNumber = 1234 emp2 : Employee name = Susan username = susan currentLocation = loc phoneNumber = 4321 www.ThesisScientist.comVariables  A variable is a name given to a piece of information in a program  Allows us write code which refers to and uses that information  Actual value will depend on what has happened as the program runs  May be different each time the program runs and may change as it runs www.ThesisScientist.comDeclaring variables  In C a variable needs to be declared before it can be used  Declaring a variable means specifying that a particular piece of information may exist by giving it a name and stating the type int myValue Declares a variable of type int with name “myValue” www.ThesisScientist.comGiving values to variables  Can assign a value to a variable, e.g. myValue = 3 myValue = x where x is another int variable myValue = 3x  Can assign at the same time as declaring, e.g int myValue = 3 www.ThesisScientist.comObject references  In an OO program, a variable can be an object reference  A name given to an object  Allows us write code which refers to and uses that object  Need to declare variable: Employee emp  Need to create an object and assign it to this variable www.ThesisScientist.comInstance variables  Attributes of an object are also known as instance variables or fields  prefer these as attribute has another meaning in C  Each instance variable represents a piece of information of a specific type, which can be:  a C built-in type, e.g. string, int  any .NET Framework type, e.g. DateTime (we will look at .NET types in more detail later)  any class in your application, e.g. Location (a class which would represent a work location) www.ThesisScientist.comC creating objects example code  OOBasicsDemo project  Employee.cs  Program.cs www.ThesisScientist.comCreating objects example  Test program creates some objects and make them do something  Program.cs  Main method – entry point to a C application  Creates object using new keyword  emp1, emp2 are object references each of which “points” to an object of type Employee www.ThesisScientist.comnull references  A reference can be null  The reference is declared but does not actually point to an object reference declared, not assigned to object - null reference assigned to object reference no longer assigned to object - null www.ThesisScientist.com

Advise: Why You Wasting Money in Costly SEO Tools, Use World's Best Free SEO Tool Ubersuggest.