Duhok Polytechnic University Amedi Technical Institute/ IT Dept. Halkawt Rajab Hussain

Similar documents
Lab - 10 Write a c# program to prepend the namespace name by calling the namespaceenabled version of either function or variable?

PASCAL - RECORDS. To define a record type, you may use the type declaration statement. The record type is defined as

Duhok Polytechnic University Amedi Technical Institute/ IT Dept. Halkawt Rajab Hussain

Duhok Polytechnic University Amedi Technical Institute/ IT Dept. Halkawt Rajab Hussain

Duhok Polytechnic University Amedi Technical Institute/ IT Dept. Halkawt Rajab Hussain

cout << "How many numbers would you like to type? "; cin >> memsize; p = new int[memsize];

Department of Networks College of Bardarash Technical Institute DUHOK Polytechnic University Subject: Programming Fundamental by JAVA Course Book

Getter and Setter Methods

Duhok Polytechnic University Amedi Technical Institute/ IT Dept. Halkawt Rajab Hussain

Apllications. February 17, Indian Institute of Space Science and Technology. MA122 - Computer Programming and. Apllications. Structures.

PROGRAMMAZIONE I A.A. 2017/2018

Structures can be easily used as a function parameters. It is done similar as with variables.

Objectives: Lab Exercise 1 Part 1. Sample Run. Part 2

Object Oriented Programming in C#

C# - Reflection. It allows examining various types in an assembly and instantiate these types.

Duhok Polytechnic University Amedi Technical Institute/ IT Dept. Halkawt Rajab Hussain

Unit 8. Structures and Unions. School of Science and Technology INTRODUCTION

Introduction To C#.NET

Department of Computer Science and Engineering. Programming for Problem Solving. I Year B.Tech. (I - Sem) Assistant Professor (M.

EEE-425 Programming Languages (2013) 1

What property of a C# array indicates its allocated size? What keyword in the base class allows a method to be polymorphic?

Learn C# Errata. 3-9 The Nullable Types The Assignment Operators

And Even More and More C++ Fundamentals of Computer Science

UNIT 2. .NET Languages. C# Language Fundamentals. Operators, Types, and Variables. Variables and Types

UNIVERSITY OF THE FREE STATE DEPARTMENT OF COMPUTER SCIENCE AND INFORMATICS CSIS1614

EEE-425 Programming Languages (2013) 1

Answer the following questions on the answer sheet that is provided. The computer must be switched off while you are busy with Section A.

EL2310 Scientific Programming

Classes and Objects 3/28/2017. How can multiple methods within a Java class read and write the same variable?

Chapter 1 Getting Started

POLYTECHNIC OF NAMIBIA SCHOOL OF COMPUTING AND INFORMATICS DEPARTMENT OF COMPUTER SCIENCE

C# is a modern, g eneral-purpose, object-oriented prog ramming lang uag e developed by Microsoft and approved by Ecma and ISO.

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

Tutorial 6 CSC 201. Java Programming Concepts مبادئ الربجمة باستخدام اجلافا

Introduction to Programming Using Java (98-388)

Homework 6. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine

Database Views & Stored Procedures. Hans-Petter Halvorsen, M.Sc.

This is an open-book test. You may use the text book Be Sharp with C# but no other sources, written or electronic, will be allowed.

I Mid Semester May 2012 : Class XII : Computer Science Max Mark 50 : Time 2 Hrs. 1. a) What is macro in C++? Give example 2

UNIVERSITY OF THE FREE STATE DEPARTMENT OF COMPUTER SCIENCE AND INFORMATICS RIS 114 CLASS TEST 2 and 3 DATE: 4 August 2014 TIME: 180 minutes MARKS: 70

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Class CSE F. *slides are from CSE S at SKKU & at MIT

IST311. Advanced Issues in OOP: Inheritance and Polymorphism

Problem 1. Multiple Choice (choose only one answer)

Static, Final & Memory Management

A method is a code block that contains a series of statements. Methods. Console.WriteLine(); Console.ReadKey(); Console.ReadKey(); int.

CS 141, Introduction to Computer Science Fall Midterm Exam

Advanced Programming C# Lecture 7. dr inż. Małgorzata Janik

THE GOOD, BAD AND UGLY ABOUT POINTERS. Problem Solving with Computers-I

Advanced Programming C# Lecture 7. dr inż. Małgorzata Janik

Programming using C# LECTURE 07. Inheritance IS-A and HAS-A Relationships Overloading and Overriding Polymorphism


Java: Classes. An instance of a class is an object based on the class. Creation of an instance from a class is called instantiation.

Class Test 10. Question 1. Create a console application using visual studio 2012 ultimate.

