INTRODUCTION TO CLASSES & OBJECTS CREATING CLASSES USING C#

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

PHP functions What are Functions? A function structure:
Introduction to classes Sangeetha Parthasarathy 06/11/2001.
Based on Java Software Development, 5th Ed. By Lewis &Loftus
1 Classes and Data Abstraction Chapter What a Class ! ! Specification and implementation Private and public elements Declaring classes data and.
C++ Classes & Data Abstraction
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
Class template Describing a generic class Instantiating classes that are type- specific version of this generic class Also are called parameterized types.
Classes, Encapsulation, Methods and Constructors
1 Review: Two Programming Paradigms Structural (Procedural) Object-Oriented PROGRAM PROGRAM FUNCTION OBJECT Operations Data OBJECT Operations Data OBJECT.
More C++ Bryce Boe 2013/07/18 CS24, Summer 2013 C.
Inheritance using Java
Introduction to C++. Overview C++? What are references Object orientation Classes Access specifiers Constructor/destructor Interface-implementation separation.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Classes Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd Spetember 2006.
Classes and Class Members Chapter 3. 3 Public Interface Contract between class and its clients to fulfill certain responsibilities The client is an object.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Introduction To Classes Chapter Procedural And Object Oriented Programming Procedural programming focuses on the process/actions that occur in a.
Tuc Goodwin  Object and Component-Oriented Programming  Classes in C#  Scope and Accessibility  Methods and Properties  Nested.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
CSE 114 Computer Science I Objects Lake Superior, Michigan.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
Chapter 4 -2 part Writing Classes 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
CSC241 Object-Oriented Programming (OOP) Lecture No. 6.
Chapter 3 Part I. 3.1 Introduction Programs written in C ◦ All statements were located in function main Programs written in C++ ◦ Programs will consist.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
08 Encapsulation and Abstraction. 2 Contents Defining Abstraction Levels of Abstraction Class as Abstraction Defining a Java Class Instantiating a Class.
Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315.
AP Computer Science A – Healdsburg High School 1 Unit 9 - Why Use Classes - Anatomy of a Class.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
72 4/11/98 CSE 143 Abstract Data Types [Sections , ]
Defining Classes I Part B. Information hiding & encapsulation separate how to use the class from the implementation details separate how to use the class.
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
YG - CS Concept of Encapsulation What is encapsulation? - data and functions/methods are packaged together in the class normally.
1 Data Structures CSCI 132, Spring 2014 Lecture 2 Classes and Abstract Data Types Read Ch Read Style Guide (see course webpage)
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-1 Learning Objectives  Classes  Constructors  Principles of OOP  Class type member.
CPSC 252 ADTs and C++ Classes Page 1 Abstract data types (ADTs) An abstract data type is a user-defined data type that has: private data hidden inside.
Introduction to Classes and Objects CS-2303, C-Term C++ Program Structure Typical C++ Programs consist of:– main –A function main –One or more classes.
Introduction to C++ programming Recap- session 1 Structure of C++ program Keywords Operators – Arithmetic – Relational – Logical Data types Classes and.
OOP Details Constructors, copies, access. Initialize Fields are not initialized by default:
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Andrew(amwallis) Classes!
Procedural and Object-Oriented Programming
Examples of Classes & Objects
Review What is an object? What is a class?
Using local variable without initialization is an error.
Lecture 13 Writing Classes Richard Gesick.
Class Constructor Recall: we can give default values to instance variables of a class The “numberOfPlayers” variable from the “PlayerData” class before.
Classes and Data Abstraction
An Introduction to Java – Part II
Simple Classes in C# CSCI 293 September 12, 2005.
Classes & Objects: Examples
Classes, Encapsulation, Methods and Constructors (Continued)
Learning Objectives Classes Constructors Principles of OOP
Defining Classes and Methods
Simple Classes in Java CSCI 392 Classes – Part 1.
© A+ Computer Science - Classes And Objects © A+ Computer Science -
Which best describes the relationship between classes and objects?
Tonga Institute of Higher Education
Outline Anatomy of a Class Encapsulation Anatomy of a Method
Fundaments of Game Design
CIS 199 Final Review.
NAME 436.
Final and Abstract Classes
Classes and Objects CGS3416 Spring 2019.
CSG2H3 Object Oriented Programming
Presentation transcript:

INTRODUCTION TO CLASSES & OBJECTS CREATING CLASSES USING C#

OBJECTS VS CLASSES Classes are abstract definitions of what an object can do and values it can hold Classes are a mix of code and data An object is an instance of a class Classes are general and Objects are specific examples

DATA HIDING Class data is private Protected from unsafe manipulation Modified only by class specific methods Methods may be public or private Private methods for internal class use only Public methods are defined interface to the class’ data

DECLARING THE DATA class Die { private int _sides; private int _value; Variables are declared private Variable names start with underscore (_) to highlight that they are private (optional) Variables declared as private

CONSTRUCTORS Constructors are methods in a class that set the initial state of an object Constructors: Are public Have the same name as the class Do not have a specified type Initialize private values in a class

SAMPLE CONSTRUCTORS public Die() { _sides = 6; ChangeValue(); } public Die(int sides) { _sides = sides; ChangeValue(); } Default constructor No parameter Non default constructor Parameter(s) Default value for number of sides Number of sides set to value passed to the constructor

ACCESSING THE DATA Class data should be accessed using methods or properties Changes to data should be validated carefully inside the class if allowed at all Some data should not be allowed to be changed after the constructor

CHANGING DATA - METHOD private void ChangeValue() { _value = r.Next(0, _sides) + 1; } Declared private Used only in the class Code to set value is “hidden”

RETURNING DATA - METHOD public int Roll() { ChangeValue(); return _value; } _value changed in a private method _value returned by method not by direct access

GETTING/SETTING DATA - PROPERTY public int Value { get { return _value; } set { if (value > 0 && value < _sides+1) _value = value; } } _value returned by method not by direct access _value set in a protected fashion

DECLARING AN OBJECT Die dSix = new Die(); Die d12 = new Die(12); Default constructor Six sides are default Non default specifies the number of sides

REVIEW An instance of a class is an object One or more constructors to set initial values Constructors have the same name as the class Access to data is limited and protected