Outline Class and Object-Oriented Programing –Encapsulation and inheritance Example –Commission Employee –Encapsulation and inheritance Strings and Formatting.

Slides:



Advertisements
Similar presentations
Class Scope class Student { private: string id; string firstName, lastName; float gpa; public: void Read() { cin >> id >> firstName >> lastName >> gpa;
Advertisements

COP 3003 Object-Oriented Programming - Inheritance Dr. Janusz Zalewski, Fall 2013 Prepared by Dr Dahai Guo.
Programming With Java ICS 201 University Of Ha’il1 Chapter 8 Abstract Class.
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
OBJECT-ORIENTED PROGRAMMING. What is an “object”? Abstract entity that contains data and actions Attributes (characteristics) and methods (functions)
C++ Inheritance Systems Programming.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Object-Oriented Programming: Inheritance Deitel &Deitel Java SE 8.
Abstract Classes. Lecture Objectives To learn about abstract classes To understand how to inherit abstract classes To understand how to override abstract.
 2006 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
CS102--Object Oriented Programming Review 1: Chapter 1 – Chapter 7 Copyright © 2008 Xiaoyan Li.
OOP Languages: Java vs C++
CSE 333 – SECTION 4. Overview Pointers vs. references Const Classes, constructors, new, delete, etc. More operator overloading.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
Module 7: Object-Oriented Programming in Visual Basic .NET
CSE 1301 Lecture 11 Object Oriented Programming Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
BIM313 – Advanced Programming Techniques Object-Oriented Programming 1.
BPJ444: Business Programming Using Java Classes and Objects Tim McKenna
C# Programming Fundamentals of Object-Oriented Programming Fundamentals of Object-Oriented Programming Introducing Microsoft.NET Introducing Microsoft.NET.
CSE 1302 Lecture 7 Object Oriented Programming Review Richard Gesick.
Programming Languages and Paradigms Object-Oriented Programming (Part II)
Programming Languages and Paradigms Object-Oriented Programming.
Method signature Name and parameter list public static void test() public static int test() => Syntax error, Duplicate method test You cannot declare more.
More on Object-Oriented Programming: Inheritance 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
Tuc Goodwin  Object and Component-Oriented Programming  Classes in C#  Scope and Accessibility  Methods and Properties  Nested.
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.
Structured Programming Good for programming in the small Often doesn't scale up Limitations –Changes at top may affect lower-level algorithms –Code reuse.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 2 – Classes and objects.
C/C++ 3 Yeting Ge. Static variables Static variables is stored in the static storage. Static variable will be initialized once. 29.cpp 21.cpp.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
O O P Polymorphism Object Oriented Programming Prepared & Presented by: dr.Ismail Farahat Chapter 4.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Inheritance.
OOP in C++ CS 124. Program Structure C++ Program: collection of files Source (.cpp) files be compiled separately to be linked into an executable Files.
© 2007 Lawrenceville Press Slide 1 Chapter 8 Objects  A variable of a data type that is a class. Also called an instance of a class.  Stores data  Can.
Object Oriented Programming
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
Inheritance ndex.html ndex.htmland “Java.
YG - CS Concept of Encapsulation What is encapsulation? - data and functions/methods are packaged together in the class normally.
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
Strings in C++/CLI us/library/system.string.aspxhttp://msdn.microsoft.com/en- us/library/system.string.aspx public: static.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
Basic Class Structure. Class vs. Object class - a template for building an object –defines the instance data that the object will hold –defines instance.
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 4th Edition.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Unit 2. Constructors It initializes an object when it is created. It has same as its class and syntactically similar to a method. Constructor have no.
Object-Oriented Programming Review 1. Object-Oriented Programming Object-Oriented Programming languages vary but generally all support the following features:
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
Java 5 Class Anatomy. User Defined Classes To this point we’ve been using classes that have been defined in the Java standard class library. Creating.
Classes (Part 1) Lecture 3
Examples of Classes & Objects
Object-Oriented Programming: Inheritance
Object-Oriented Programming: Inheritance
Chapter 4: Creating Objects in C#
More Object Oriented Programming
March 29th Odds & Ends CS 239.
Chapter 9 Object-Oriented Programming: Inheritance
Classes & Objects: Examples
Object-Oriented Programming: Inheritance
Midterm Thursday, in class.
Recitation Course 0610 Speaker: Liu Yu-Jiun.
Object-Oriented Programming
CIS 199 Final Review.
Chapter 9 Object-Oriented Programming: Inheritance
Object-Oriented Programming: Inheritance
Presentation transcript:

outline Class and Object-Oriented Programing –Encapsulation and inheritance Example –Commission Employee –Encapsulation and inheritance Strings and Formatting In-class assignment –Derived class –Access scope

Class Procedural vs Object-oriented User-defined types (programmer-defined types) class Encapsulation (private and public) Member Variables and access modifiers Member Methods Constructor Constructor and method overloading –Method overloading: two or more methods have same name but with different number or type of parameters. Using class

ref class Square { public: Square (int d) { Dims = d; } int Area () { return Dims*Dims; } int Dims; };

Use ref class Square ^sqr1 = gcnew Square(); // a handle sqr1->Dims = 5; int area = sqr1->Area (); Or: Square sqr2; // a local or a stack instance sqr2.Dims = 3; int i = sqr2.Dims; int area = sqr2.Area ();

Example CommissionEmployee firstName lastName socialSecurityNumber grossSales commissionRate Read/write these variables getXXX() / setXXX() earnings() print() private and public accesses

Constructor –Initialize the object at instantiate –A special method: called at object creation by gcnew(), same name as the class, no return (not defined with void either), can take any number of parameters, public Constructor overloading class testclass { public: testclass () { x = y = 0; } testclass(Int32 a) { x = y = a; } // x =a; y = 10; testclass(Int32 a, Int32 b) { x = a; y = b; } private: Int32 x; Int32 y; };

testclass ^a1 = gcnew testclass(100); testclass ^a2 = gcnew testclass(); testclass ^a3 = gcnew testclass(100, 2);

static class methods and variables Static variable in a class: –only one copy of the variable is created, and it is shared by all the instances of the class. (not associated with an instance) –Initiated only at the first time the class is instantiated. ref class staticVar { static int counts = 3; }; Static method in a class: –the method is accessible without the need to instantiate the class. –Use only static member variables. Public: static int get_x () { return x; } –To use it: Console::WriteLine(className::get_x());

Inheritance Code reuse Base class and derived class in C++ this and class scope(::) in C++ Method overriding –A method in the derived ref class has an identical signature to the base ref class Access modifiers protected

using namespace System; // Base class ref class Square { public: int Area () { return Dims*Dims;} int Dims; }; // Derived class Ref class Cube : public Square { public: int Volume() { return Area()* Dims; } };

// inheritance in action void main(void) { Cube ^cube = gcnew Cube (); cube->Dims = 3; Console::WriteLine(cube->Dims); Console::WriteLine(cube->Area()); Console::WriteLine(cube->Volume()); }

Access modifiers Public –Accessible by external functions and methods –Accessible to derived ref classes Private –Not accessible by external functions and methods –Not accessible to derived ref classes Protected –Not accessible by external functions and methods –Accessible to derived ref classes

Example CommissionEmployee BasePlusCommissionEmployee firstName lastName socialSecurityNumber grossSales commissionRate earnings() print() baseSalary earnings() print()

Strings and Formatting in C++/CLI us/library/system.string.aspxhttp://msdn.microsoft.com/en- us/library/system.string.aspx public: static String^ Format( String^ format,... array ^ args ) Format:{index[,length][:formatString]} {0}, {0, 10}, {0, -10}, {0, -10:D6}, {0,-10:f} double d = 134; int a = 30; Console::WriteLine("{0,-10:f}{1,-10}",d, a);

String^ dateString1 = "1"; String^ fmtString = String::Format("{0,10}",dateString1); // -10 to left, 10 to right Console::WriteLine(fmtString); int dateString2 = 1; String^ fmtString = String::Format("{0,-10:D6}",dateString2); Console::WriteLine(fmtString); String^ result = nullptr; result += String::Format("{0,10}", L"X4"); result += String::Format("{0, 10:D6}", dateString2); result += String::Format("{0,10}", L"end"); Console::WriteLine(result);

String^ result1 = L"X4"; String^ result2 = String::Format("{0} ", dateString2); String^ result3 = L"end"; Console::WriteLine("{0,10} {1, 10} {2,10}", result1, result2, result3); fmtString = String::Format("{0,10} {1, 10} {2,10}", result1, result2, result3); Console::WriteLine(fmtString); fmtString = String::Format("{0,10} {1, 10} {2,10}", result1, dateString1, result3); Console::WriteLine(fmtString);