CS 116 Week 8 Page 1

A506 / C201 Computer Programming II Placement Exam Sample Questions. For each of the following, choose the most appropriate answer (2pts each).

Software and Programming 1

Microsoft Visual Basic 2005: Reloaded

EEE-425 Programming Languages (2013) 1

Objects as a programming concept

Spring % of course grade

Week 5-1: ADT Design

Object Oriented Programming. Assistant Lecture Omar Al Khayat 2 nd Year

G205 Fundamentals of Computer Engineering. CLASS 3, Wed. Sept Stefano Basagni Fall 2004 M-W, 1:30pm-3:10pm

Lab - 1. Solution : 1. // Building a Simple Console Application. class HelloCsharp. static void Main() System.Console.WriteLine ("Hello from C#.

Class, Variable, Constructor, Object, Method Questions

VISUAL PROGRAMMING_IT0309 Semester Number 05. G.Sujatha & R.Vijayalakshmi Assistant professor(o.g) SRM University, Kattankulathur

Two hours QUESTION PAPER MUST NOT BE REMOVED FROM THE EXAM ROOM UNIVERSITY OF MANCHESTER SCHOOL OF COMPUTER SCIENCE

I BCS-031 BACHELOR OF COMPUTER APPLICATIONS (BCA) (Revised) Term-End Examination. June, 2015 BCS-031 : PROGRAMMING IN C ++

Developing Microsoft.NET Applications for Windows (Visual Basic.NET)

CONSTRUCTOR & Description. String() This initializes a newly created String object so that it represents an empty character sequence.

This is an open-book test. You may use the text book Be Sharp with C# but no other sources, written or electronic, will be allowed.

INDEX. A SIMPLE JAVA PROGRAM Class Declaration The Main Line. The Line Contains Three Keywords The Output Line

CS111: PROGRAMMING LANGUAGE II. Lecture 1: Introduction to classes

Inheritance, and Polymorphism.

Building non-windows applications (programs that only output to the command line and contain no GUI components).

CSCI 1370 APRIL 26, 2017

PES INSTITUTE OF TECHNOLOGY

Interview Questions of C++

CS32 - Week 4. Umut Oztok. Jul 15, Umut Oztok CS32 - Week 4

Constructor - example

More C++ : Vectors, Classes, Inheritance, Templates

CSC1322 Object-Oriented Programming Concepts

More C++ : Vectors, Classes, Inheritance, Templates. with content from cplusplus.com, codeguru.com

Chapter 11. Abstract Data Types and Encapsulation Concepts

EXAMINATION FOR THE DIPLOMA IN INFORMATION TECHNOLOGY; YEAR 2

Programming Fundamentals II (C++ II) Final Exam June 17 th, Sun, 2007

RIS214. Class Test 3

Classes. Logical method to organise data and functions in a same structure. Also known as abstract data type (ADT).

DC69 C# and.net JUN 2015

IT 374 C# and Applications/ IT695 C# Data Structures

Homework 5. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine

University of Cape Town ~ Department of Computer Science. Computer Science 1015F ~ 2007

Question 1 Consider the following structure used to keep employee records:

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #1 Examination 12:30 noon, Tuesday, February 14, 2012

Lab 10: Structs and Enumeration

Chapter 10. Class & Objects in JAVA. By: Deepak Bhinde PGT Com.Sc.

User Defined Classes. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

Final Exam CS 251, Intermediate Programming December 10, 2014

C# - ATTRIBUTES. Specifying an Attribute. Predefined Attributes. AttributeUsage

Transcription:

Duhok Polytechnic University Amedi Technical Institute/ IT Dept. By Halkawt Rajab Hussain 2016-04-02

: Structure declaration and initialization. Access to fields of structure. Array of Structs. 11/10/2016 1

In C# and.net Framework there are two implementations of the concept of "class" from the object-oriented programming: classes and structures. Classes are defined through the keyword class. Structures are defined through the keyword struct. The main difference between a structure and a class is that: 1. Classes are reference types (references to some address in the heap which holds their members). 2. Structures (structs) are value types (they directly hold their members in the program execution stack). 11/10/2016 2

Structure declaration and initialization. To define a structure, you must use the struct statement. The struct statement defines a new data type, with more than one member for your program. For example, here is the way you can declare the Book structure: struct Books public string title; public string author; public string subject; public int book_id; ; 11/10/2016 3

Initialization: StudentInfo st = new StudentInfo() Id = 123, Name = "Ali", Phone = 4577678 ; StudentInfo st = new StudentInfo() st.id=123; st.name= Ali ; st.phone=4577678; Direct set values set values after create new object 11/10/2016 4

The following program shows the use of the structure: using System; struct Books public string title; public string author; public string subject; public int book_id; ; public class teststructure public static void Main(string[] args) Books Book1; /* Declare Book1 of type Book */ Books Book2; /* Declare Book2 of type Book */ /* book 1 specification */ Book1.title = "C Programming"; Book1.author = "Nuha Ali"; Book1.subject = "C Programming Tutorial"; Book1.book_id = 6495407; /* book 2 specification */ Book2.title = "Telecom Billing"; Book2.author = "Zara Ali"; Book2.subject = "Telecom Billing Tutorial"; Book2.book_id = 6495700; /* print Book1 info */ Console.WriteLine( "Book 1 title : 0", Book1.title); Console.WriteLine("Book 1 author : 0", Book1.author); Console.WriteLine("Book 1 subject : 0", Book1.subject); Console.WriteLine("Book 1 book_id :0", Book1.book_id); /* print Book2 info */ Console.WriteLine("Book 2 title : 0", Book2.title); Console.WriteLine("Book 2 author : 0", Book2.author); Console.WriteLine("Book 2 subject : 0", Book2.subject); Console.WriteLine("Book 2 book_id : 0", Book2.book_id); Console.ReadKey(); 11/10/2016 5

Features of C# Structures: Structures can have methods, fields, indexers, properties, operator methods, and events. Structures can have defined constructors, but not destructors. However, you cannot define a default constructor for a structure. The default constructor is automatically defined and cannot be changed. Unlike classes, structures cannot inherit other structures or classes. Structures cannot be used as a base for other structures or classes. A structure can implement one or more interfaces. Structure members cannot be specified as abstract, virtual, or protected. 11/10/2016 6

When you create a struct object using the New operator, it gets created and the appropriate constructor is called. Unlike classes, structs can be instantiated without using the New operator. If the New operator is not used, the fields remain unassigned and the object cannot be used until all the fields are initialized. HomeWork: Rewrite the previous example, creates two methods, getvalue to get values of book properties and display method to display book information. 11/10/2016 7

Example The following program, if the access specifier for struct properties n1,n2 and res are privates, how to set values to n1,n2 and get result of res? using system; namespace Exam class test struct Calc public int n1, n2, res; ; static void Main(string[] args) Clac c; c.n1=5; c.n2=10; c.res=c.n1+c.n2; Console.WriteLine(c.res); 11/10/2016 8

Defining (Creating a Custom struct Type) The following example demonstrates how to define a custom struct. In this case,the struct is a StudentInfo with Id, Name and Phone properties. Example Create custom structure for student called StudentInfo? struct StudentInfo private int m_id; public int Id get return m_id; set m_id = value; private string m_name; public string Name get return m_name; set m_name = value; private int m_phone; public int Phone get return m_phone; set m_phone = value; 11/10/2016 9

Array of structs: Structures Write a console application that receives the following information for a set of students: studentid, studentname, coursename, date-of-birth.. The application should also be able to display the information being entered.. Implement this using structs? struct student public int s_id; public String s_name, c_name, dob; class Program static void Main(string[] args) student[] arr = new student[4]; for(int i = 0; i < 4; i++) Console.WriteLine("Please enter StudentId, StudentName, CourseName, Date-Of-Birth"); arr[i].s_id = Int32.Parse(Console.ReadLine()); arr[i].s_name = Console.ReadLine(); arr[i].c_name = Console.ReadLine(); arr[i].s_dob = Console.ReadLine(); 11/10/2016 10

Example This example declares a struct with three members: a property, a method, and a private field. It creates an instance of the struct and puts it to use: using System; struct SimpleStruct private int xval; public int X get return xval; set if (value < 100) xval = value; public void DisplayX() Console.WriteLine("The stored value is: 0", xval); class TestClass public static void Main() SimpleStruct ss = new SimpleStruct(); ss.x = 5; ss.displayx(); 11/10/2016 11

Example Write complite program in C# console application that demonstrates struct initialization using both default and parameterized constructors. using system; Namespace exam public struct CoOrds public int x, y; public CoOrds(int p1, int p2) x = p1; y = p2; class TestCoOrds static void Main() CoOrds coords1 = new CoOrds(); CoOrds coords2 = new CoOrds(10, 10); // Display results: Console.Write("CoOrds 1: "); Console.WriteLine("x = 0, y = 1", coords1.x, coords1.y); Console.Write("CoOrds 2: "); Console.WriteLine("x = 0, y = 1", coords2.x, coords2.y); // Keep the console window open in debug mode. Console.WriteLine("Press any key to exit."); Console.ReadKey(); 11/10/2016 12

Thanks Any Questions 11/10/2016 13